更改 JFrame 中的内容窗格
我见过一些这样的例子,并且我尝试过以下代码。我试图在选择 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)
{
}
}
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)
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中存在几个问题,但主要问题之一来自于构造函数中 JFrame 类字段的隐藏,导致类字段为空且不可用。要解决此问题,请不要重新声明此变量。因此,将其更改
为:
然后您可以稍后在交换 contentPane 内容的方法中使用此变量:
话虽如此,我本人可能会使用 JPanel,该 JPanel 使用 CardLayout 作为交换视图的容器(其他J 面板)。
另外,您似乎在这里有一个“伪”构造函数:
Why not just use a real constructor?:
另外,为了良好的编程实践,您需要避免使用普通的 Exception 类,而是抛出或捕获特定的异常。
接下来,您将要摆脱使用绝对大小和位置的习惯,而是使用布局管理器来做他们最擅长的事情。
编辑:
回复您最近的评论
这是修复错误的事情,尽管更好的解决方案是使其成为真正的构造函数,而不是为其提供返回类型。
再次强调,了解错误发生的原因比修复错误更重要。
当然,这不是来自 Swing 示例,而更可能来自较旧的 AWT 示例。您不需要使用 Swing 进行此类编码。
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:
to this:
Then you can use this variable later on in a method where you swap contents of the contentPane:
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:
Why not just use a real constructor?:
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
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.
Again, it's important to know why the error is occurring rather than fixing the wrong thing.
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.