【发布时间】:2014-10-04 06:25:17
【问题描述】:
单击 Jbutton jbtcredits 后,我试图将父类中的 JPanel 更改为子类中的 JPanel。我尝试在子类中实现卡片布局,但是,不是旧的 JFrame 将旧的 JPanel 更改为新的,一个新的 JFrame 和新的 JPanel 一起弹出,旧的 JFrame 和旧的 JPanel 保持不变。这是父类:
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;
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);
}
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new GUI_interface().createAndDisplayGUI();
}
});
}
这是带有新 JPanel 的子类:
private JButton jbtback = new JButton("back");
JPanel a =new JPanel();
JPanel b=new JPanel();
public void createAndDisplayGUI(){
CardLayout c1= new CardLayout();
a.setLayout(c1);
JLabel back1=new JLabel(new ImageIcon("C:\\Users\\Desktop\\background.png"));
b.add(back1);
b.add(jbtback);
a.add(back,"1");
a.add(b,"2");
c1.show(a,"2");
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");
}
});
jbtback.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
// find out which button was pressed
c1.show(a,"1");
}
});
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);
}
}
【问题讨论】:
-
我应该在哪里添加?
-
您好,我已经尝试在任何地方添加它,但程序没有任何变化。
标签: java