我已经看到了一些示例,并尝试使用以下代码。当我选择portraitB时,我试图更改内容窗格,然后运行其他类文件。

//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)
{
}


}

最佳答案

您的代码中有几个问题,但是您的主要问题之一是您对构造函数中JFrame类字段的遮蔽,从而使类字段为null且不可用。要解决此问题,请不要重新声明此变量。因此更改此:

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用作交换视图(其他JPanels)的容器的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()相同;返回面板。同样,返回类型必需的错误出现了几次。


同样,重要的是要知道为什么会发生错误,而不是解决错误的问题。


  update(getGraphics());这也是我从发现相同问题的代码示例中尝试的一种方法。


当然,这不是来自Swing的示例,而是来自旧的AWT的示例。您不会使用Swing进行这种编码。

10-08 15:07