我想让JFrame显示如下的GUI:http://i.stack.imgur.com/2POFs.png (我不允许嵌入图片)
空白的spcaces是相同的X和Y维度, 按钮是相同的X和Y维度, JTextArea是帧的一半。
public static JFrame frame = new JFrame ("Launcher " + version);
public static GridBagConstraints c = new GridBagConstraints();
public static JTextArea news = new JTextArea();
public static JButton launchGame = new JButton("Launch Game");
public static JButton settings = new JButton("Settings");
public static JButton exitGame = new JButton("Exit");
public static void main(String[] args)
{
news.setEditable(false);
news.setBounds(0, 0, (screenSize.width - 500) / 2, screenSize.height - 400);
news.append(" NEWS:\n");
news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));
frame.setLayout(new GridBagLayout());
frame.setBounds(300, 200, screenSize.width - 500, screenSize.height - 400);
c.fill = GridBagConstraints.VERTICAL;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
frame.add(news, c);
frame.setVisible(true);
c.fill = GridBagConstraints.EAST;
c.weightx = 0.5;
c.gridx = 1;
c.gridheight = 1;
frame.add(launchGame, c);
c.gridy = 1;
frame.add(settings, c);
c.gridy = 2;
frame.add(exitGame, c);
}
答案 0 :(得分:2)
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 10, 0, 10); // 10 is the marginal
这是完整的代码:
public static void main(String[] args)
{
news.setEditable(false);
news.append(" NEWS:\n");
news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));
frame.setLayout(new GridBagLayout());
c.fill = GridBagConstraints.VERTICAL;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
frame.add(news, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(10, 10, 10, 10); // 10 is the marginal
c.weightx = 0.5;
c.gridx = 1;
c.gridheight = 1;
frame.add(launchGame, c);
c.gridy = 1;
frame.add(settings, c);
c.gridy = 2;
frame.add(exitGame, c);
frame.pack();
frame.setVisible(true);
}
以下是使用BoxLayout
的类似布局(我认为更容易使用):
private static JComponent buildButtons() {
for (JButton button : Arrays.asList(launchGame, settings, exitGame)) {
button.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
}
Box b = Box.createVerticalBox();
b.add(Box.createVerticalStrut(10));
b.add(launchGame);
b.add(Box.createVerticalStrut(10));
b.add(settings);
b.add(Box.createVerticalStrut(10));
b.add(exitGame);
b.add(Box.createVerticalStrut(10));
return b;
}
static JComponent buildUI() {
Box b = Box.createHorizontalBox();
b.add(news);
b.add(Box.createHorizontalStrut(10));
b.add(buildButtons());
b.add(Box.createHorizontalStrut(10));
return b;
}
public static void main(String[] args)
{
news.setEditable(false);
news.append(" NEWS:\n");
news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));
frame.add(buildUI());
frame.pack();
frame.setVisible(true);
}
答案 1 :(得分:2)
首先......
c.fill = GridBagConstraints.EAST;
不是我认为你想做的事。我认为填充应至少为HORIZONTAL
...其次
您可以使用Insets
来调整组件之间的空间,例如
public class TestLayout {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new FormPane());
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected static class FormPane extends JPanel {
private JTextArea news = new JTextArea();
private JButton launchGame = new JButton("Launch Game");
private JButton settings = new JButton("Settings");
private JButton exitGame = new JButton("Exit");
public FormPane() {
setLayout(new GridBagLayout());
news.setEditable(false);
news.append(" NEWS:\n");
news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.weightx = 0.25;
add(news, gbc);
gbc.insets = new Insets(0, 50, 0, 50);
gbc.gridheight = 1;
gbc.gridx++;
add(launchGame, gbc);
gbc.gridy = 1;
add(settings, gbc);
gbc.gridy = 2;
add(exitGame, gbc);
}
}
}
我可能也倾向于将按钮放在自己的容器中(如JPanel
)并可能使用GridLayout
将它们放置出来,然后使用上面的建议来放置文本区域和按钮容器。
这可以隔离各个布局需求,而不会使布局复杂化......