【问题标题】:Quiz Game with multiple choice in java GUIjava GUI中的多项选择测验游戏
【发布时间】:2015-03-13 19:42:28
【问题描述】:

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

这是我的代码:

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);


      }});
      }
    });
  }
}

【问题讨论】:

  • 您可以使用JFrame#get/setContentPane交换内容
  • 究竟如何?我对 GUI 还是很陌生,但需要下周提交这个项目。

标签: java user-interface


【解决方案1】:

这很丑陋,但您应该了解如何在运行时使用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;
    }
}

【讨论】:

  • 对不起,我还是个菜鸟程序员。但是感谢您的代码。我要研究它。这对我有很大帮助。
  • 您将控件添加到单独的 JPanel 包装它们(我称之为 page),然后您将覆盖 JFrames 内容窗格(默认情况下是new JPanel)。来切换控件,这仅仅意味着您正在重新分配内容窗格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多