【问题标题】:None of the elements in the JPanel display in JFrame [duplicate]JFrame中没有显示JPanel中的任何元素[重复]
【发布时间】: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 中的所有元素(JButtonJLabel)均未显示。

【问题讨论】:

  • 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


【解决方案1】:

我相信这可能是你的问题:check it

主要答案是:

这是绝对定位(或空布局)的问题。它要求您设置所有组件的大小,否则它们将保持默认的零大小并且不会出现。这就是为什么使用layout manager 总是更好的原因。

【讨论】:

  • “我相信这可能是你的问题:检查一下” - 所以,你没有投票结束这个问题,而是决定代表别人的一些奖励回答?
猜你喜欢
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 2011-07-18
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多