2

这是我第一次涉足 GridBagLayout,已经在线查看了 Java 文档(以及许多 SO Q&A),并且我创建了一个面板,我希望这样显示:

+--------------------------+
|      choose a timer      |
+--------+--------+--------+
|  5min  |  25min | 30min  |
+--------+--------+--------+
|         00:00:00         |
+--------+--------+--------+
|  Start |  Pause |  Quit  |
+--------+--------+--------+

这是我正在使用的 GridBagLayout。

public class ButtonSel extends JFrame implements ActionListener {
    JLabel buttonSelLabel = new JLabel("Choose Which Timer To Run");
    JButton pomoButton    = new JButton("00:25:00");
    JButton shrtButton    = new JButton("00:05:00");
    JButton longButton    = new JButton("00:30:00");
    JButton startButton   = new JButton("Go");
    JButton pauseButton   = new JButton("Pause");
    JButton quitButton    = new JButton("Quit");
    JLabel textDisplay    = new JLabel("00:00:00");
     //
    JPanel timerPanel     = new JPanel();
     //

public ButtonSel() {
    super("ButtonTest");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

            add(buttonSelLabel, c);    // line 1
             c.fill = GridBagConstraints.HORIZONTAL;
             c.gridx = 1;
             c.gridy = 1;
             c.gridwidth = 3;

            add(pomoButton, c);        // line 2
             c.gridx = 1;
             c.gridy = 2;
             c.gridwidth = 1;

            add(shrtButton, c);
             c.gridx = 2;
             c.gridy = 2;
             c.gridwidth = 1;

            add(longButton, c);
             c.gridx = 3;
             c.gridy = 2;
             c.gridwidth = 1;

            add(textDisplay, c);       // line 3
             c.fill = GridBagConstraints.HORIZONTAL;
             c.gridx=1;
             c.gridy=3;
             c.gridwidth=3;

            add(startButton, c);       // line 4
             c.gridx = 1;
             c.gridy = 4;
             c.gridwidth = 1;

            add(pauseButton, c);
             c.gridx = 2;
             c.gridy = 4;
             c.gridwidth = 1;

            add(quitButton, c);
             c.gridx = 2;
             c.gridy = 4;
             c.gridwidth = 1;

    pomoButton.addActionListener(this);
    shrtButton.addActionListener(this);
    longButton.addActionListener(this);
    startButton.addActionListener(this);
    pauseButton.addActionListener(this);
    quitButton.addActionListener(this);
}
public void actionPerformed(ActionEvent radioSelect) {
    Object source = radioSelect.getSource();
    if (source == pomoButton)
        textDisplay.setText("00:25:00");
    else
    if (source == shrtButton)
        textDisplay.setText("00:05:00");
    else
    if (source == longButton)
        textDisplay.setText("00:30:00");
    else
    if (source == startButton)
        textDisplay.setText("Started");
    else
    if (source == pauseButton)
        textDisplay.setText("Paused");
    else
    if (source == quitButton)
        textDisplay.setText("Quit");
    else
        textDisplay.setText("00:00:00");
}
}

我得到这个输出,

在此处输入图像描述

并从这个问题中得到关于水平对齐的提示,我将HORIZONTAL约束添加到字段中。现在我得到:

在此处输入图像描述

有趣的是,我使用(0,0)(1,1)作为我的起始坐标,结果相同。(x,y)

谁能看到我缺少的东西?

4

2 回答 2

4

我不得不添加几行以使您的代码真正可运行。

在将 GridBagConstraints 添加到组件后,您正在设置它们。您必须在添加之前设置它们。

诀窍是将要居中的 JLabels 的 setHorizo​​ntalAlignment 方法设置为居中对齐。

我格式化了您的代码并添加了一些插图,以使您的 GUI 在视觉上更具吸引力。

这是 GUI 的图片。

按钮测试 GUI

这是代码。

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class ButtonSel extends JFrame implements ActionListener {
    JLabel  buttonSelLabel  = new JLabel("Choose Which Timer To Run");
    JButton pomoButton      = new JButton("00:25:00");
    JButton shrtButton      = new JButton("00:05:00");
    JButton longButton      = new JButton("00:30:00");
    JButton startButton     = new JButton("Go");
    JButton pauseButton     = new JButton("Pause");
    JButton quitButton      = new JButton("Quit");
    JLabel  textDisplay     = new JLabel("00:00:00");
    JPanel  timerPanel      = new JPanel();


    public ButtonSel() {
        super("ButtonTest");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        timerPanel = new JPanel();
        timerPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.CENTER;
        c.weightx = 1.0D;
        c.weighty = 1.0D;
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.insets = new Insets(10, 10, 0, 0);
        c.ipadx = 0;
        c.ipady = 0;
        buttonSelLabel.setHorizontalAlignment(SwingConstants.CENTER);
        timerPanel.add(buttonSelLabel, c); // line 1

        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        timerPanel.add(pomoButton, c); // line 2

        c.gridx = 2;
        c.gridy = 2;
        c.gridwidth = 1;
        timerPanel.add(shrtButton, c);

        c.gridx = 3;
        c.gridy = 2;
        c.gridwidth = 1;
        c.insets = new Insets(10, 10, 0, 10);
        timerPanel.add(longButton, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 3;
        c.insets = new Insets(10, 10, 0, 0);
        textDisplay.setHorizontalAlignment(SwingConstants.CENTER);
        timerPanel.add(textDisplay, c); // line 3

        c.gridx = 1;
        c.gridy = 4;
        c.gridwidth = 1;
        c.insets = new Insets(10, 10, 10, 0);
        timerPanel.add(startButton, c); // line 4

        c.gridx = 2;
        c.gridy = 4;
        c.gridwidth = 1;
        timerPanel.add(pauseButton, c);

        c.gridx = 3;
        c.gridy = 4;
        c.gridwidth = 1;
        c.insets = new Insets(10, 10, 10, 10);
        timerPanel.add(quitButton, c);

        pomoButton.addActionListener(this);
        shrtButton.addActionListener(this);
        longButton.addActionListener(this);
        startButton.addActionListener(this);
        pauseButton.addActionListener(this);
        quitButton.addActionListener(this);

        this.add(timerPanel);
        this.pack();
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent radioSelect) {
        Object source = radioSelect.getSource();
        if (source == pomoButton)
            textDisplay.setText("00:25:00");
        else if (source == shrtButton)
            textDisplay.setText("00:05:00");
        else if (source == longButton)
            textDisplay.setText("00:30:00");
        else if (source == startButton)
            textDisplay.setText("Started");
        else if (source == pauseButton)
            textDisplay.setText("Paused");
        else if (source == quitButton)
            textDisplay.setText("Quit");
        else
            textDisplay.setText("00:00:00");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ButtonSel();            
            }
        });
    }

}
于 2013-05-08T16:42:46.800 回答
0

我认为一种解决方案是BoxLayout(). 您可以将您的面板划分为 4 个独立的面板。

第 1 - 选择时间 - 作为一个面板 第 2 - 5, 25,30 分钟 第 3 - 00:00:00 第 4 - 开始 暂停 退出

然后准备添加 4 个面板并使用 BoxLayout() 的主面板/此面板的布局很重要:例如

JPanel p = new JPanel();
p.add(panel1);
p.add(panel2);
p.add(panel3);
p.add(panel4);
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); 

BoxLayout.Y_AXIS - 面板将从上到下布局

于 2013-05-08T15:44:42.287 回答