我是Swing的新手,我正在尝试创建一个界面,如:
+----------------------------------+
| |
| Information text |
| |
+--------+----------------+--------+
| | JPwdField | |
| +----------------+ |
| Button | Button 7 | Button |
| 1 +----------------+ 3 |
| | JTextField | |
+--------+-------+--------+--------+
| | | | |
| | | | |
| Button | Button| Button | Button |
| 2 | 5 | 6 | 4 |
| | | | |
+--------+----------------+--------+
这个想法是所有的按钮都在他们的“单元格”中展开,所以我读到了嵌套的GridLayout
,但我在思考如何实现它时遇到了问题。有关如何嵌套布局或更好的方法的任何建议吗?
答案 0 :(得分:3)
我会推荐你MigLayout。在我看来,它使用起来非常简单,非常强大。
代码可能如下所示:
public JPanel createPanel() {
JPanel panel = new JPanel(new MigLayout("wrap 4, fill, debug", "align center", "align center"));
panel.add(new JLabel("Information text"), "spanx 4, align center center");
panel.add(new JButton("Button 1"), "spany 3");
panel.add(new JTextField("Password"), "spanx 2");
panel.add(new JButton("Button 3"), "spany 3");
panel.add(new JButton("Button 7"), "spanx 2");
panel.add(new JTextField("JTextField"), "spanx 2");
panel.add(new JButton("Button 2"), "");
panel.add(new JButton("Button 5"), "");
panel.add(new JButton("Button 6"), "");
panel.add(new JButton("Button 4"), "");
return panel;
}
答案 1 :(得分:2)
创建用户界面有严格的规则。您可以使用不同的布局以多种方式实现它。
请查看所有可用的布局,并根据需要选择适合的布局。
您可以使用具有不同布局的多个容器,最后将它们添加到另一个容器中。
阅读How to Use Various Layout Managers
您可以尝试使用GridBagLayout或使用包含水平布局的垂直布局。
答案 2 :(得分:2)
GridBagLayout
将充当最佳解决方案。由于此结果最适合GridBagLayout
,因此只要知道如何解决相同的约束: - )
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutExample {
private GridBagConstraints gbc;
private JButton[] buttons;
public GridBagLayoutExample() {
buttons = new JButton[7];
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
}
private void displayGUI() {
JFrame frame = new JFrame("GridBagLayout Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(new GridBagLayout());
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(Integer.toString(i + 1));
}
JLabel informationLabel = new JLabel("Information Text", JLabel.CENTER);
addComp(contentPane, informationLabel,
0, 0, 1.0, 0.20, 4, 1);
addComp(contentPane, buttons[0],
0, 1, 0.25, 0.60, 1, 3);
JPasswordField passwordField = new JPasswordField(10);
addComp(contentPane, passwordField,
1, 1, 0.50, 0.20, 2, 1);
addComp(contentPane, buttons[6],
1, 2, 0.50, 0.20, 2, 1);
JTextField tField = new JTextField(10);
addComp(contentPane, tField,
1, 3, 0.50, 0.20, 2, 1);
addComp(contentPane, buttons[2],
3, 1, 0.25, 0.60, 1, 3);
addComp(contentPane, buttons[1],
0, 4, 0.25, 0.20, 1, 1);
addComp(contentPane, buttons[4],
1, 4, 0.25, 0.20, 1, 1);
addComp(contentPane, buttons[5],
2, 4, 0.25, 0.20, 1, 1);
addComp(contentPane, buttons[3],
3, 4, 0.25, 0.20, 1, 1);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp,
int x, int y, double wx, double wy,
int gw, int gh) {
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = wx;
gbc.weighty = wy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
panel.add(comp, gbc);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new GridBagLayoutExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
输出: