java - 如何修复发送垃圾邮件的 JOptionPane?

标签 java swing jframe joptionpane

所以我创建了这个程序,之前它运行得很好,但是在转移计算机之后,我的程序爆炸了。 JOptionPane 正在被垃圾邮件发送并完全运行内存。如果您能运行这个程序并告诉我出了什么问题,我将不胜感激。该程序应该在 JFrame 上创建一张宾果卡。

package Bingopackage;



import javax.swing*;

import java.awt.*;

import java.awt.event.*;

import java.awt.AWTEvent;


public class BingoCard extends JComponent {


    public void paintComponent(Graphics g)
     {
        //Recover Graphics



        int BValues []= new int [5];
        int IValues []= new int [5];
        int NValues []= new int [4];
        int GValues []= new int [5];
        int OValues []= new int [5];
        JOptionPane.showMessageDialog(null," We will now guide you in making your very own Bingo Card");
        for (int B = 0; B<BValues.length; B++)
        {
        String BVal = JOptionPane.showInputDialog(null, "Please enter 5 different numbers"+ "\n" + "that are in the range of 1-15");
        BValues[B]=Integer.parseInt(BVal);
        if (BValues[B]<1 || BValues[B]>15)
        { JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        }
        int B1 = BValues [0];
        int B2 = BValues [1];
        int B3 = BValues [2];
        int B4 = BValues [3];
        int B5 = BValues [4];
        if (B1==B2 || B1==B3 || B1== B4 || B1==B5 || B2==B3 || B2== B4|| B2==B5 || B3== B4 || B3== B5 || B4==B5)
        {
        JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        for (int I = 0; I<IValues.length; I++)
        {
        String IVal = JOptionPane.showInputDialog(null, "Please enter 5 different numbers"+ "\n" + "that are in the range of 16-30");
        IValues[I]=Integer.parseInt(IVal);
        if (IValues[I]<16 || IValues[I]>30)
        { JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        }
        int I1 = IValues [0];
        int I2 = IValues [1];
        int I3 = IValues [2];
        int I4 = IValues [3];
        int I5 = IValues [4];
        if (I1==I2 || I1==I3 || I1== I4 || I1==I5 || I2==I3 || I2== I4|| I2==I5 || I3== I4 || I3== I5 || I4==I5)
        {
        JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        for (int N = 0; N<NValues.length; N++)
        {
        String NVal = JOptionPane.showInputDialog(null, "Please enter 4 different numbers"+ "\n" + "that are in the range of 31-45");
        NValues[N]=Integer.parseInt(NVal);
        if (NValues[N]<31 || NValues[N]>45)
        { JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        }
        int N1 = NValues [0];
        int N2 = NValues [1];
        int N3 = NValues [2];
        int N4 = NValues [3];
        if (N1==N2 || N1==N3 || N1== N4 || N2==N3 || N2== N4|| N3== N4)
        {
        JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        for (int G = 0; G<GValues.length; G++)
        {
        String GVal = JOptionPane.showInputDialog(null, "Please enter 5 different numbers"+ "\n" + "that are in the range of 46-60");
        GValues[G]=Integer.parseInt(GVal);
        if (GValues[G]<46 || GValues[G]>60)
        { JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        }
        int G1 = GValues [0];
        int G2 = GValues [1];
        int G3 = GValues [2];
        int G4 = GValues [3];
        int G5 = GValues [4];
        if (G1==G2 || G1==G3 || G1== I4 || G1==G5 || G2==G3 || G2== G4|| G2==G5 || G3== G4 || G3== G5 || G4==G5)
        {
        JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        for (int O = 0; O<OValues.length; O++)
        {
        String OVal = JOptionPane.showInputDialog(null, "Please enter 5 different numbers"+ "\n" + "that are in the range of 61-75");
        OValues[O]=Integer.parseInt(OVal);
        if (OValues[O]<61 || OValues[O]>75)
        { JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }
        }
        int O1 = OValues [0];
        int O2 = OValues [1];
        int O3 = OValues [2];
        int O4 = OValues [3];
        int O5 = OValues [4];
        if (O1==O2 || O1==O3 || O1== O4 || O1==O5 || O2==O3 || O2== O4|| O2==O5 || O3== O4 || O3== O5 || O4==O5)
        {
        JOptionPane.showMessageDialog(null, "You have failed to comply");
        System.exit (0);
        }


        Graphics2D g3 = (Graphics2D) g;
        g3.setColor(Color.BLACK);
        Font bingoFont = new Font ("Tahoma", Font.PLAIN,69);
        g3.setFont(bingoFont);

        Rectangle bingosign = new Rectangle (75,10, 500, 75);
        g3.draw(bingosign);
        g3.drawString ("B   I   N  G  O", 110, 70);

        Rectangle B1Box = new Rectangle (75, 85, 100, 100);
        g3.draw(B1Box);

        Rectangle B2Box = new Rectangle (75, 185, 100, 100);
        g3.draw(B2Box);

        Rectangle B3Box = new Rectangle (75, 285, 100, 100);
        g3.draw(B3Box);

        Rectangle B4Box = new Rectangle (75, 385, 100, 100);
        g3.draw(B4Box);

        Rectangle B5Box = new Rectangle (75, 485, 100, 100);
        g3.draw(B5Box);

        Rectangle I1Box = new Rectangle (175, 85, 100, 100);
        g3.draw(I1Box);

        Rectangle I2Box = new Rectangle (175, 185, 100, 100);
        g3.draw(I2Box);

        Rectangle I3Box = new Rectangle (175, 285, 100, 100);
        g3.draw(I3Box);

        Rectangle I4Box = new Rectangle (175, 385, 100, 100);
        g3.draw(I4Box);

        Rectangle I5Box = new Rectangle (175, 485, 100, 100);
        g3.draw(I5Box);

        Rectangle N1Box = new Rectangle (275, 85, 100, 100);
        g3.draw(N1Box);

        Rectangle N2Box = new Rectangle (275, 185, 100, 100);
        g3.draw(N2Box);

        Rectangle N3Box = new Rectangle (275, 285, 100, 100);
        g3.draw(N3Box);

        Rectangle N4Box = new Rectangle (275, 385, 100, 100);
        g3.draw(N4Box);

        Rectangle N5Box = new Rectangle (275, 485, 100, 100);
        g3.draw(N5Box);

        Rectangle G1Box = new Rectangle (375, 85, 100, 100);
        g3.draw(G1Box);

        Rectangle G2Box = new Rectangle (375, 185, 100, 100);
        g3.draw(G2Box);

        Rectangle G3Box = new Rectangle (375, 285, 100, 100);
        g3.draw(G3Box);

        Rectangle G4Box = new Rectangle (375, 385, 100, 100);
        g3.draw(G4Box);

        Rectangle G5Box = new Rectangle (375, 485, 100, 100);
        g3.draw(G5Box);

        Rectangle O1Box = new Rectangle (475, 85, 100, 100);
        g3.draw(O1Box);

        Rectangle O2Box = new Rectangle (475, 185, 100, 100);
        g3.draw(O2Box);

        Rectangle O3Box = new Rectangle (475, 285, 100, 100);
        g3.draw(O3Box);

        Rectangle O4Box= new Rectangle (475, 385, 100, 100);
        g3.draw(O4Box);

        Rectangle O5Box = new Rectangle (475, 485, 100, 100);
        g3.draw(O5Box);

        g3.setColor(Color.RED);
        g3.fill(N3Box);

        g3.setColor(Color.BLACK);
        String B1String = String.valueOf(B1);
        String B2String = String.valueOf(B2);
        String B3String = String.valueOf(B3);
        String B4String = String.valueOf(B4);
        String B5String = String.valueOf(B5);
        String I1String = String.valueOf(I1);
        String I2String = String.valueOf(I2);
        String I3String = String.valueOf(I3);
        String I4String = String.valueOf(I4);
        String I5String = String.valueOf(I5);
        String N1String = String.valueOf(N1);
        String N2String = String.valueOf(N2);
        String N3String = String.valueOf(N3);
        String N4String = String.valueOf(N4);
        String G1String = String.valueOf(G1);
        String G2String = String.valueOf(G2);
        String G3String = String.valueOf(G3);
        String G4String = String.valueOf(G4);
        String G5String = String.valueOf(G5);
        String O1String = String.valueOf(O1);
        String O2String = String.valueOf(O2);
        String O3String = String.valueOf(O3);
        String O4String = String.valueOf(O4);
        String O5String = String.valueOf(O5);

        g3.drawString(B1String, 90, 160);
        g3.drawString(B2String, 90, 260);
        g3.drawString(B3String, 90, 360);
        g3.drawString(B4String, 90, 460);
        g3.drawString(B5String, 90, 560);
        g3.drawString(I1String, 190, 160);
        g3.drawString(I2String, 190, 260);
        g3.drawString(I3String, 190, 360);
        g3.drawString(I4String, 190, 460);
        g3.drawString(I5String, 190, 560);
        g3.drawString(N1String, 290, 160);
        g3.drawString(N2String, 290, 260);
        g3.drawString(N3String, 290, 460);
        g3.drawString(N4String, 290, 560);
        g3.drawString(G1String, 390, 160);
        g3.drawString(G2String, 390, 260);
        g3.drawString(G3String, 390, 360);
        g3.drawString(G4String, 390, 460);
        g3.drawString(G5String, 390, 560);
        g3.drawString(O1String, 490, 160);
        g3.drawString(O2String, 490, 260);
        g3.drawString(O3String, 490, 360);
        g3.drawString(O4String, 490, 460);
        g3.drawString(O5String, 490, 560);


    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.LIGHT_GRAY);
    Font freeSpaceFont = new Font("Helvetica", Font.PLAIN,16);
    g2.setFont(freeSpaceFont);
    g2.drawString("Free", 305, 330);
    g2.drawString("Space", 300, 350);


     }
}

package Bingopackage;

import javax.swing.JFrame;
import Bingopackage.BingoCard;

public class BingoCardJFrame {

        public static void main(String[] args)
         {
        // TODO Auto-generated method stub/
        JFrame frame =new JFrame();
        frame.setSize(700,650);
        frame.setTitle("Bingo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   

        BingoCard component = new BingoCard();
        frame.add(component);

        frame.setVisible(true);

         }

}

最佳答案

首先阅读 Performing Custom PaintingPainting in AWT and Swing这样您就可以了解 Swing 中的绘画是如何完成的。

接下来,从组件的 paintComponent 方法中删除与绘制组件不直接相关的所有内容

Swing 是一个事件驱动的环境,这意味着发生了某些事情并且您对其做出响应。 Swing 会出于多种原因重新绘制您的组件,其中许多原因是您无法控制的。

看看How to create a GUI with Swing了解更多详情

关于java - 如何修复发送垃圾邮件的 JOptionPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20831134/

相关文章:

java - OpenGL 和 AWT/Swing 用户界面

java - 如何修改 HTML JLabel 以使用小于 8k 的缓冲区?

java - 像 Microsoft 一样居中文本

java - Matlab + Java + JNI : java. lang.UnsatisfiedLinkError

java - 从 GPS 设备读取数据

java - 如何捕获 javas-swing 应用程序中文本区域中的 jar 文件发出的控制台输出

java - BoxLayout 无法共享错误?

java - 如何正确执行密码的 while 循环

Java : Throwable not caught

Java 在给定枚举值的情况下获取枚举名称