我正在尝试创建一个UI。在U将所有内容移至private并设置getter和setter后,我的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
占据整个框架。