Fancy0731 2021-11-09 15:09 采纳率: 50%
浏览 80
已结题

java 请问如何把button放在最右边?


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 {
    protected JTextField textField;
    protected JTextArea textArea;
    protected JButton b;
    private final static String newline = "\n";

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

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

        add(b);

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

        textArea = new JTextArea(10, 30);
        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);

        JButton b = new JButton("Enter");
    }

    public void actionPerformed(ActionEvent evt) {
        String text = textField.getText();
        if(evt.getSource() == b) {
            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);
    }

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

现在button在最左边,但是我想把它放在最右边 请问要如何解决?

  • 写回答

1条回答 默认 最新

  • iilmx 2021-11-09 15:37
    关注

    有个方法可以设置button的坐标点,从而实现button处于哪个位置
    xxx.setBounds(x,y,width,height);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    问题事件

    • 系统已结题 11月20日
    • 已采纳回答 11月12日
    • 创建了问题 11月9日

    悬赏问题

    • ¥20 关于TRPD(波形特征)局部放电特征提取
    • ¥15 C语言快速排序函数纠错
    • ¥15 C#的一个应用程序书写
    • ¥65 页面调接口时加载卡住不响应
    • ¥35 用C语言解决编程问题
    • ¥15 unity硬件连接与使用
    • ¥15 鱼缸加热棒的数据分析或者实际案例也行
    • ¥15 postgresql11安装完成后,pgadmin无法启动
    • ¥15 (标签-无人机|关键词-Matlab代码)
    • ¥15 执行shell脚本提示参数太多