Java Swing:如何对齐JButton

Java Swing:如何对齐JButton,java,swing,alignment,jbutton,Java,Swing,Alignment,Jbutton,我一直在尝试对齐一个JButton,我在google上搜索了一些东西,但似乎没有一个奏效。我找到的所有代码都不会移动JButtons。我想对齐JButtons,使它们看起来像计算器的面。这是我的密码: import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.J

我一直在尝试对齐一个JButton,我在google上搜索了一些东西,但似乎没有一个奏效。我找到的所有代码都不会移动JButtons。我想对齐JButtons,使它们看起来像计算器的面。这是我的密码:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class CalculatorUI extends JFrame
{
    //Variables.
    private JTextField text1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JButton button0;
    private JPanel pane;

    public CalculatorUI()
    {
        super ("Calculator");
        setLayout(new FlowLayout());
        text1 = new JTextField(20);
        text1.setEditable(false);
        add(text1);
        button1 = new JButton("1");
        button1.setAlignmentX(LEFT_ALIGNMENT);
        add(button1);
        button2 = new JButton("2");
        add(button2);
        button3 = new JButton("3");
        add(button3);
        button4 = new JButton("4");
        add(button4);
        button5 = new JButton("5");
        add(button5);
        button6 = new JButton("6");
        add(button6);
        button7 = new JButton("7");
        add(button7);
        button8 = new JButton("8");
        add(button8);
        button9 = new JButton("9");
        add(button9);
        button0 = new JButton("0");
        add(button0);



        theHandler handler = new theHandler();
        text1.addActionListener(handler);
        button1.addActionListener(handler);
        button2.addActionListener(handler);
        button3.addActionListener(handler);
        button4.addActionListener(handler);
        button5.addActionListener(handler);
        button6.addActionListener(handler);
        button7.addActionListener(handler);
        button8.addActionListener(handler);
        button9.addActionListener(handler);
        button0.addActionListener(handler);

    }
    private class theHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent event)
    {
        String string = "";


        if(event.getSource() == text1)
        {
            string = String.format("Field1: %s", event.getActionCommand());
        }
        else if(event.getSource() == button1)
        {
            string = String.format("Field2: %s", event.getActionCommand());
        }
        else if(event.getSource() == button2)
        {
            string = String.format("Field3: %s", event.getActionCommand());
        }
        else if(event.getSource() == button3)
        {
            string = String.format("Pasword Field: %s", event.getActionCommand());
        }

        JOptionPane.showMessageDialog(null, string);
    }
    }


}

您需要找到一个合适的布局,而FlowLayout可能不是最佳选择

我在GridBayLayout这类事情上运气不错

但我也为我遇到的一项独特任务编写了自己的布局管理器。并不是特别难


事实上,如果您只需要网格中的按钮,那么简单的olgridlayout就可以了。也会更容易。

既然Marvo给出了具体的建议,我将反驳他一点,告诉你在你更熟悉更简单的布局管理器之前,不要使用GridBagLayout。例如,对于JButton的网格,使用GridLayout。如果您想要其他组件,请考虑使用JPAND在另一个使用不同布局的JPAND内部放置GrDeDay布局,比如边框布局——您可以用简单的布局嵌套JPanels来实现复杂的GUI。本教程将再次为您指明方向。

谷歌搜索Java Swing布局管理器教程。这就是你的答案。编辑:第一个点击是bulls eye.oops,似乎OP的帐户删除得太快了。