2015-06-09 36 views
1

我正在嘗試創建一個用戶界面。在你把所有東西都搬到了私人空間,然後把我的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; 
    } 

} 
+3

是您最近添加getContentPane方法的更改之一嗎?我問,因爲這覆蓋了JFrame類中的方法。這可能是問題所在。 – BPS

回答

1

您實際上沒有將contenetPane添加到框架。您只需將其設置爲一個變量。正如BPS所評論的那樣,您可能不想覆蓋setContentPane方法。但是,如果你真的想這樣做...添加

add(contentPane); 

之後

setContentPane(contentPane); 

我也可能會建議使用LayoutMager。我建議加入:

setLayout(new GridLayout()); 

因爲這樣會使contentPane佔據整個框架。

+0

我得到這個錯誤 java.lang.IllegalArgumentException:將容器的父項添加到自身 – Styxer

+0

我有一種感覺,它的setContentPane和getContentPane函數搞砸了。我真的建議你刪除這些。你不會失去功能。 – user489041

+0

是的,這就是我所做的,它解決了這個問題 – Styxer

相關問題