如果您能帮助我修改此界面,我将不胜感激。我刚开始学习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
}
}
由于
答案 0 :(得分:2)
我强烈建议使用三行的一列网格。
我唯一担心的是滑块面板可以在垂直方向上调整大小。在这种情况下,您可能想要抛弃GridBaglayout,并且只需在顶部中心位于顶部的borderlayout,并将底部的两行打包到南面的面板中。