简体   繁体   中英

Change Content Pane in JFrame

I've seen a few examples of this, and I've tried with the following code. I'm trying to change the content pane when portraitB is selected and then run the other class file.

//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();
    }
}

And this is the PortraitGUI file that creates the new content pane.

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

}

There are several problems in your code, but one of your main problems comes from your shadowing of the JFrame class field in your constructor leaving the class field null and non-usable. To fix this, don't redeclare this variable. Thus change this:

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

to this:

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

Then you can use this variable later on in a method where you swap contents of the 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();
   }

Having said this, myself, I'd probably use a JPanel that uses CardLayout as my Container for swapping views (other JPanels).

Also, you appear to have a "pseudo" constructor here:

public JPanel PortraitGUI() throws Exception {

Why not just use a real constructor?:

   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);
   }

Also for good programming practice you'll want to avoid using a plain-vanilla Exception class and instead throw or catch specific exceptions.

Next, you're going to want to get out of the habit of using absolute size and position and instead using the layout managers for doing what they do best.

Edit:

replies to your recent comments

The reason I used public "JPanel" PortraitGUI is because it was throwing the error or return type required,

This was fixing the wrong thing though as the better solution was to make it a true constructor, not to give it a return type.

and I coded the class the same as create_Content_Pane(); with returning a Panel. Also the return type required error came up a few times.

Again, it's important to know why the error is occurring rather than fixing the wrong thing.

The update(getGraphics()); was also a method I tried from code examples I found with the same problem.

Surely that didn't come from a Swing example but more likely an older AWT example. You don't do that sort of coding with Swing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM