摆动,构建和制作GUI,空屏幕

use*_*614 0 java user-interface swing

我有一个秋千计划.首先我创建框架然后到那个框架我插入JPanel,它包含所有图形组件.不知何故,我无法绘制底部面板.我只有空窗.

    @Override
    public void run() {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new buildGUI());
            frame.pack();
            frame.getContentPane();
            frame.setLocationRelativeTo(null);   
            frame.setVisible(true);
            frame.getContentPane().validate();
            frame.getContentPane().repaint();
    }

    .
    .
    .


    public class buildGUI extends JPanel {
        public void buildGUI() {
            bottomPanel = new JPanel(); 
            bottomPanel.setLayout(new GridBagLayout());
            // build gui here, add components to bottomPanel
            frame.add(bottomPanel);
        }

        @Override
        public Dimension getPreferredSize() {
            // this works
            return new Dimension(800, 800);
        }

        @Override
        protected void paintComponent(Graphics g) {
            // here I want to draw my app gui
            super.paintComponent(g);
        }
   }
Run Code Online (Sandbox Code Playgroud)

谢谢

Joh*_*isK 6

public class buildGUI extends JPanel {
        public /*void*/ buildGUI() {
            bottomPanel = new JPanel(); 
            bottomPanel.setLayout(new GridBagLayout());
            // build gui here, add components to bottomPanel
            // frame.add(bottomPanel);
        }
Run Code Online (Sandbox Code Playgroud)

您的buildGUI构造函数不应具有返回类型.

frame.add也没有必要,因为你在实例化时添加它.

可能你需要将bottomPanel添加到"this";

this.add(bottomPanel)
Run Code Online (Sandbox Code Playgroud)

实际上将bottomPanel添加到buildGUI面板(因为你的扩展JPanel,buildGUI也是一个JPanel)或者引用"this"而不是bottomPanel.