这就是我希望我的JFrame看起来像:
________________________________________________________________________________
|Play - 0 x |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Score List: |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Mario: 100 |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Luigi: 50 |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Waluigi: 20 |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Wario: 10 |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|Status: Ready! Score: 30 |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
目前,每个元素的大小相同,基本上只是将中间和上/下分开。 Play是Frame的标题,%s表示用户可以在其中玩游戏的JPanel。 'Status:Ready'是JLabel,'得分:30'也是如此。分数列表是JTextArea。
那么,我该如何设置这些元素的大小和位置?到目前为止我会给你我的代码,也许你可以告诉我哪里出错了。
this.add(playPanel);
this.add(scoreArea);
this.add(statusLabel);
this.add(scoreLabel);
this.setLayout(new GridLayout(2,2));
答案 0 :(得分:3)
GridBagLayout
应该是您的完美解决方案。它支持服从组件的首选大小和巧妙使用GridBagConstraints
。使用GridBagLayout's GridBagConstraint
属性:
gridx, gridy
:您的组件将被放置在grid(X, Y)
中。在添加任何组件之前,使用这些属性指定网格,将添加到哪个组件。anchor
:FIRST_LINE_START, PAGE_START, FIRST_LINE_END,
LINE_START, LINE_END
等用于定位组件
每个网格的可用空间。weightx, weighty
:对it is the center
标签weightx
和weighty
进行回复,并在width
中重新调整容器大小和height
。对于score list
标签,仅指定weighty
进行响应,仅在height
中重新调整容器大小。gridwidth
:对于it is the center
标签,此属性已指定为2
以获取两个网格。BOTH
,it is the center
标签用于填充可用显示的宽度和高度。并且此属性设置为VERTICAL
score list
标签,以填充高度显示区域。 这是一个专为您提供的演示乍一看:
没有真正编码为标准(,例如setPreferredSize()
设置首选大小不应该),但我认为它符合演示目的。
源代码:
import java.awt.*;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.HeadlessException;
import java.awt.Insets;
import javax.swing.*;
import javax.swing.SwingUtilities;
/**
*
* @author Rashed
*/
class GridBagLayoutDemo extends JFrame{
public JLabel createLabel(String txt, int width, int height, Color color)
{
JLabel label = new JLabel(txt);
label.setOpaque(true);
label.setBackground(color);
label.setPreferredSize(new Dimension(width, height));
return label;
}
public GridBagLayoutDemo() throws HeadlessException {
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new java.awt.GridBagLayout());
GridBagConstraints labCnst = new GridBagConstraints();
labCnst.fill = GridBagConstraints.NONE;
labCnst.insets = new Insets(3, 3, 3, 3);
labCnst.anchor = GridBagConstraints.FIRST_LINE_START;
labCnst.gridx = 0;
labCnst.gridy = 0;
panel.add(createLabel("play", 100, 30, new Color(0x359DBD)), labCnst);
// labCnst.anchor = GridBagConstraints.LAST_LINE_START;
labCnst.gridx = 0;
labCnst.gridy = 2;
panel.add(createLabel("Status: Ready!", 100, 30, new Color(0x359DBD)), labCnst);
labCnst.anchor = GridBagConstraints.FIRST_LINE_END;
labCnst.gridx = 2;
labCnst.gridy = 0;
panel.add(createLabel("-0x", 100, 30, new Color(0x359DBD)), labCnst);
labCnst.anchor = GridBagConstraints.LAST_LINE_END;
labCnst.gridx = 2;
labCnst.gridy = 2;
panel.add(createLabel("score:30", 100, 30, new Color(0x359DBD)), labCnst);
labCnst.anchor = GridBagConstraints.LINE_START;
labCnst.fill = GridBagConstraints.VERTICAL;
labCnst.gridx = 2;
labCnst.gridy = 1;
labCnst.gridwidth = 1;
labCnst.weightx = 0.7;
labCnst.weighty = 0.7;
panel.add(createLabel("ScoreList", 100, 200, new Color(0xFFAA00)), labCnst);
labCnst.gridx = 0;
labCnst.gridy = 1;
//labCnst.anchor = GridBagConstraints.LIN;
labCnst.gridwidth = 2;
labCnst.weightx = 0.8;
labCnst.weighty = 0.8;
labCnst.fill = GridBagConstraints.BOTH;
panel.add(createLabel("It is the center", 200, 200, new Color(0xFFD47E)), labCnst);
//labCnst.anchor = GridBagConstraints.LINE_END;
add(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GridBagLayoutDemo().setVisible(true);
}
});
}
}
答案 1 :(得分:1)
您可能需要查看Gridbaglayout。 它可能更适合您的需求。