鱼C论坛

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

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

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

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

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

x
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试过 但是一直还是在最右边 请问有什么方法可以放到最右边吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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);
    }
}

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

使用道具 举报

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

显示了
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)
错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-12-22 17:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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