【发布时间】:2017-10-25 19:43:06
【问题描述】:
当我遇到一个奇怪的问题时,我只是在拼凑一个快速而肮脏的 GUI 来显示一些数据。我添加到JFrame 的最后一个标签不想定位或显示我放在它上面的边框,所以它看起来像这样:
这是我的代码:
public DisplayData (Connection tConn)
{
ID = tID;
conn = tConn;
setupObjects();
setupFrame();
}
private void setupObjects()
{
JLabel caseLabel = new JLabel ("Case #:");
JLabel dateLabel = new JLabel ("Date:");
JLabel reportLabel = new JLabel ("Report:");
JLabel offenceLabel = new JLabel ("Offence:");
JLabel descriptionLabel = new JLabel ("Description:");
this.add(caseLabel);
this.add(dateLabel);
this.add(reportLabel);
this.add(offenceLabel);
this.add(descriptionLabel);
caseLabel.setBounds(50, 50, 130, 25); //x, y, width, height
dateLabel.setBounds(50, 100, 130, 25);
reportLabel.setBounds(50, 150, 130, 25);
offenceLabel.setBounds(50, 200, 130, 25);
descriptionLabel.setBounds(100, 50, 130, 25);
caseLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
dateLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
reportLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
offenceLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
descriptionLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
private void setupFrame()
{
this.setTitle("Data Display");
this.setSize (650, 700); //Width, Height
this.setLocation(300, 10);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
}
是的,我知道我应该使用适当的布局管理器,但就像我说的那样,我只是想要一些快速而肮脏的东西。另外,我不会被这么简单的事情打败。任何想法将不胜感激。
编辑: 正如 Compass 和 Neophyte 指出的那样,我的操作顺序已经关闭。翻转我的方法调用,世界上一切都好起来了。感谢第二双眼睛。
【问题讨论】:
-
订单很重要。在设置框架大小之前不要设置项目的边界。通过先运行 setupFrame 然后运行 setupObjects,可以按预期工作。
-
两个词 - 布局管理器。当然手动完成它“快速”和“肮脏”,但它需要更长的时间,增加“意外”问题的数量,并且通常只是完全浪费时间。更多地关注信息/交互的流动,然后是“像素完美”布局,不同的平台/PC 将把“像素完美”布局弄得一团糟。简而言之,
null布局永远不是答案,即使您认为它们是,它们也不是
标签: java swing jlabel layout-manager null-layout-manager