gpt4 book ai didi

java - 在我的 jframe 中看不到任何内容

转载 作者:行者123 更新时间:2023-11-30 08:08:31 25 4
gpt4 key购买 nike

我正在尝试创建一个用户界面。在您将所有内容移至私有(private)并制作 getters 和 setters 后,我的 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 占据整个框架。

关于java - 在我的 jframe 中看不到任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com