【问题标题】:Swing: How to align a JButtonSwing:如何对齐 JButton
【发布时间】:2012-05-16 10:23:36
【问题描述】:

我一直在尝试对齐 JButton,并且我一直在谷歌搜索一些东西,但似乎没有任何效果。我发现的所有代码都不会移动 JButton。我想对齐 JButton,使它们看起来像计算器的脸。这是我的代码:

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);
    }
    }


}

【问题讨论】:

  • Google 获取 Java Swing 布局管理器教程。你的答案就在于此。编辑:第一击是靶心。
  • 哎呀,好像OP的账号被删除太快了。

标签: java swing alignment jbutton


【解决方案1】:

既然 Marvo 给出了具体的建议,我将稍微反驳他,并告诉您在您更熟悉更简单的布局管理器之前避免使用 GridBagLayout。例如,对于 JButton 网格,使用 GridLayout。如果您需要其他组件,请考虑将使用 GridLayout 的 JPanel 放在另一个使用不同布局的 JPanel 中,例如 BorderLayout ——您可以使用简单布局嵌套 JPanel 以实现复杂的 GUI。本教程将再次向您展示方法。

【讨论】:

    【解决方案2】:

    你需要找到一个合适的布局,而 FlowLayout 可能不是要走的路。

    我在 GridBayLayout 上运气不错。

    http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      相关资源
      最近更新 更多