我正在使用JOptionPane获取用户输入并显示消息。单击“确定”时,是否有一种方法可以使输入和消息窗口都关闭?

时间:2016-03-09 21:57:52

标签: java swing joptionpane

当程序运行时,它会显示一个窗口,里面填满各种颜色的微小(5x5)矩形,红色和绿色RGB值对应于屏幕上矩形的位置。蓝色RGB值是随机的,但适用于所有矩形。我要求用户输入一个输入对话框来猜测随机值,然后我用一个消息对话框告诉他们离他们有多远。这是该计划:

import java.util.*;
import java.util.concurrent.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.event.*;
import java.applet.*;
import java.text.*;
import javax.swing.*;

public class TwoColorSpectrum extends Applet {  
public void paint(Graphics squares) {   
    squares.setColor(Color.white);
    squares.fillRect(0, 0, 1280, 750);

    Random rectStr = new Random();
    int randomRed,
        randomGreen,
        randomBlue,
        randomX,
        randomY,
        rectNum;
    do {
        rectNum = rectStr.nextInt(9999999);
    } while(rectNum < 5000000);

    Random color3 = new Random();
            randomBlue = color3.nextInt(255);

    for(int h = 1; h <= rectNum; h++) {
        Random xPosition = new Random(); 
            randomX = xPosition.nextInt(1275);
            double pcx = randomX / 1275.0;
            double dRandomR = pcx * 255.0;
            randomRed = (int)(dRandomR);

        Random yPosition = new Random();
            randomY = yPosition.nextInt(745);
            double pcy = randomY / 745.0;
            double dRandomG = pcy * 255.0;
            randomGreen = (int)(dRandomG);

        Color randomColor = new Color(randomRed, randomGreen, randomBlue);
        squares.setColor(randomColor);
        squares.fillRect(randomX, randomY, 4, 4);
    }

    //asking the user what blue value it is
    JPanel panel = new JPanel();
    JLabel label = new JLabel("Enter a value from 0 to 255.");
    JTextField field = new JTextField(10);
    JButton button = new JButton("Am I right?");
    final int WIDTH = 350;
    final int HEIGHT = 100;

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String str;
            int num;

            str = field.getText();
            num = Integer.parseInt(str);
            int absOff = Math.abs((randomBlue - num));

            if(absOff != 0) {
                JOptionPane.showMessageDialog(null, "You were " + absOff
                                              + " off! The blue value is "
                                              + randomBlue + ".", "How much "
                                              + "were you off?", 
                                              JOptionPane.PLAIN_MESSAGE);
            } 
            else {
                JOptionPane.showMessageDialog(null, "That's right! The blue "
                                              + "value is " + randomBlue
                                              + ".", "How much " + "were you off?",
                                               JOptionPane.PLAIN_MESSAGE);
            }
        }
    }

    JFrame frame = new JFrame();
        frame.setTitle("Guess the Color!");
        frame.setSize(WIDTH, HEIGHT);

    button.addActionListener(new ButtonListener());

    panel = new JPanel();
    panel.add(label);
    panel.add(field);
    panel.add(button);

    frame.add(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
}

我怎样才能使消息对话框只显示一次,然后在按下OK按钮时消失?

0 个答案:

没有答案