我试图在点击Jbutton jbtcredits后从父类中的JPanel更改为子类中的JPanel。我尝试在子类中实现cardlayout但是,JFrame没有更新,旧的JPanel是当我想从子类中显示我的新JPanel时仍然存在。我还按照某人的建议添加了invalidate()和repaint(),但它仍然不起作用。只有JFrame标题发生变化。
我的父类代码如下:
private JButton jbtstart= new JButton("Start Cooking!");
private JButton jbtabout = new JButton("About");
JButton jbtcredits = new JButton("Credits");
private JButton jbtexit = new JButton("Exit");
private JLabel Screen;
static JFrame frame = new JFrame("f");
JPanel p1 = new JPanel();
JPanel p = new JPanel();
JLabel back=new JLabel(new ImageIcon("C:\\Users\\Desktop\\background.png"));
//private static int gridSize = 2;
public GUI_interface()
{
super("GUIinterface");
}
public void createAndDisplayGUI()
{
Screen=new JLabel(new ImageIcon("C:\\Users\\Desktop\\Title.png"));
jbtstart.setLayout(new BorderLayout());
//JPanel p = new JPanel();
//p.setLayout(null);
p1.setLayout(new GridLayout(1,1));
p.setLayout(new BorderLayout());
back.setLayout(new FlowLayout());
jbtstart.setPreferredSize(new Dimension(40, 40));
p.add(Screen,BorderLayout.NORTH);
p.add(jbtstart);
p1.add(jbtabout);
p1.add(jbtcredits);
p1.add(jbtexit);
back.add(p);
back.add(p1);
jbtstart.addActionListener(this);
jbtabout.addActionListener(this);
jbtcredits.addActionListener(this);
jbtexit.addActionListener(this);
setContentPane(back);
frame.setTitle("Cooking App");
frame.setLocation(300,250);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(back);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
// find out which button was pressed
if (e.getSource()==jbtstart)
{
JOptionPane.showMessageDialog(null,"to be added");
}
else if (e.getSource() == jbtabout)
{
About files=new About();
//files.setSize(20, 30);
files.createAndDisplayGUI();
}
else if (e.getSource() == jbtcredits)
{
Credits filess=new Credits();
filess.createAndDisplayGUI();
}
else
{
{ Object[] options = {"Yes","No"};
int n = JOptionPane.showOptionDialog(null,
"Are you sure you would like to quit? ","Cooking App",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]);
if (n==0)
{
System.exit(0);
}
}
}
// end the program
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new GUI_interface().createAndDisplayGUI();
}
});
}
这是我的子课程,我正在实施CardLayout:
private JButton jbtback = new JButton("back");
JPanel a =new JPanel();
JPanel b=new JPanel();
JPanel c=new JPanel();
String text="Victory is a term, originally applied"
+ " to warfare, given to success achieved in personal combat, "
+ "after military operations in general or, by extension, in any"
+ " competition.";
CardLayout c1= new CardLayout();
public void createAndDisplayGUI(){
a.setLayout(c1);
JLabel back1=new JLabel(new ImageIcon("C:\\Users\\Desktop\\background.png"));
JLabel info=new JLabel("<html>"+text+"</html>");;
jbtback.setLayout(new BorderLayout());
//
p1.setLayout(new GridLayout(1,1));
c.add(info,BorderLayout.NORTH);
c.add(jbtback);
back1.add(c);
//
b.add(back1);
a.add(back,"1");
a.add(b,"2");
c1.show(a,"1");
setContentPane(a);
//a.add(back,"1");
//a.add(jbtback,"2");
jbtcredits.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
// find out which button was pressed
c1.show(a,"2");
frame.invalidate();
frame.repaint();
}
});
jbtback.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
// find out which button was pressed
c1.show(a,"1");
frame.invalidate();
frame.repaint();
}
});
frame.add(a);
frame.setTitle("About us");
frame.setSize(300,250);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.pack();
//frame.setLocationByPlatform(true);
frame.setVisible(true);
}