我正在尝试创建一个用户界面。在U将所有内容移至私有并制作了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;
}
}
答案 0 :(得分:1)
您实际上并未将contenetPane
添加到框架中。您只需将其设置为变量即可。正如BPS评论的那样,您可能不想覆盖setContentPane
方法。但是,如果你真的想这样做......添加
add(contentPane);
之后
setContentPane(contentPane);
我可能还建议使用LayoutMager
。我建议添加:
setLayout(new GridLayout());
因为这将使contentPane
占据整个框架。