2013-01-22 97 views
0

我似乎無法正確理解。該代碼沒有任何語法錯誤,但它不顯示我想要顯示的內容。這是我寫的代碼。預先感謝您的幫助。我想在gamepanel(子面板)中顯示圖像jbuttons ..應該有9個jbuttons,但它不顯示。該框架只是灰色。我不知道如何讓子面板顯示。如何顯示插入主面板內子面板的jbutton?

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class midterm extends JFrame 
{ 
    JPanel mainpanel; 
    JButton image1=new JButton(); 
    JButton image2=new JButton(); 
    JButton image3=new JButton(); 
    JButton image4=new JButton(); 
    JButton image5=new JButton(); 
    JButton image6=new JButton(); 
    JButton image7=new JButton(); 
    JButton image8=new JButton(); 
    JButton image9=new JButton(); 


    JLabel text=new JLabel("Please select a photo above."); 


    public midterm() 
    { 
     super("Girls' Generation - I GOT A BOY 4th Korean Album"); 
     setSize(700,700); 
     setLocation(200,30); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
     setResizable(false); 

     panel(); 
    } 

    public static void main(String[] args) 
    { 
     midterm frame=new midterm(); 
    } 

    void panel() 
    { 
     mainpanel=new JPanel(); 
     mainpanel.setLayout(new BorderLayout()); 


     JPanel gamepanel=new JPanel(); 
     gamepanel.setBorder(BorderFactory.createTitledBorder("Photo Puzzle")); 
     gamepanel.setLayout(new GridLayout(3,3)); 


     ImageIcon sica = new ImageIcon("jung.jpg"); 
     image1.setBorder(BorderFactory.createLineBorder(Color.blue)); 
     image1.setIcon(sica); 
     image1.setSize(200,200); 
     image1.setVisible(true); 
     image1.setToolTipText("Jessica Jung Sooyeon"); 
     gamepanel.add((image1)); 

     ImageIcon yuri = new ImageIcon("kwon.jpg"); 
     image2.setIcon(yuri); 
     image2.setSize(200,200); 
     image2.setVisible(true); 
     image2.setToolTipText("Kwon Yuri"); 
     gamepanel.add(image2); 

     ImageIcon tiff = new ImageIcon("hwang.jpg"); 
     image3.setIcon(tiff); 
     image3.setSize(200,200); 
     image3.setVisible(true); 
     image3.setToolTipText("Tiffany Hwang Miyoung"); 
     gamepanel.add(image3); 

     ImageIcon sunny = new ImageIcon("lee.jpg"); 
     image4.setIcon(sunny); 
     image4.setSize(200,200); 
     image4.setVisible(true); 
     image4.setToolTipText("Sunny Lee Sunkyu");  
     gamepanel.add(image4); 

     ImageIcon soo = new ImageIcon("choi.jpg"); 
     image5.setIcon(soo); 
     image5.setSize(200,200); 
     image5.setVisible(true); 
     image5.setToolTipText("Choi Sooyoung"); 
     gamepanel.add(image5); 

     ImageIcon hyo = new ImageIcon("kimh.jpg"); 
     image6.setIcon(hyo); 
     image6.setSize(200,200); 
     image6.setVisible(true); 
     image6.setToolTipText("Kim Hyoyeon"); 
     gamepanel.add(image6); 

     ImageIcon yoona = new ImageIcon("im.jpg"); 
     image7.setIcon(yoona); 
     image7.setSize(200,200); 
     image7.setVisible(true); 
     image7.setToolTipText("Im Yoona");  
     gamepanel.add(image7); 

     ImageIcon taeng = new ImageIcon("kimt.jpg"); 
     image8.setIcon(taeng); 
     image8.setSize(200,200); 
     image8.setVisible(true); 
     image8.setToolTipText("Kim Taeyeon"); 
     gamepanel.add(image8); 

     ImageIcon hyunnie = new ImageIcon("seo.jpg"); 
     image9.setIcon(hyunnie); 
     image9.setSize(200,200); 
     image9.setVisible(true); 
     image9.setToolTipText("Seohyun Seo Juhyun"); 
     gamepanel.add(image9); 



     JPanel controls = new JPanel(); 
     controls.setLayout(new BorderLayout()); 
     controls.setBorder(BorderFactory.createTitledBorder("Control")); 

     JButton newGame = new JButton("New Game"); 
     controls.add(newGame, BorderLayout.NORTH); 


     JButton exitGame = new JButton("Exit"); 
     controls.add(exitGame, BorderLayout.SOUTH); 


     mainpanel.add(gamepanel, BorderLayout.CENTER); 
     mainpanel.add(controls, BorderLayout.EAST); 


     process jj = new process(); 

     image1.addActionListener(jj); 
     image2.addActionListener(jj); 
     image3.addActionListener(jj); 
     image4.addActionListener(jj); 
     image5.addActionListener(jj); 
     image6.addActionListener(jj); 
     image7.addActionListener(jj); 
     image8.addActionListener(jj); 
     image9.addActionListener(jj); 


    } 

    class process implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      if(e.getSource().equals(image1)) 
      { 
       image2.setVisible(false); 
       image3.setVisible(false); 
       image4.setVisible(false); 
       image5.setVisible(false); 
       image6.setVisible(false); 
       image7.setVisible(false); 
       image8.setVisible(false); 
       image9.setVisible(false); 
       text.setVisible(false); 

      } 

      else if(e.getSource().equals(image2)) 
      { 
       image1.setVisible(false); 
       image3.setVisible(false); 
       image4.setVisible(false); 
       image5.setVisible(false); 
       image6.setVisible(false); 
       image7.setVisible(false); 
       image8.setVisible(false); 
       image9.setVisible(false); 
       text.setVisible(false); 

      } 

      else if(e.getSource().equals(image3)) 
      { 
       image2.setVisible(false); 
       image1.setVisible(false); 
       image4.setVisible(false); 
       image5.setVisible(false); 
       image6.setVisible(false); 
       image7.setVisible(false); 
       image8.setVisible(false); 
       image9.setVisible(false); 
       text.setVisible(false); 

      } 

      else if(e.getSource().equals(image4)) 
      { 
       image2.setVisible(false); 
       image3.setVisible(false); 
       image1.setVisible(false); 
       image5.setVisible(false); 
       image6.setVisible(false); 
       image7.setVisible(false); 
       image8.setVisible(false); 
       image9.setVisible(false); 
       text.setVisible(false); 

      } 

      else if(e.getSource().equals(image5)) 
      { 
       image2.setVisible(false); 
       image3.setVisible(false); 
       image4.setVisible(false); 
       image1.setVisible(false); 
       image6.setVisible(false); 
       image7.setVisible(false); 
       image8.setVisible(false); 
       image9.setVisible(false); 
       text.setVisible(false); 

      } 

      else if(e.getSource().equals(image6)) 
      { 
       image2.setVisible(false); 
       image3.setVisible(false); 
       image4.setVisible(false); 
       image5.setVisible(false); 
       image1.setVisible(false); 
       image7.setVisible(false); 
       image8.setVisible(false); 
       image9.setVisible(false); 
       text.setVisible(false); 

      } 

      else if(e.getSource().equals(image7)) 
      { 
       image2.setVisible(false); 
       image3.setVisible(false); 
       image4.setVisible(false); 
       image5.setVisible(false); 
       image6.setVisible(false); 
       image1.setVisible(false); 
       image8.setVisible(false); 
       image9.setVisible(false); 
       text.setVisible(false); 

      } 

      else if(e.getSource().equals(image8)) 
      { 
       image2.setVisible(false); 
       image3.setVisible(false); 
       image4.setVisible(false); 
       image5.setVisible(false); 
       image6.setVisible(false); 
       image7.setVisible(false); 
       image1.setVisible(false); 
       image9.setVisible(false); 
       text.setVisible(false); 

      } 

      else if(e.getSource().equals(image9)) 
      { 
       image2.setVisible(false); 
       image3.setVisible(false); 
       image4.setVisible(false); 
       image5.setVisible(false); 
       image6.setVisible(false); 
       image7.setVisible(false); 
       image8.setVisible(false); 
       image1.setVisible(false); 
       text.setVisible(false); 

      } 

     } 
    } 






} 
+1

