测验游戏在java GUI中有多种选择

时间:2015-03-13 09:55:35

标签: java user-interface

我的项目是关于测验游戏,但我不知道如何在不创建另一个框架的情况下转到另一个屏幕。有人可以教我或告诉我如何做到这一点吗?

这是我的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Project {

  public static void main(String[] args) {
     JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Who wants to be a Millionaire!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    frame.setLayout(null);
    frame.setVisible(true);

    ImageIcon image = new ImageIcon("image1.png");
    JButton b1=new JButton(image);
    frame.add(b1);

    ImageIcon bg = new ImageIcon("image2.png");
    JLabel b2=new JLabel(bg);
    frame.add(b2);

    frame.setSize(1280, 800);
    b1.setBounds(400,650,414,60);
    b2.setBounds(1,1,1280,800);

    b1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {   

         frame.dispose();
         JFrame popup = new JFrame("Who wants to be a Millionaire!");
         ImageIcon q1 = new ImageIcon("question1.png");
         ImageIcon c1 = new ImageIcon("choice1.png");
         ImageIcon c2 = new ImageIcon("choice2.png");
         popup.setLayout(null);
         popup.setVisible(true);
         popup.setSize(1280, 800);

         JButton qC1=new JButton(c1);
         popup.add(qC1);
         JButton qC2=new JButton(c2);
         popup.add(qC2);
         JLabel qL1=new JLabel(q1);
         popup.add(qL1);



         qL1.setBounds(1,1,1280,800);
         qC1.setBounds(80,580,526,82);
         qC2.setBounds(650,580,526,82);

         qC1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {  
        JFrame correct = new JFrame("Who wants to be a Millionaire!");
        ImageIcon correct1 = new ImageIcon("correct.png");
        correct.setLayout(null);
        correct.setVisible(true);
        correct.setSize(420, 230);
        JLabel correct2=new JLabel(correct1);
        correct.add(correct2);
        correct2.setBounds(1,1,420,230);


      }});
      }
    });
  }
}

1 个答案:

答案 0 :(得分:0)

这非常难看,但您应该了解如何使用JFrame#get/setContentPane在运行时切换显示的控件:

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public final class MainFrame extends JFrame {
    public static MainFrame instance;
    public static final Container startPage = createStartPage();
    public static final Container question1Page = createQuestion1Page();
    public static final Container correctPage = createCorrectPage();

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        instance = new MainFrame();
        instance.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        instance.setTitle("Who wants to be a Millionaire!");
        instance.setVisible(true);
        navigateTo(startPage);
    }

    static void navigateTo(Container page) {
        instance.setContentPane(page);
        instance.setSize(page.getSize());
    }

    static Container createStartPage() {
        JPanel result = new JPanel(null);
        result.setSize(1280, 800);

        //b1
        ImageIcon image = new ImageIcon("image1.png");
        JButton b1 = new JButton(image);
        b1.setBounds(400, 650, 414, 60);
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                navigateTo(question1Page);
            }
        });
        result.add(b1);

        //b2
        ImageIcon bg = new ImageIcon("image2.png");
        JLabel b2 = new JLabel(bg);
        b2.setBounds(1, 1, 1280, 800);
        result.add(b2);
        return result;
    }

    static Container createQuestion1Page() {
        JPanel result = new JPanel(null);
        result.setSize(1280, 800);

        //qC1
        ImageIcon c1 = new ImageIcon("choice1.png");
        JButton qC1 = new JButton(c1);
        qC1.setBounds(80, 580, 526, 82);
        qC1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                navigateTo(correctPage);
            }
        });
        result.add(qC1);

        //qC2
        ImageIcon c2 = new ImageIcon("choice2.png");
        JButton qC2 = new JButton(c2);
        qC2.setBounds(650, 580, 526, 82);
        result.add(qC2);

        //qL1
        ImageIcon q1 = new ImageIcon("question1.png");
        JLabel qL1 = new JLabel(q1);
        qL1.setBounds(1, 1, 1280, 800);
        result.add(qL1);
        return result;
    }

    static Container createCorrectPage() {
        JPanel result = new JPanel(null);
        result.setSize(420, 230);

        //correct2
        ImageIcon correct1 = new ImageIcon("correct.png");
        JLabel correct2 = new JLabel(correct1);
        correct2.setBounds(1, 1, 420, 230);
        result.add(correct2);
        return result;
    }
}