我已经看过一些这样的例子,我尝试过以下代码。我正在尝试在选择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)
{
}
}
答案 0 :(得分:5)
您的代码中存在一些问题,但是您的一个主要问题来自您在构造函数中隐藏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();
}
说完这个,我自己,我可能会使用一个JPanel,它使用CardLayout作为我的容器来交换视图(其他JPanels)。
此外,您似乎在这里有一个“伪”构造函数:
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);
}
同样对于良好的编程习惯,您将希望避免使用普通的异常类,而是抛出或捕获特定的异常。
接下来,您将不再想要使用绝对大小和位置,而是使用布局管理器来做他们最擅长的事情。
修改:
回复您最近的评论
我使用公共“JPanel”PortraitGUI的原因是因为它抛出了所需的错误或返回类型,
这是修复错误的东西,因为更好的解决方案是使它成为一个真正的构造函数,而不是给它一个返回类型。
我编写的类与create_Content_Pane()相同;返回一个小组。返回类型所需的错误也出现了几次。
同样,重要的是要知道错误发生的原因而不是纠正错误。
更新(getGraphics());我也从我发现的同样问题的代码示例中尝试了一种方法。
当然,这不是来自Swing示例,但更可能是较旧的AWT示例。你没有用Swing进行那种编码。