也許截圖就已經幫助。 – GGrec

回答

2

主要問題是您沒有將顯示面板添加到JFrame

隨着Swing您需要將其添加到content pane

之後,你想打電話pack()這將調整窗格的大小,並使其中的所有內容「適合」。

打電話setVisible(true)最後沒有必要,但是很好的做法。

我總是發現tutorials有幫助。

public midterm() 
{ 
    super("Girls' Generation - I GOT A BOY 4th Korean Album"); 
    setSize(700,700); 
    setLocation(200,30); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setResizable(false); 

    panel(); 
    this.getContentPane().add(mainpanel);//<-- add panel to content pane 
    this.pack();       //<-- resize everything 
    setVisible(true);     //<-- display 
} 
+0

+1 - 謹慎解釋爲什麼這會起作用;) – MadProgrammer

+0

非常感謝!有效! :)我是一個初學者,謝謝你謝謝..我試圖建立一個圖片拼圖,我仍然在尋找如何做到這一點.. :)無論如何,這是一個很好的幫助..再次,謝謝你 – arnelyn

+0

@arnelyn謹慎接受最能幫助你的答案;) – MadProgrammer

2

您永遠不會將mainpanel添加到您的JFrame。還請確保在之後添加所有組件,然後致電JFrame.setVisible(true)

+0

非常感謝! :)它正在工作.. – arnelyn

0

只需添加的JPanel(的mainPanel)到您的JFrame

panel();  
this.getContentPane().add(mainpanel); 
setVisible(true); 
+0

謝謝〜:)它現在正在工作^ _^ – arnelyn

+1

'show'已被棄用,我將在未來的版本中被刪除,您應該使用'setVisible(true)'而不是... – MadProgrammer

+0

好的,我會改變它 –