2011-06-28 69 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

@伊丽莎白:请看编辑回答。 –