通过按钮单击进行面板交换

时间:2016-05-20 00:01:21

标签: java user-interface

最近,我一直在制作类似节目的游戏节目。我的目的是在单击按钮时将一个问题切换到下一个问题(每个问题都在JPanel上)。 这是我的代码:

Frame.java

    public class Frame{
static JFrame f = new JFrame();
static JPanel container = new JPanel();
CardLayout cl = new CardLayout();
static Question1 q1 = new Question1();
static Question2 q2 = new Question2();
static Question3 q3 = new Question3();
static Container0 cont = new Container0();

public static void main(String[] args){
    f.add(cont);
    f.setSize(600,800);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(600,800);
}

}

Question1.java

public class Question1 extends JPanel{
private static final long serialVersionUID = 1L;
final static Question2 q2 = new Question2();
public static String Question = "Why does Grover tell Percy not to retaliate when Nancy Bobofit is throwing peanut butter and ketchup sandwich chunks into Grover’s hair?";
public static String AnswerA = "A : He wanted Percy to save it for a better moment.";
public static String AnswerB = "B : He wanted Percy to keep his good behavior record.";
public static String AnswerC = "C : Percy was already on probation and he didn’t want him in any more trouble.";
public static String AnswerD = "D : Nancy Bobofit had anger issues and Grover didn’t want to disturb her.";
public static JButton AB = new JButton();
public static JButton BB = new JButton();
public static JButton CB = new JButton();
public static JButton DB = new JButton();
CardLayout cl = new CardLayout();
public static boolean drawNext = false;
public Question1(){

    this.setBackground(Color.BLACK);
    this.setSize(600,800);
    this.add(AB);
    this.add(BB);
    this.add(CB);
    this.add(DB);
    AB.setSize(50,25);
    AB.setText("A");
    AB.addActionListener(new ActionListener()
            {   
                @Override
                public void actionPerformed(ActionEvent e) {


                }
            });
    AB.setVisible(true);
    BB.setSize(50,25);
    BB.setText("B");
    BB.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    });
    BB.setVisible(true);
    CB.setSize(50,25);
    CB.setText("C");
    CB.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {

        }

    });
    CB.setVisible(true);
    DB.setSize(50,25);
    DB.setText("D");
    DB.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {


        }});
    DB.setVisible(true);
}
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.GREEN);
    g.drawString(Question, 0, 75);
    g.drawString(AnswerA, 0, 125);
    g.drawString(AnswerB, 0, 200);
    g.drawString(AnswerC, 0, 275);
    g.drawString(AnswerD, 0, 350);
}

Question2.java

public class Question2 extends JPanel{
private static final long serialVersionUID = 1L;
public static boolean isCorrect1;
public static String Question = "What did Mr. Brunner ask Percy about the carving in the museum?";
public static String AnswerA = "A : ”Perhaps you’ll tell us what this picture represents?”";
public static String AnswerB = "B : ”Perhaps you’ll tell me what this photo represents?”";
public static String AnswerC = "C : ”How does this picture relate to the modern day world?”";
public static String AnswerD = "D : ”Could you please jump up and down like a bunny rabbit?”";
public static JButton AB = new JButton();
public static JButton BB = new JButton();
public static JButton CB = new JButton();
public static JButton DB = new JButton();
Question3 q3 = new Question3();
public Question2(){
    this.setBackground(Color.BLACK);
    this.setSize(600,800);
    this.add(AB);
    this.add(BB);
    this.add(CB);
    this.add(DB);

    AB.setSize(50,25);
    AB.setText("A");
    AB.addActionListener(new ActionListener()
            {   
                @Override
                public void actionPerformed(ActionEvent e) {


                }
            });

    BB.setSize(50,25);
    BB.setText("B");
    BB.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {


        }
    });

    CB.setSize(50,25);
    CB.setText("C");
    CB.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {


        }


    });
    DB.setSize(50,25);
    DB.setText("D");
    DB.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {


        }});
    this.setVisible(true);
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.GREEN);
    g.drawString(Question, 0, 75);
    g.drawString(AnswerA, 0, 125);
    g.drawString(AnswerB, 0, 200);
    g.drawString(AnswerC, 0, 275);
    g.drawString(AnswerD, 0, 350);
}



}

Container0.java

public class Container0 extends JPanel{

static CardLayout cl = new CardLayout();
static Question1 q1 = new Question1();
static Question2 q2 = new Question2();
static Question3 q3 = new Question3();
public Container0(){
    this.setLayout(cl);
    this.setSize(600,800);
    this.add(q1,"1");
    this.add(q2,"2");
    this.add(q3,"3");
    cl.show(this, "1");
}
public void show2(){
    cl.show(this,"2");
}
public void show3(){

}

}

0 个答案:

没有答案