鱼C论坛

 找回密码
 立即注册
查看: 2616|回复: 2

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

[复制链接]
发表于 2021-11-9 16:14:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyEvent;
  6. import java.util.EventObject;

  7. public class SwingTextFieldView extends JPanel implements ActionListener {

  8.     public static void main(String[] args) {
  9.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  10.             public void run() {
  11.                 createAndShowGUI();
  12.             }
  13.         });
  14.     }

  15.     protected JTextField textField;
  16.     protected JTextArea textArea;
  17.     protected JButton enter;
  18.     private final static String newline = "\n";

  19.     public SwingTextFieldView() {
  20.         super(new GridBagLayout());

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

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

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

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

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

  34.         c.fill = GridBagConstraints.BOTH;
  35.         c.weightx = 1.0;
  36.         c.weighty = 1.0;
  37.         add(scrollPane, c);


  38.     }

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

  44.             textArea.setCaretPosition(textArea.getDocument().getLength());
  45.         }
  46.     }
  47.     private static void createAndShowGUI() {
  48.         JFrame frame = new JFrame("SwingTextFieldView");
  49.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.         frame.add(new TextDemo());
  51.         SwingTextFieldView newContenPanw = new SwingTextFieldView();
  52.         newContenPanw.setOpaque(true);
  53.         frame.setContentPane(newContenPanw);
  54.         frame.pack();
  55.         frame.setVisible(true);
  56.     }
  57. }
复制代码


我想把Enter放到最右边 但是它一直在左边 我有用setBounds试过 但是一直还是在最右边 请问有什么方法可以放到最右边吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-9 20:31:03 | 显示全部楼层
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyEvent;
  6. import java.util.EventObject;

  7. public class SwingTextFieldView extends JPanel implements ActionListener {

  8.     public static void main(String[] args) {
  9.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  10.             public void run() {
  11.                 createAndShowGUI();
  12.             }
  13.         });
  14.     }

  15.     protected JTextField textField;
  16.     protected JTextArea textArea;
  17.     protected JButton enter;
  18.     private final static String newline = "\n";

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

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

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

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

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

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

  34.         c.fill = GridBagConstraints.BOTH;
  35.         c.weightx = 1.0;
  36.         c.weighty = 1.0;
  37.         add(scrollPane, c);


  38.     }

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

  44.             textArea.setCaretPosition(textArea.getDocument().getLength());
  45.         }
  46.     }
  47.     private static void createAndShowGUI() {
  48.         JFrame frame = new JFrame("SwingTextFieldView");
  49.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.         frame.add(new TextDemo());
  51.         SwingTextFieldView newContenPanw = new SwingTextFieldView();
  52.         newContenPanw.setOpaque(true);
  53.         frame.setContentPane(newContenPanw);
  54.         frame.pack();
  55.         frame.setVisible(true);
  56.     }
  57. }
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-10 15:58:38 | 显示全部楼层

显示了
  1. Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
  2.         at java.desktop/java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:431)
  3.         at java.desktop/java.awt.Container.addImpl(Container.java:1156)
  4.         at java.desktop/java.awt.Container.add(Container.java:1001)
  5.         at week10.SwingTextFieldView.<init>(SwingTextFieldView.java:44)
  6.         at week10.SwingTextFieldView.createAndShowGUI(SwingTextFieldView.java:67)
  7.         at week10.SwingTextFieldView$1.run(SwingTextFieldView.java:15)
  8.         at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
  9.         at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
  10.         at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
  11.         at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
  12.         at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
  13.         at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
  14.         at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
  15.         at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
  16.         at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
  17.         at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
  18.         at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
  19.         at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
  20.         at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
复制代码

错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-21 03:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表