制作随机字符字母,并在Java swing中一一显示

时间:2019-03-08 17:37:12

标签: java swing

我试图制作随机字母,但只能包含三个字母,并且我想在按下“随机”按钮时在创建的窗口中逐一显示它们,但是当我按下随机按钮时,循环没有停下来。并以与CLI尝试相同的方式给了我预期的结果。我已经尝试了很多次,但是没有用。请帮忙吗?

以下是我程序的完整源代码:

package testGrafik;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestRandom extends JFrame {

public TestRandom() {
    setTitle("Test Game");
    setSize(500, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    Board board = new Board();
    getContentPane().add(board);
}

public static void main(String[] args) {
    new TestRandom();
}

public class Board extends JPanel implements MouseListener, 
MouseMotionListener {
    int x;
    int y;
    public boolean isMouseMoving = false;
    public boolean isClicked = false;

    public char[] character = {'S', 'O', 'I'};
    public Random rand = new Random();

    public Board() {
        x = 0;
        y = 0;

        addMouseListener(this);
        addMouseMotionListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.DARK_GRAY);
        g.fillRect(x, y, 500, 500);

        // draw box
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(x + 10, y + 10, 70, 40);

        if (isMouseMoving == true) {
            g.setColor(Color.cyan);
            g.fillRect(x + 10, y + 10, 70, 40);
        }

        //draw string
        g.setColor(Color.BLACK);
        g.setFont(new Font("Tahoma", Font.BOLD, 15));
        g.drawString("Click", x + 25, y + 35);

        if (isClicked == true) {
            char c = character[rand.nextInt(character.length)];
            String convertString = String.valueOf(c);

            g.setColor(Color.WHITE); // border color
                g.setFont(new Font("Tahoma", Font.PLAIN, 30)); // 
    String.valueOf(hrf)
                g.drawString(convertString, x + 500 / 2, y + 500 / 2); // 
border boax t4 huruf muncul
                System.out.println("Latter: " + convertString); 
            }
            repaint();
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            int mx = e.getX();
            int my = e.getY();

            if (mx > x + 10 && mx < x + 10 + 70 && my > y + 10 && my < y + 
10 + 40) {
                isClicked = true;
                System.out.println("YOU CKLIK ON THE BOX");
            } else {
                isClicked = false;
                System.out.println("You click outside the box");
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseDragged(MouseEvent e) {
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            int mx = e.getX();
            int my = e.getY();

            if (mx > x + 10 && mx < x + 10 + 70 && my > y + 10 && my < y + 
10 + 40) {
                isMouseMoving = true;
            } else {
                isMouseMoving = false;
            }
        }
    }
}

以及以下是我在CLI中尝试的代码:

package testGrafik;

import java.util.Random;

public class TestRandomCLI {

    public static void main(String[] args) {
        char[] character = {'S', 'O', 'I'};
        Random rand = new Random(); 
        char c = character[rand.nextInt(character.length)];
        String convertString = String.valueOf(c);
        System.out.println("Latter: " + convertString);
    }  
}

1 个答案:

答案 0 :(得分:1)

发生这种情况是因为您在JComponent#repaint()中调用了JComponent#paintComponent()方法。当您呼叫repaint()时,将呼叫paintComponent。再一次,repaint()将被称为...,您明白了。

解决方案:

用户单击时,仅呼叫repaint() 1次。

@Override
public void mouseClicked(MouseEvent e) {
    int mx = e.getX();
    int my = e.getY();

    if (mx > x + 10 && mx < x + 10 + 70 && my > y + 10 && my < y + 10 + 40) {
        isClicked = true;
        System.out.println("YOU CKLIK ON THE BOX");
        repaint(); //Only 1 call after call
    } else {
        isClicked = false;
        System.out.println("You click outside the box");
    }
}

当然要从paintComponent()

删除它
 if (isClicked == true) {
        char c = character[rand.nextInt(character.length)];
        String convertString = String.valueOf(c);

        g.setColor(Color.WHITE); // border color
            g.setFont(new Font("Tahoma", Font.PLAIN, 30)); // 
            g.drawString(convertString, x + 500 / 2, y + 500 / 2); // 
            System.out.println("Latter: " + convertString); 
        }
        //repaint();
    }