我正在开发一个带有以下GUI的小型实用程序:
现在,我有一个使用BorderLayout布局的容器(JPanel),它将一切放在原处。然后,我又有另外两个JPanel分别放置在BorderLayout.NORTH和BorderLayout.SOUTH上,每个面板都采用GridLayout(2列1行)。表格位于主容器的中央。
您认为这是最好的方法吗?我在处理组件之间的间距和框架边框时遇到了麻烦。
现在我有这段代码:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class GUI extends JFrame {
private JButton loadFileBtn = new JButton("Load File");
private JButton generateReportBtn = new JButton("Generate Report");
private JButton exitBtn = new JButton("Exit");
private JLabel fileNameLbl = new JLabel("File Name Here");
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMI = new JMenu("File");
private JMenuItem openFileMenu = new JMenuItem("Open File");
private JSeparator separator = new JSeparator();
private JMenuItem exitMenu = new JMenuItem("Exit");
private JMenu reportMI = new JMenu("Report");
private JMenuItem generateReportMenu = new JMenuItem("Generate Report");
private JMenu helpMI = new JMenu("Help");
private JMenuItem aboutMenu = new JMenuItem("About");
private JTable table = new JTable(5, 2);
private JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
private JPanel panel1 = new JPanel(new BorderLayout());
private JPanel panel2 = new JPanel(new GridLayout(1, 2));
private JPanel panel3 = new JPanel(new GridLayout(1, 2));
public GUI() {
super("Sample GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(300, 300));
setResizable(false);
setLayout(new BorderLayout(10, 10));
fileMI.add(openFileMenu);
fileMI.add(separator);
fileMI.add(exitMenu);
reportMI.add(generateReportMenu);
helpMI.add(aboutMenu);
menuBar.add(fileMI);
menuBar.add(reportMI);
menuBar.add(helpMI);
setJMenuBar(menuBar);
panel1.add(table, BorderLayout.CENTER);
panel2.add(fileNameLbl);
panel2.add(loadFileBtn);
panel3.add(generateReportBtn);
panel3.add(exitBtn);
mainPanel.add(panel2, BorderLayout.NORTH);
mainPanel.add(panel1, BorderLayout.CENTER);
mainPanel.add(panel3, BorderLayout.SOUTH);
add(mainPanel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GUI app = new GUI();
app.setVisible(true);
}
});
}
}
你认为最好的方法是什么?
非常感谢任何帮助。
更新:
现在,我有以下GUI
我希望组件能够平均地远离边框,就像模型中一样。
BorderFactory.createEmptyBorder(int, int, int, int)
。 - Guillaume PoletEmptyBorder
来避免使用setPreferredSize
,还是使用EmptyBorder
来避免在NestedLayout
表单中使用SpringLayout
和GridBagLayout
,还是使用MigLayout
等等。 - mKorbel