繁体   English   中英

更改 JFrame 中的内容窗格

[英]Change Content Pane in JFrame

我已经看到了一些这样的例子,并且我尝试过使用以下代码。 我正在尝试在选择 PortraitB 时更改内容窗格,然后运行另一个 class 文件。

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

}

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. 要解决此问题,请不要重新声明此变量。 因此改变这个:

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

话虽如此,我自己可能会使用一个 JPanel,它使用 CardLayout 作为我的容器来交换视图(其他 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);
   }

此外,为了获得良好的编程习惯,您需要避免使用普通的异常 class 而是抛出或捕获特定异常。

接下来,您将要摆脱使用绝对大小和 position 的习惯,而是使用布局管理器来做他们最擅长的事情。

编辑:

回复您最近的评论

我使用公共“JPanel”PortraitGUI 的原因是因为它抛出了所需的错误或返回类型,

这是解决错误的问题,因为更好的解决方案是使其成为真正的构造函数,而不是给它一个返回类型。

我对 class 进行了与 create_Content_Pane() 相同的编码; 返回一个面板。 还出现了几次返回类型所需的错误。

同样,重要的是要知道为什么会发生错误,而不是修复错误的事情。

更新(getGraphics()); 也是我从遇到相同问题的代码示例中尝试的一种方法。

当然,这不是来自 Swing 示例,而是更可能来自较旧的 AWT 示例。 你不用 Swing 做那种编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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