单击按钮后,如何将旧JPanel更改为新JPanel?

时间:2014-08-11 12:14:24

标签: java

我试图在点击Jbutton jbtcredits后将父类中的JPanel更改为子类中的JPanel。我尝试在子类中实现cardlayout但是,而不是旧的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);



}

}

1 个答案:

答案 0 :(得分:0)

这会创建一个新的JFrame:

Credits filess=new Credits();

如果使用静态访问框架

旧:

JFrame frame = new JFrame("f");

新:

static JFrame frame = new JFrame("f");

只有一个JFrame:)

PS:用

更新你的画面
frame.invalidate();
frame.repaint();

PSPS:你可以使用动作命令,你不必像(e.getSource()== jbtcredits)那样比较对象。

示例:

 btn.setActionCommand("Show credits");

 ....
 public void actionPerformed(ActionEvent actionEvent){

  if("Show credits".equals(actionEvent.getActionCommand())){

  }
 }