我正在尝试创建一个UI。在U将所有内容都移至私有状态并设置了获取方法和设置方法后,我的JFrame显示为空白。出于某种原因,我只看到基本的灰色窗口,但在此之前一切正常。

public class Aboutus_GUI extends JFrame {

    private JPanel contentPane;
    private JLabel join;
    private JLabel Names;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Aboutus_GUI frame = new Aboutus_GUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws MalformedURLException
     */
    public Aboutus_GUI() throws MalformedURLException {
        setBackground(Color.WHITE);
        setTitle("About Us");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 727, 651);
        JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);


        join = new JLabel();
        join.setIcon(new ImageIcon("src/GUI_final/about_us.jpg"));
        join.setBounds(0, -267, 701, 770);
        contentPane.add(join);

        Names = new JLabel();
        Names.setIcon(new ImageIcon("src/GUI_final/names.jpg"));
        Names.setLocation(10, 276);
        Names.setSize(887, 492);
        contentPane.add(Names);
    }

    public JPanel getContentPane() {
        return contentPane;
    }

    public void setContentPane(JPanel contentPane) {
        this.contentPane = contentPane;
    }

    public JLabel getJoin() {
        return join;
    }

    public void setJoin(JLabel join) {
        this.join = join;
    }

    public JLabel getNames() {
        return Names;
    }

    public void setNames(JLabel names) {
        Names = names;
    }

}

最佳答案

您实际上从未将contenetPane添加到框架中。您只需将其设置为变量。正如BPS所述,您可能不想覆盖setContentPane方法。但是,如果您确实想这样做...添加

add(contentPane);


之后

setContentPane(contentPane);


我可能还会建议使用LayoutMager。我建议添加:

setLayout(new GridLayout());


因为这将使contentPane占据整个帧。

09-28 10:20