Java Gui计划

时间:2014-04-09 19:52:15

标签: java swing layout

这是第一次做一个gui,一个数学测试。我想要一个代码,当学生完成测试时,测试再次显示给下一个学生回答练习。我试着循环;它刚刚生产了2个gui。但是,我需要按顺序显示它们。我已经阅读了有关gui的Oracle文档,但我找不到答案。这个程序只有一个练习,真正的练习有10个。我做了简单易读。如果GridLayout的参数看起来很奇怪,对我来说现在并不重要。谢谢。

import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt. GridBagLayout;
import java.awt.FlowLayout;
import java.awt.Container;
import java.util.Scanner;

public class MathTest extends JFrame {

    JLabel m,error;
    JTextArea exone;
    JLabel y;
    public JTextField stu;
    GridBagLayout  g;
    FlowLayout  fL;
    Container  c;

    public MathTest() {

        super ("Math Test");

        fL=new FlowLayout();
        c = getContentPane();

        setLayout (fL);

        setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        g.fill = GridBagConstraints.VERTICAL;

        m = new JLabel("Math Test  10 exercises");

        g.gridx = 0;
        g.gridy = 0;
        add(m, g);

        exone=new JTextArea ( "1. How much is  890 times 500?.");
        g.gridx= 2;
        g.gridy= 0;
        add(exone,g);

        y=new JLabel("Your answer, please.");
        g.gridx =3;
        g.gridy= 0;
        add(y,g);
        stu=new JTextField (4);
        g.gridx=3;
        g.gridy=1;
        add(stu,g);

        Utest manage =new Utest ();
        stu.addActionListener (manage);

    }//end test

    public class Utest implements ActionListener
    {
        public void actionPerformed (ActionEvent event)

        {
            try
            {
                String text = stu.getText();
                Double num=Double.parseDouble(text);

                if (num==445000)
                {
                    JOptionPane.showMessageDialog (null, "   Good start",
                    "Exercise 1 Result ", JOptionPane.PLAIN_MESSAGE );
                }//end of if

                else
                {
                    JOptionPane.showMessageDialog (null, "    Wroooooong! The answer is 2 ",
                    "Exercise 1 Result  ", JOptionPane.PLAIN_MESSAGE );

                }// end of else
            }//end of try

            catch (Throwable t)
            {
                JOptionPane.showMessageDialog (null, "You must type numbers.",
                "You should follow instructions.  ", JOptionPane.WARNING_MESSAGE )
            }
        } //end action
    }//end utest
}//end MathTest

以下是代码的其余部分:

import javax.swing.JFrame;

public class University
{
    public static void main (String [] args)  //main method
    {
        MathTest mt= new MathTest(); //Create MathTest object

        mt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // Put in JFrame
        mt.setSize(500, 500);    // Size JFrame
        mt.setVisible(true);     // Display

    }//end main
}// end Uni

0 个答案:

没有答案