如何使用GridBagLayout修改此视图

时间:2014-09-17 14:47:41

标签: swing jbutton jlabel gridbaglayout jspinner

如果您能帮助我修改此界面,我将不胜感激。我刚开始学习GridBagLayout,我想弄清楚如何使用GridBagLayout()实现绘制的界面

|-----------------------------------------------|
| Playing File: name of the file                | //This JLabel should cover the entire space
|-----------------------------------------------| 
| 00:00:00 ----#---------------------- 00:00:00 | //This has on the left and on the right side two labels and in the middle a JSpinner
|-----------------------------------------------|
|  [Open]  [Play]  [Pause]  [Rewind]  [Save]    | //This line contains 5 buttons
|-----------------------------------------------|

请在附件中找到一个工作示例

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;

public class Sample extends JPanel
{

    private JLabel labelFileName = new JLabel("Playing File:");
    private JLabel labelTimeCounter = new JLabel("00:00:00");
    private JLabel labelDuration = new JLabel("00:00:00");

    private JButton buttonOpen   = new JButton("  Open  ");
    private JButton buttonPlay   = new JButton("  Play  ");
    private JButton buttonPause  = new JButton("  Pause ");
    private JButton buttonRewind = new JButton(" Rewind ");
    private JButton buttonSave   = new JButton("  Save  ");

    private JSlider sliderTime = new JSlider();

    GridBagConstraints constraints = new GridBagConstraints();

    public Sample() 
    {

        setLayout(new GridBagLayout());

        constraints.insets = new Insets(5, 5, 5, 5);

        sliderTime.setValue(0);

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 5; 
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(labelFileName, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 1; 
        add(labelTimeCounter, constraints);

        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.gridwidth = 3; 
        add(sliderTime, constraints);

        constraints.gridx = 4;
        constraints.gridy = 1;
        constraints.gridwidth = 1; 
        add(labelDuration, constraints);

        JPanel panelButtons = new JPanel(new FlowLayout(FlowLayout.LEFT, 25, 5));
        panelButtons.add(buttonOpen);
        panelButtons.add(buttonPlay);
        panelButtons.add(buttonPause);
        panelButtons.add(buttonRewind);
        panelButtons.add(buttonSave);

        constraints.gridx = 0;
        constraints.gridy = 2;
        add(panelButtons, constraints);

    }

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

            }
        });
    }

    public static void runApp() 
    {
        JFrame frame = new JFrame("Frame");
        frame.setVisible(true);
        frame.setSize(800, 200);
        frame.add( new Sample());
        frame.setDefaultCloseOperation
    }

}

由于

1 个答案:

答案 0 :(得分:2)

我强烈建议使用三行的一列网格。

  • 顶行可以是设置为 flowlayout 的面板,也可以直接删除标签。您可以自行设置标签的大小。布局管理人员无法为您做到这一点。
  • 中间一行是 borderlayout ,其中微调器设置为居中,两个标签位于东西方。
  • 底行应为具有 flowlayout 的面板集。要使图表中的按钮间隔很好,只能使用flowlayout来完成。

我唯一担心的是滑块面板可以在垂直方向上调整大小。在这种情况下,您可能想要抛弃GridBaglayout,并且只需在顶部中心位于顶部的borderlayout,并将底部的两行打包到南面的面板中。