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

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日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器