eobeom 发表于 2021-11-9 16:14:38

请问如何把按钮放到最右边?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.EventObject;

public class SwingTextFieldView extends JPanel implements ActionListener {

    public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
      });
    }

    protected JTextField textField;
    protected JTextArea textArea;
    protected JButton enter;
    private final static String newline = "\n";

    public SwingTextFieldView() {
      super(new GridBagLayout());

      textField = new JTextField(30);
      textField.addActionListener(this);

      enter = new JButton("Enter");
      enter.addActionListener(this);
      enter.setMnemonic(KeyEvent.VK_E);
      add(enter);

      textArea = new JTextArea(10, 40);
      textArea.setEditable(false);
      JScrollPane scrollPane = new JScrollPane(textArea);

      GridBagConstraints c = new GridBagConstraints();
      c.gridwidth = GridBagConstraints.REMAINDER;

      c.fill = GridBagConstraints.HORIZONTAL;
      add(textField, c);

      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1.0;
      c.weighty = 1.0;
      add(scrollPane, c);


    }

    public void actionPerformed(ActionEvent evt) {
      String text = textField.getText();
      if(evt.getSource() == enter) {
            textArea.append(text + newline);
            textField.selectAll();

            textArea.setCaretPosition(textArea.getDocument().getLength());
      }
    }
    private static void createAndShowGUI() {
      JFrame frame = new JFrame("SwingTextFieldView");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(new TextDemo());
      SwingTextFieldView newContenPanw = new SwingTextFieldView();
      newContenPanw.setOpaque(true);
      frame.setContentPane(newContenPanw);
      frame.pack();
      frame.setVisible(true);
    }
}

我想把Enter放到最右边 但是它一直在左边 我有用setBounds试过 但是一直还是在最右边 请问有什么方法可以放到最右边吗?

不会起名字的我 发表于 2021-11-9 20:31:03

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.EventObject;

public class SwingTextFieldView extends JPanel implements ActionListener {

    public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
      });
    }

    protected JTextField textField;
    protected JTextArea textArea;
    protected JButton enter;
    private final static String newline = "\n";

    public SwingTextFieldView() {
setLayout(new BorderLayout());                                       //设置为边界布局模式

      textField = new JTextField(30);
      textField.addActionListener(this);

      enter = new JButton("Enter");
      enter.addActionListener(this);
      enter.setMnemonic(KeyEvent.VK_E);
      add(enter,BorderLayout.EAST);                               //将按钮放置此面板中的东侧(右侧)

      textArea = new JTextArea(10, 40);
      textArea.setEditable(false);
      JScrollPane scrollPane = new JScrollPane(textArea);

      GridBagConstraints c = new GridBagConstraints();
      c.gridwidth = GridBagConstraints.REMAINDER;

      c.fill = GridBagConstraints.HORIZONTAL;
      add(textField, c);

      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1.0;
      c.weighty = 1.0;
      add(scrollPane, c);


    }

    public void actionPerformed(ActionEvent evt) {
      String text = textField.getText();
      if(evt.getSource() == enter) {
            textArea.append(text + newline);
            textField.selectAll();

            textArea.setCaretPosition(textArea.getDocument().getLength());
      }
    }
    private static void createAndShowGUI() {
      JFrame frame = new JFrame("SwingTextFieldView");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(new TextDemo());
      SwingTextFieldView newContenPanw = new SwingTextFieldView();
      newContenPanw.setOpaque(true);
      frame.setContentPane(newContenPanw);
      frame.pack();
      frame.setVisible(true);
    }
}

eobeom 发表于 2021-11-10 15:58:38

不会起名字的我 发表于 2021-11-9 20:31


显示了
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
        at java.desktop/java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:431)
        at java.desktop/java.awt.Container.addImpl(Container.java:1156)
        at java.desktop/java.awt.Container.add(Container.java:1001)
        at week10.SwingTextFieldView.<init>(SwingTextFieldView.java:44)
        at week10.SwingTextFieldView.createAndShowGUI(SwingTextFieldView.java:67)
        at week10.SwingTextFieldView$1.run(SwingTextFieldView.java:15)
        at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
错误
页: [1]
查看完整版本: 请问如何把按钮放到最右边?