如何使用带有条件的jbuttons切换面板

时间:2015-09-25 11:02:40

标签: java swing jframe jpanel switching

我需要帮助actionPerformed(actionEvent e)部分

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NumberGame extends JPanel implements ActionListener 
{
JPanel firstPanel = new JPanel()//firstPanel
{
    public void paintComponent(Graphics g)
        {  
            super.paintComponent(g);

            Image img0 =    Toolkit.getDefaultToolkit().getImage(NumberGame.class.getResource("panel1.jpg"));  
            g.drawImage(img0,0,0,this.getWidth(),this.getHeight(), this);  
        }
};


JPanel secondPanel = new JPanel()//secondPanel
{
    public void paintComponent(Graphics g)
        {  
            super.paintComponent(g);

            Image img = Toolkit.getDefaultToolkit().getImage(NumberGame.class.getResource("panel2.jpg"));  
            g.drawImage(img,0,0,this.getWidth(),this.getHeight(), this);  
        }
};

JPanel thirdPanel = new JPanel()//thirdPanel
{
    public void paintComponent(Graphics g)
    {
            super.paintComponent(g);

            Image img2 = Toolkit.getDefaultToolkit().getImage(NumberGame.class.getResource("panel3.jpg"));  
            g.drawImage(img2,0,0,this.getWidth(),this.getHeight(), this);
    }
};

JPanel fourthPanel = new JPanel()//fourthPanel
{
    public void paintComponent(Graphics g)
    {
            super.paintComponent(g);

            Image img3 = Toolkit.getDefaultToolkit().getImage(NumberGame.class.getResource("panel4.jpg"));
            g.drawImage(img3,0,0,this.getWidth(),this.getHeight(), this);
    }
};

JPanel fifthPanel = new JPanel()//fifthPanel
{
    public void paintComponent(Graphics g)
    {
            super.paintComponent(g);

            Image img4 = Toolkit.getDefaultToolkit().getImage(NumberGame.class.getResource("panel5.jpg"));
            g.drawImage(img4,0,0,this.getWidth(),this.getHeight(), this);
    }
};

JPanel sixthPanel = new JPanel()//sixthPanel
{
    public void paintComponent(Graphics g)
    {
            super.paintComponent(g);

            Image img5 = Toolkit.getDefaultToolkit().getImage(NumberGame.class.getResource("panel6.jpg"));
            g.drawImage(img5,0,0,this.getWidth(),this.getHeight(), this);
    }
};

JPanel sevPanel = new JPanel()//sevPanel
{
    public void paintComponent(Graphics g)
    {
            super.paintComponent(g);

            Image img6 = Toolkit.getDefaultToolkit().getImage(NumberGame.class.getResource("panel7.jpg"));
            g.drawImage(img6,0,0,this.getWidth(),this.getHeight(), this);
    }
};

JPanel eightPanel = new JPanel()//eightPanel
{
    public void paintComponent(Graphics g)
    {
            super.paintComponent(g);

            Image img7 = Toolkit.getDefaultToolkit().getImage(NumberGame.class.getResource("panel8.jpg"));
            g.drawImage(img7,0,0,this.getWidth(),this.getHeight(), this);
    }
};

JPanel ninthPanel = new JPanel()//ninthPanel
{
    public void paintComponent(Graphics g)
    {
            super.paintComponent(g);

            Image img8 = Toolkit.getDefaultToolkit().getImage(NumberGame.class.getResource("panel9.jpg"));
            g.drawImage(img8,0,0,this.getWidth(),this.getHeight(), this);
    }
};

JPanel tenthPanel = new JPanel()//tenthPanel
{
    public void paintComponent(Graphics g)
    {
            super.paintComponent(g);

            Image img9 = Toolkit.getDefaultToolkit().getImage(NumberGame.class.getResource("panel10.jpg"));
            g.drawImage(img9,0,0,this.getWidth(),this.getHeight(), this);
    }
};

public NumberGame() {

    super(new BorderLayout());

    JLabel ans1 = new JLabel(new ImageIcon("ANS1.jpg")); 
    ans1.setIcon(new ImageIcon("ANS1.jpg"));
    JLabel ans2 = new JLabel(new ImageIcon("ANS2.jpg")); 
    ans2.setIcon(new ImageIcon("ANS2.jpg"));
    JLabel ans3 = new JLabel(new ImageIcon("ANS3.jpg")); 
    ans3.setIcon(new ImageIcon("ANS3.jpg"));
    firstPanel.add(ans1);
    firstPanel.add(ans2);
    firstPanel.add(ans3);

    JButton Pan1Ans1 = new JButton("1");
    firstPanel.setLayout(null);
    Pan1Ans1.setBounds(300,480,200,100);
    Pan1Ans1.add(ans1);
    JButton Pan1Ans2 = new JButton ("2");
    Pan1Ans2.setBounds(550,480,200,100);
    Pan1Ans2.add(ans2);
    JButton Pan1Ans3 = new JButton("3");
    Pan1Ans3.setBounds(800,480,200,100);
    Pan1Ans3.add(ans3);//firstPanel

    JLabel ans21 = new JLabel(new ImageIcon("ANS2.1.jpg")); 
    ans21.setIcon(new ImageIcon("ANS2.1.jpg"));
    JLabel ans22 = new JLabel(new ImageIcon("ANS2.2.jpg")); 
    ans22.setIcon(new ImageIcon("ANS2.2.jpg"));
    JLabel ans23 = new JLabel(new ImageIcon("ANS2.3.jpg")); 
    ans23.setIcon(new ImageIcon("ANS2.3.jpg"));
    secondPanel.add(ans21);
    secondPanel.add(ans22);
    secondPanel.add(ans23);

    JButton Pan2Ans1 = new JButton("2");
    secondPanel.setLayout(null);
    Pan2Ans1.setBounds(300,480,200,100);
    Pan2Ans1.add(ans21);
    JButton Pan2Ans2 = new JButton("3");
    Pan2Ans2.add(ans22);
    Pan2Ans2.setBounds(550,480,200,100);
    JButton Pan2Ans3 = new JButton("4");
    Pan2Ans3.setBounds(800,480,200,100);
    Pan2Ans3.add(ans23);//secondPanel

    JLabel ans31 = new JLabel(new ImageIcon("ANS3.1.jpg")); 
    ans31.setIcon(new ImageIcon("ANS3.1.jpg"));
    JLabel ans32 = new JLabel(new ImageIcon("ANS3.2.jpg")); 
    ans32.setIcon(new ImageIcon("ANS3.2.jpg"));
    JLabel ans33 = new JLabel(new ImageIcon("ANS3.3.jpg")); 
    ans33.setIcon(new ImageIcon("ANS3.3.jpg"));
    thirdPanel.add(ans31);
    thirdPanel.add(ans32);
    thirdPanel.add(ans33);

    JButton Pan3Ans1 = new JButton("2");
    thirdPanel.setLayout(null);
    Pan3Ans1.setBounds(300,480,200,100);
    Pan3Ans1.add(ans31);
    JButton Pan3Ans2 = new JButton("4");
    Pan3Ans2.setBounds(550,480,200,100);
    Pan3Ans2.add(ans32);
    JButton Pan3Ans3 = new JButton("3");
    Pan3Ans3.setBounds(800,480,200,100);
    Pan3Ans3.add(ans33);//thirdPanel

    JLabel ans41 = new JLabel(new ImageIcon("ANS4.1.jpg")); 
    ans41.setIcon(new ImageIcon("ANS4.1.jpg"));
    JLabel ans42 = new JLabel(new ImageIcon("ANS4.2.jpg")); 
    ans42.setIcon(new ImageIcon("ANS4.2.jpg"));
    JLabel ans43 = new JLabel(new ImageIcon("ANS4.3.jpg")); 
    ans43.setIcon(new ImageIcon("ANS4.3.jpg"));
    fourthPanel.add(ans41);
    fourthPanel.add(ans42);
    fourthPanel.add(ans43);

    JButton Pan4Ans1 = new JButton("5");
    fourthPanel.setLayout(null);
    Pan4Ans1.setBounds(300,480,200,100);
    Pan4Ans1.add(ans41);
    JButton Pan4Ans2 = new JButton("6");
    Pan4Ans2.setBounds(550,480,200,100);
    Pan4Ans2.add(ans42);
    JButton Pan4Ans3 = new JButton("4");
    Pan4Ans3.add(ans43);
    Pan4Ans3.setBounds(800,480,200,100);//fourthPanel

    JLabel ans51 = new JLabel(new ImageIcon("ANS5.1.jpg")); 
    ans51.setIcon(new ImageIcon("ANS5.1.jpg"));
    JLabel ans52 = new JLabel(new ImageIcon("ANS5.2.jpg")); 
    ans52.setIcon(new ImageIcon("ANS5.2.jpg"));
    JLabel ans53 = new JLabel(new ImageIcon("ANS5.3.jpg")); 
    ans53.setIcon(new ImageIcon("ANS5.3.jpg"));
    fifthPanel.add(ans51);
    fifthPanel.add(ans52);
    fifthPanel.add(ans53);

    JButton Pan5Ans1 = new JButton("7");
    fifthPanel.setLayout(null);
    Pan5Ans1.setBounds(300,480,200,90);
    Pan5Ans1.add(ans51);
    JButton Pan5Ans2 = new JButton("5");
    Pan5Ans2.add(ans52);
    Pan5Ans2.setBounds(550,480,200,90);
    JButton Pan5Ans3 = new JButton("4");
    Pan5Ans3.add(ans53);
    Pan5Ans3.setBounds(800,480,200,90);//fifthPanel

    JLabel ans61 = new JLabel(new ImageIcon("ANS6.1.jpg")); 
    ans61.setIcon(new ImageIcon("ANS6.1.jpg"));
    JLabel ans62 = new JLabel(new ImageIcon("ANS6.2.jpg")); 
    ans62.setIcon(new ImageIcon("ANS6.2.jpg"));
    JLabel ans63 = new JLabel(new ImageIcon("ANS6.3.jpg")); 
    ans63.setIcon(new ImageIcon("ANS6.3.jpg"));
    sixthPanel.add(ans61);
    sixthPanel.add(ans62);
    sixthPanel.add(ans63);

    JButton Pan6Ans1 = new JButton("6");
    sixthPanel.setLayout(null);
    Pan6Ans1.setBounds(300,480,200,100);
    Pan6Ans1.add(ans61);
    JButton Pan6Ans2 = new JButton("5");
    Pan6Ans2.setBounds(550,480,200,100);
    Pan6Ans2.add(ans62);
    JButton Pan6Ans3 = new JButton("4");
    Pan6Ans3.add(ans63);
    Pan6Ans3.setBounds(800,480,200,100);//sixthPanel

    JLabel ans71 = new JLabel(new ImageIcon("ANS7.1.jpg")); 
    ans71.setIcon(new ImageIcon("ANS7.1.jpg"));
    JLabel ans72 = new JLabel(new ImageIcon("ANS7.2.jpg")); 
    ans72.setIcon(new ImageIcon("ANS7.2.jpg"));
    JLabel ans73 = new JLabel(new ImageIcon("ANS7.3.jpg")); 
    ans73.setIcon(new ImageIcon("ANS7.3.jpg"));
    sevPanel.add(ans71);
    sevPanel.add(ans72);
    sevPanel.add(ans73);

    JButton Pan7Ans1 = new JButton("5");
    sevPanel.setLayout(null);
    Pan7Ans1.setBounds(300,480,200,100);
    Pan7Ans1.add(ans71);
    JButton Pan7Ans2 = new JButton("6");
    Pan7Ans2.add(ans72);
    Pan7Ans2.setBounds(550,480,200,100);
    JButton Pan7Ans3 = new JButton("7");
    Pan7Ans3.add(ans73);
    Pan7Ans3.setBounds(800,480,200,100);//sevPanel

    JLabel ans81 = new JLabel(new ImageIcon("ANS8.1.jpg")); 
    ans81.setIcon(new ImageIcon("ANS8.1.jpg"));
    JLabel ans82 = new JLabel(new ImageIcon("ANS8.2.jpg")); 
    ans82.setIcon(new ImageIcon("ANS8.2.jpg"));
    JLabel ans83 = new JLabel(new ImageIcon("ANS8.3.jpg")); 
    ans83.setIcon(new ImageIcon("ANS8.3.jpg"));
    eightPanel.add(ans81);
    eightPanel.add(ans82);
    eightPanel.add(ans83);

    JButton Pan8Ans1 = new JButton("8");
    eightPanel.setLayout(null);
    Pan8Ans1.setBounds(300,480,200,100);
    Pan8Ans1.add(ans81);
    JButton Pan8Ans2 = new JButton("9");
    Pan8Ans2.setBounds(550,480,200,100);
    Pan8Ans2.add(ans82);
    JButton Pan8Ans3 = new JButton("10");
    Pan8Ans3.add(ans83);
    Pan8Ans3.setBounds(800,480,200,100);//eightPanel

    JLabel ans91 = new JLabel(new ImageIcon("ANS9.1.jpg")); 
    ans91.setIcon(new ImageIcon("ANS9.1.jpg"));
    JLabel ans92 = new JLabel(new ImageIcon("ANS9.2.jpg")); 
    ans92.setIcon(new ImageIcon("ANS9.2.jpg"));
    JLabel ans93 = new JLabel(new ImageIcon("ANS9.3.jpg")); 
    ans93.setIcon(new ImageIcon("ANS9.3.jpg"));
    ninthPanel.add(ans91);
    ninthPanel.add(ans92);
    ninthPanel.add(ans93);

    JButton Pan9Ans1= new JButton("7");
    ninthPanel.setLayout(null);
    Pan9Ans1.setBounds(300,480,200,100);
    Pan9Ans1.add(ans91);
    JButton Pan9Ans2 = new JButton("8");
    Pan9Ans2.add(ans92);
    Pan9Ans2.setBounds(550,480,200,100);
    JButton Pan9Ans3 = new JButton("9");
    Pan9Ans3.add(ans93);
    Pan9Ans3.setBounds(800,480,200,100);//ninthPanel

    JLabel ans101 = new JLabel(new ImageIcon("ANS10.1.jpg")); 
    ans101.setIcon(new ImageIcon("ANS10.1.jpg"));
    JLabel ans102 = new JLabel(new ImageIcon("ANS10.2.jpg")); 
    ans102.setIcon(new ImageIcon("ANS10.2.jpg"));
    JLabel ans103 = new JLabel(new ImageIcon("ANS10.3.jpg")); 
    ans103.setIcon(new ImageIcon("ANS10.3.jpg"));
    tenthPanel.add(ans101);
    tenthPanel.add(ans102);
    tenthPanel.add(ans103);

    JButton Pan10Ans1 = new JButton("8");
    tenthPanel.setLayout(null);
    Pan10Ans1.setBounds(300,480,200,100);
    Pan10Ans1.add(ans101);
    JButton Pan10Ans2 = new JButton("9");
    Pan10Ans2.add(ans102);
    Pan10Ans2.setBounds(550,480,200,100);
    JButton Pan10Ans3 = new JButton("10");
    Pan10Ans3.add(ans103);
    Pan10Ans3.setBounds(800,480,200,100);


    firstPanel.add(Pan1Ans1);
    firstPanel.add(Pan1Ans2);
    firstPanel.add(Pan1Ans3);
    Pan1Ans1.addActionListener(this);//Pan1Ans1.setBounds(196,527,250,73);
    Pan1Ans2.addActionListener(this);//Pan1Ans2.setBounds(530,527,250,73);
    Pan1Ans3.addActionListener(this);//Pan1Ans3.setBounds(865,527,250,73);

    secondPanel.add(Pan2Ans1);
    secondPanel.add(Pan2Ans2);
    secondPanel.add(Pan2Ans3);
    Pan2Ans1.addActionListener(this);
    Pan2Ans2.addActionListener(this);
    Pan2Ans3.addActionListener(this);

    thirdPanel.add(Pan3Ans1);
    thirdPanel.add(Pan3Ans2);
    thirdPanel.add(Pan3Ans3);
    Pan3Ans1.addActionListener(this);
    Pan3Ans2.addActionListener(this);
    Pan3Ans3.addActionListener(this);

    fourthPanel.add(Pan4Ans1);
    fourthPanel.add(Pan4Ans2);
    fourthPanel.add(Pan4Ans3);
    Pan4Ans1.addActionListener(this);
    Pan4Ans2.addActionListener(this);
    Pan4Ans3.addActionListener(this);

    fifthPanel.add(Pan5Ans1);
    fifthPanel.add(Pan5Ans2);
    fifthPanel.add(Pan5Ans3);
    Pan5Ans1.addActionListener(this);
    Pan5Ans2.addActionListener(this);
    Pan5Ans3.addActionListener(this);

    sixthPanel.add(Pan6Ans1);
    sixthPanel.add(Pan6Ans2);
    sixthPanel.add(Pan6Ans3);
    Pan6Ans1.addActionListener(this);
    Pan6Ans2.addActionListener(this);
    Pan6Ans3.addActionListener(this);

    sevPanel.add(Pan7Ans1);
    sevPanel.add(Pan7Ans2);
    sevPanel.add(Pan7Ans3);
    Pan7Ans1.addActionListener(this);
    Pan7Ans2.addActionListener(this);
    Pan7Ans3.addActionListener(this);

    eightPanel.add(Pan8Ans1);
    eightPanel.add(Pan8Ans2);
    eightPanel.add(Pan8Ans3);
    Pan8Ans1.addActionListener(this);
    Pan8Ans2.addActionListener(this);
    Pan8Ans3.addActionListener(this);

    ninthPanel.add(Pan9Ans1);
    ninthPanel.add(Pan9Ans2);
    ninthPanel.add(Pan9Ans3);
    Pan9Ans1.addActionListener(this);
    Pan9Ans2.addActionListener(this);
    Pan9Ans3.addActionListener(this);

    tenthPanel.add(Pan10Ans1);
    tenthPanel.add(Pan10Ans2);
    tenthPanel.add(Pan10Ans3);
    Pan10Ans1.addActionListener(this);
    Pan10Ans2.addActionListener(this);
    Pan10Ans3.addActionListener(this);

    add(firstPanel);
    }

当用户点击右键切换到下一个面板时,如何使用JBUTTONS切换面板,如果错误,它应该保留在现有面板中

 public void actionPerformed(ActionEvent e)
 {

 for (Component component : getComponents())  
 {  
        if (firstPanel == component)
        {
                 if (firstPanel == component)
                 {
                    remove(firstPanel);
                    add(secondPanel);
                 } 
                else 
                 {
                    remove(secondPanel);
                    add(firstPanel);
                 }
            }

        else if (secondPanel == component)
        {

                if (secondPanel== component)
                {
                    remove(secondPanel);
                    add(thirdPanel);
                }
                else
                {
                    remove(thirdPanel);
                    add(secondPanel);
                }               
        }

        else if (thirdPanel == component)
        {
                if (thirdPanel == component)
                {
                    remove(thirdPanel);
                    add(fourthPanel);
                }
                else
                {
                    remove(fourthPanel);
                    add(thirdPanel);
                }
        }

        else if (fourthPanel == component)
        {
                if (fourthPanel == component)
                {
                    remove(fourthPanel);
                    add(fifthPanel);
                }
                else
                {
                    remove(fifthPanel);
                    add(fourthPanel);
                }
        }

        else if (fifthPanel == component)
        {

                if (fifthPanel == component)
                {
                    remove(fifthPanel);
                    add(sixthPanel);
                }
                else 
                {
                    remove(sixthPanel);
                    add(fifthPanel);                    
                }
        }
        else if (sixthPanel == component)
        {

                if (sixthPanel == component)
                {
                    remove(sixthPanel);
                    add(sevPanel);
                }
                else
                {
                    remove(sevPanel);
                    add(sixthPanel);
                }
        }

        else if (sevPanel == component)
        {
                if (sevPanel == component)
                {
                    remove(sevPanel);
                    add(eightPanel);
                }
                else
                {
                    remove(eightPanel);
                    add(sevPanel);
                }
        }

        else if (eightPanel == component)
        {
                if (eightPanel == component)
                {
                    remove(eightPanel);
                    add(ninthPanel);
                }
                else 
                {
                    remove(ninthPanel);
                    add(eightPanel);
                }
        }
        else if (ninthPanel == component)
        {
                if (ninthPanel == component)
                {
                    remove(ninthPanel);
                    add(tenthPanel);
                }
                else
                {
                    remove(tenthPanel);
                    add(ninthPanel);
                }
        }

        else  
        {       
        }
   }

    repaint();
    revalidate();

 }
 public static void launch() 
 {

    JFrame frame = new JFrame("PanelSwap");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1300,700);
    frame.setLocationRelativeTo(null);

    JComponent ContentPane = new NumberGame();
    ContentPane.setOpaque(true);
    frame.setContentPane(ContentPane);

    frame.setVisible(true);
   }

   public static void main(String[] args) 
   {

   NumberGame n = new NumberGame();
   n.launch();

   }

   }

0 个答案:

没有答案