我想向JApplet中添加4個JPanel,並給每個JApplet添加不同的顏色。但沒有顯示顏色 - 我的意思是我看不到輸出。根本沒有顏色。以下代碼在init()
方法中。將JPanels添加到具有空佈局的JApplet
this.setSize(1400, 780);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null) ;
setLayout(null) ;
Panel1 = new JPanel() ;
Panel2 = new JPanel() ;
Panel3 = new JPanel() ;
Panel4 = new JPanel() ;
Label1 = new JLabel ("Label1") ;
Label2 = new JLabel ("Label2") ;
Label3 = new JLabel ("Label3") ;
Label4 = new JLabel ("Label4") ;
Panel1.add(Label1) ;
Panel2.add(Label2) ;
Panel3.add(Label3) ;
Panel4.add(Label4) ;
// Panel 1 "About Me"
Panel1.setSize(140,390) ;
Panel1.setLocation(0,0) ;
Panel1.setBackground(Color.red) ;
Panel1.setVisible(true) ;
this.add(Panel1) ;
// Panel 2 "MyHoppies"
Panel2.setSize(140,390) ;
Panel2.setLocation(0,700) ;
Panel2.setBackground(Color.yellow) ;
this.add(Panel2) ;
// Panel 3 "Photo Gallery"
Panel3.setSize(140,390) ;
Panel3.setLocation(390,0) ;
Panel3.setBackground(Color.black) ;
this.add(Panel3) ;
// Panel 4 "Happey face"
Panel4.setSize(140,390) ;
Panel4.setLocation(390,700) ;
Panel4.setBackground(Color.pink) ;
this.add(Panel4) ;
也許是因爲你一直向Panel1應用'setBackground'(和'setLocation')? – assylias
@assylias考慮發佈這個答案作爲答案 –
@GuillaumePolet顯然是一個錯字。 – assylias