2011-06-28 85 views
0

我見過幾個這樣的例子,我用下面的代碼嘗試過。我試圖在選擇portraitB時更改內容窗格,然後運行其他類文件。更改JFrame中的內容窗格

//imported java libraries 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.UIManager; 
import javax.swing.*; 
import javax.swing.border.*; 
import java.awt.Dimension; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 


public class birthdayCardGUI implements ActionListener 
{ 

//Welcome Screen 
JPanel welcomeP, welcomeImageP, portraitP, landscapeP, backP; 
JLabel welcomeImageL; 
JButton portraitB, landscapeB, backB; 

//Portrait Screen 
JTabbedPane tabbedPane; 
JPanel portraitOne; 
JLabel test; 

public JFrame frame; 

//Colours 
int colourOne = Integer.parseInt("c1c7f9", 16); 
Color Blue = new Color(colourOne); 


public birthdayCardGUI() throws Exception 
{ 
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 

    JFrame frame = new JFrame("birthday Card Maker!"); 
    frame.setExtendedState(frame.NORMAL); 

    frame.getContentPane().add(create_Content_Pane()); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(500, 700); //Size of main window 
    frame.setVisible(true); 

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 

    //sets frame location 
    int fw = frame.getSize().width; 
    int fh = frame.getSize().height; 
    int fx = (dim.width-fw)/2; 
    int fy = (dim.height-fh)/2; 

    //moves the frame 
    frame.setLocation(fx, fy); 
} 

public JPanel create_Content_Pane() throws Exception 
{ 
    JPanel TotalGUI = new JPanel(); 
    //TotalGUI.setBackground(Blue); 
    TotalGUI.setLayout(null); 

    //Welcome Panel 
    welcomeP = new JPanel(); 
    Border etched = BorderFactory.createBevelBorder(10); 
    Border titled = BorderFactory.createTitledBorder(etched, "Welcome"); 
    welcomeP.setBorder(titled); 
    welcomeP.setLayout(null); 
    welcomeP.setLocation(0,0); 
    welcomeP.setSize(485, 680); 
    welcomeP.setBackground(Blue); 
    TotalGUI.add(welcomeP); 

    welcomeImageP = new JPanel(); 
    welcomeImageP.setLayout(null); 
    welcomeImageP.setLocation(88,20); 
    welcomeImageP.setSize(324, 225); 
    welcomeP.add(welcomeImageP); 

    String welcomeG = "Welcome Image.png"; 
    ImageIcon WelcomeG = new ImageIcon(welcomeG); 
    welcomeImageL = new JLabel(WelcomeG, JLabel.CENTER); 
    welcomeImageL.setSize(324, 225); 
    welcomeImageL.setLocation(0,0); 
    welcomeImageP.add(welcomeImageL); 

    portraitB = new JButton("Portrait"); 
    portraitB.setSize(100, 30); 
    portraitB.setLocation(200, 295); 
    portraitB.addActionListener(this); 
    welcomeP.add(portraitB); 

    landscapeB = new JButton("Landscape"); 
    landscapeB.setSize(100, 30); 
    landscapeB.setLocation(200, 335); 
    landscapeB.addActionListener(this); 
    welcomeP.add(landscapeB); 

    TotalGUI.setOpaque(true); 

    return TotalGUI; 

} 


public void create_Portrait_Pane() 
{ 
    PortraitGUI portrait = new PortraitGUI(); 
    getContentPane().removeAll(); 
    getContentPane().add(portrait.PortraitGUI); 
    getContentPane().doLayout(); 
    update(getGraphics()); 
} 

@Override 
public void actionPerformed(ActionEvent e) 
    { 
     if(e.getSource() == portraitB) 
     {    
      create_Portrait_Pane(); 
     } 
    } 

//MAIN METHOD 
public static void main(String[] args) throws Exception 
{ 
    birthdayCardGUI CGUI = new birthdayCardGUI(); 
    } 
} 

這是創建新內容窗格的PortraitGUI文件。

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

