2014-05-09 25 views
0

我試圖創建一個帶有背景圖片的JFrame,一個JLabel放在背景圖片的中心和底部,左邊和右邊有兩個按鈕,分別表示「Stay」和「Leave」。這已經創建。這個問題出現在每個項目的順序上。我無法將帶有文本和按鈕的JLabel放在背景圖片上,並且兩者都顯示出來。這是我的代碼;任何意見,將不勝感激。先謝謝你。如何將按鈕和JPanel添加到ZOrderComponent?

public class SceneOne { 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     JFrame SceneOne = new JFrame(); 

     ImageIcon image = new ImageIcon(
       "C:/Users/alan/Downloads/scary_forest_2.jpg"); 
     JLabel imageLabel = new JLabel("", image, JLabel.CENTER); 
     JPanel panel = new JPanel(new BorderLayout()); 
     panel.add(imageLabel, BorderLayout.CENTER); 
     SceneOne.add(panel); 
     SceneOne.setResizable(true); 
     imageLabel.setVisible(true); 
     SceneOne.pack(); 

     JButton Leave=new JButton("Leave"); 
     JButton Stay= new JButton ("Stay"); 
     JPanel Leave1= new JPanel(new FlowLayout()); 
     JPanel Stay1=new JPanel(new FlowLayout()); 
     FlowLayout two = new FlowLayout(FlowLayout.LEFT); 
     FlowLayout three = new FlowLayout(FlowLayout.RIGHT); 
     Leave1.setLayout(two); 
     Stay1.setLayout(three); 
     Stay1.add(Leave); 
     Leave1.add(Stay); 
     Leave1.setOpaque(false); 
     Stay1.setOpaque(false); 
     SceneOne.add(Leave1); 
     SceneOne.add(Stay1); 


     JLabel label1 = new JLabel("Test"); 
     SceneOne.add(label1); 

     label1.setText("<html><font color='red'> It was approximately 11:30 pm. The night sky was black not a single star piercing through the darkness" 
       + "except the thick and powerful moonlight." 
       + "<br>" 
       + "You are alone leaving a costume party at a friend's place." 
       + "It was rather boring and you decided to leave early." 
       + "A stutter is heard and your" 
       + "<br>" 
       + "car begins to shake" 
       + "Your headlights and car lights crack. The engine is left dead silent." 
       + "You are left in a total silence" 
       + "and baked in merely the moonlight." 
       + "<br>" 
       + "There is a mere second of silence till a harsh chill ripes through the" 
       + "car like a bullet through paper. You are left dumbfounded. What do you do?</font><html>"); 
     label1.setHorizontalAlignment(JLabel.CENTER); 
     label1.setVerticalAlignment(JLabel.BOTTOM); 
     label1.setVisible(true); 
     label1.setOpaque(false); 



     SceneOne.setComponentZOrder(panel, 0); 
     SceneOne.setComponentZOrder(label1, 0); 
    // SceneOne.setComponentZOrder(Leave1,0); 
     // SceneOne.setComponentZOrder(Stay1,0); 



     SceneOne.setSize(400,320); 
     SceneOne.setTitle("The Car"); 
     SceneOne.setVisible(true); 
     SceneOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     SceneOne.setLocation(500, 300); 
    } 

} 

回答

0

首先遵循Java命名約定。變量名稱不應以大寫字符開頭。我從來沒有見過使用變量名稱的教程,教科書或論壇示例。不要組成你自己的約定!

JFrame的默認佈局管理器是BorderLayout。當您使用frame.add(...)方法時,組件默認添加到BorderLayout的CENTER。只有在組件上纔可以添加到CENTER,因此只顯示最後一個組件。

如果您希望組件顯示在圖像頂部,則需要將組件添加到圖像。基本的代碼是:

JLabel label = new JLabel(new ImageIcon(...)); 
frame.add(label); 

label.setLayout(....); 
label.add(leaveButton); 
label.add(labelWithText); 
label.add(stayButton); 

我會讓你找出你想要的確切佈局。

+0

1.對不起,截至目前的整理和理解一直是我的首要任務。我剛開始學習並實現適當的命名約定。感謝你的例子,我會加註這個,但我還沒有獲得必要的聲譽。 – user3092008