我已经找了几个小时类似的问题,但我对这个问题一无所知。希望有人能帮我。
以下是除了导入之外的类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
)均未显示。
我认为这可能是你的问题:检查
主要答案是:
这就是绝对定位(或空布局)的问题。它要求您设置所有组件的大小,否则它们将保持为默认的零大小,不会出现。这就是为什么使用布局管理器总是更好的原因。