public class PortraitGUI extends JPanel implements ActionListener 
{ 
JPanel frontPageP; 
JLabel frontPageL; 

//Color White; 
int intValue = Integer.parseInt("FFFFFF", 16); 
Color White = new Color(intValue); 

public JPanel PortraitGUI() throws Exception 
{ 
    JPanel PortraitGUI = new JPanel(); 
    PortraitGUI.setLayout(null); 

    frontPageP = new JPanel(); 
    frontPageP.setBackground(White); 
    frontPageP.setSize(350, 400); 
    frontPageP.setLocation(20, 70); 
    PortraitGUI.add(frontPageP); 

    frontPageL = new JLabel("Front Page"); 
    frontPageL.setLocation(10, 5); 
    frontPageL.setSize(70, 30); 
    frontPageL.setHorizontalAlignment(JTextField.CENTER); 
    PortraitGUI.add(frontPageL);   

    PortraitGUI.setOpaque(true); 

    return PortraitGUI; 
} 

public void actionPerformed(ActionEvent e) 
{ 
} 

}

+0

ContentPane與JPanel相同,基本上與Borderlayout.CENTER一起佈置,如果您在另一個JPanels之間切換,那麼請不要忘記調用revalidate()+ repaint() – mKorbel

回答

5

有在你的代碼的幾個問題,但是你的一個主要問題來自於你的JFrame類領域的陰影在你的構造離開類字段爲空和不可使用。要解決這個問題,不要重新聲明這個變量。因此,改變這種:

JFrame frame = new JFrame("birthday Card Maker!"); 

這樣:

// this uses the JFrame variable declared in the class. 
frame = new JFrame("birthday Card Maker!"); 

然後你就可以在以後的方法,你掉的contentPane的內容使用這個變量:話雖如此

public void create_Portrait_Pane() throws Exception { 
     PortraitGUI portrait = new PortraitGUI(); 
     frame.getContentPane().removeAll(); // now you can use the frame variable 
     frame.getContentPane().add(portrait); 
     //!! getContentPane().doLayout(); 
     //!! update(getGraphics()); // WTF? 
     ((JPanel)frame.getContentPane()).revalidate(); 
     frame.repaint(); 
    } 

,我自己,我可能會使用一個使用CardLayout作爲容器來交換視圖的JPanel(其他JPanel)。

而且,你似乎有一個「僞」構造這裏:

public JPanel PortraitGUI() throws Exception { 

爲什麼不直接使用一個真正的構造函數?:

public PortraitGUI() throws Exception { 
     setLayout(null); 

     frontPageP = new JPanel(); 
     frontPageP.setBackground(White); 
     frontPageP.setSize(350, 400); 
     frontPageP.setLocation(20, 70); 
     add(frontPageP); 

     frontPageL = new JLabel("Front Page"); 
     frontPageL.setLocation(10, 5); 
     frontPageL.setSize(70, 30); 
     frontPageL.setHorizontalAlignment(JTextField.CENTER); 
     add(frontPageL); 

     setOpaque(true); 
    } 

而且你會希望良好的編程習慣避免使用純粹的Exception類,而是拋出或捕獲特定的異常。

接下來,你將不想使用絕對大小和位置的習慣,而是使用佈局管理器來做他們最擅長的事情。

編輯:

答覆您最近的評論

我使用的公共原因 「的JPanel」 PortraitGUI是因爲它引發錯誤或所需的類型返回,

這是修正錯誤的東西,但更好的解決方案是使它成爲一個真正的構造函數,而不是給它一個返回類型。

我編寫的類與create_Content_Pane()相同;返回一個Panel。此外,返回類型所需的錯誤出現了幾次。

此外,重要的是要知道錯誤發生的原因而不是修復錯誤的東西。

update(getGraphics());也是我從代碼示例中嘗試的一種方法,我找到了同樣的問題。

當然,這不是來自Swing的例子,而更可能是一個較老的AWT例子。你不用Swing做那種編碼。

+0

感謝您對我的不良習慣和回答我的問題。我使用公共「JPanel」PortraitGUI的原因是因爲它引發了所需的錯誤或返回類型,並且我編寫的類與create_Content_Pane()相同;返回一個Panel。此外,返回類型所需的錯誤出現了幾次。更新(getGraphics());也是我從代碼示例中嘗試的一種方法,我找到了同樣的問題。 – Elizabeth

+0

@伊麗莎白:請看編輯回答。 –