Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我使用JOptionPane获取用户输入并显示消息。单击“确定”时,输入窗口和消息窗口是否都可以关闭?_Java_Swing_Joptionpane - Fatal编程技术网

Java I';我使用JOptionPane获取用户输入并显示消息。单击“确定”时,输入窗口和消息窗口是否都可以关闭?

Java I';我使用JOptionPane获取用户输入并显示消息。单击“确定”时,输入窗口和消息窗口是否都可以关闭?,java,swing,joptionpane,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.a

当程序运行时,它会显示一个充满各种颜色的小(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);
}
}
import java.util.*;
导入java.util.concurrent.*;
导入java.awt.*;
导入java.awt.font.*;
导入java.awt.event.*;
导入java.applet.*;
导入java.text.*;
导入javax.swing.*;
公共类TwoColorSpectrum扩展小程序{
公共空白绘制(图形方形){
正方形。设置颜色(颜色。白色);
正方形.fillRect(0,0,1280,750);
Random rectStr=新的Random();
int红色,
绿色,
蓝色,
randomX,
随机的,
直肠;
做{
rectNum=rectStr.nextInt(999999);
}而(rectNum<5000000);
随机色3=新随机色();
randomBlue=color3.nextInt(255);

对于(int h=1;h)让我们从开始,然后问这样一个问题:为什么要从applet中创建
JFrame
?哦,天哪…,不要将所有程序逻辑都放在paint方法中,paint是用于绘制的,其他什么都没有