【发布时间】:2016-05-10 07:44:06
【问题描述】:
我已经找了几个小时类似的问题,但我不知道这个问题。希望有人可以帮助我。
这里是 SimulatedWindow 除了导入之外的类:
public class SimulatedWindow extends JFrame {
private JFrame defFrame = new JFrame();
SimulatedWindow() {
windowsInit();
}
private void windowsInit() {
defFrame.setSize(new Dimension(600, 480));
defFrame.setTitle("Radar Simulate System");
defFrame.setLayout(null);
defFrame.setLocation(100, 100);
DefPanel defPanel = new DefPanel();
// this.getContentPane().add(defPanel, BorderLayout.CENTER);
this.add(new DefPanel());
defFrame.add(defPanel);
defFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
defFrame.setVisible(true);
}
class DefPanel extends JPanel {
private LeftPanel myLeftPanel;
private RightPanel myRightPanel;
public DefPanel() {
setLayout(null);
myLeftPanel = new LeftPanel();
myRightPanel = new RightPanel();
add(new JButton("Hello World!"));
add(myLeftPanel);
add(myRightPanel);
System.out.println("DefPAnel");
}
}
class LeftPanel extends JPanel {
private JButton avgSpeedButton = new JButton();
private JButton trafficColumeButton = new JButton();
private JLabel avgSpeedLabel = new JLabel();
private JLabel trifficColumeLabel = new JLabel();
private JLabel curTimeLabel = new JLabel();
LeftPanel() {
setLayout(null);
this.setBounds(0, 0, this.getSize().width, this.getSize().height);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
curTimeLabel.setText(df.format(new Date()));
add(curTimeLabel);
add(avgSpeedButton);
System.out.println("LeftPanel");
}
}
}
主要功能如下:
public class SimulatedWindowTest {
public static void main(String[] args) {
SimulatedWindow simulatedWindow = new SimulatedWindow();
}
}
JPanel 中的所有元素(JButton、JLabel)均未显示。
【问题讨论】:
-
this.setBounds(0, 0, this.getSize().width, this.getSize().height);将使面板的大小为 0(getSize 返回其自己的大小,即 0)。此外,curTimeLabel没有设置大小。最后,如果您真的不需要绝对定位,则应避免使用null布局。 -
使用LayoutManager Luke。
setLayout(null)是通往黑暗面的道路。 -
初学者只需尝试
setLayout(new FlowLayout()) -
1) Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 2) 以最小尺寸提供 ASCII 艺术或 GUI 的 预期 布局的简单绘图,如果可调整大小,则具有更大的宽度和高度。
-
非常感谢所有上层cmets!布局管理器中存在问题。我忘了我已经将JPanel设置为空布局。最后,我对所有组件都使用了FlowLayout,它运行良好。谢谢寻求建议和帮助!来吧,初学者!
标签: java swing user-interface layout-manager null-layout-manager