Print one random answer out of six

My code prints several answers when I need only one at a time, could you please help me print it only one at a time?

I would also like to help so that at startup a button appears on which you can click a button that will refresh the page, creating a new question, out of six anyway.

package ScienceRevision; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Image extends JFrame { Font font = new Font("Arial", Font.BOLD | Font.ITALIC, 30 ); public Image(){ //Game Properties setTitle("WheelOP"); //setSize(750, 750); setResizable(false); setBackground(Color.CYAN); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g){ g.setFont(font); g.setColor(Color.RED); Random rand = new Random(); int result = rand.nextInt(6); if (result == 0){ g.drawString("Cell Wall Definition", 100, 100 ); } else if (result == 1){ g.drawString("Vacuole Definition", 100, 100 ); } else if (result == 2){ g.drawString("Choroplast Definition", 100, 100 ); } else if (result == 3){ g.drawString("Nucleus Definition", 100, 100 ); } else if (result == 4){ g.drawString("Cell Membrane Definition", 100, 100 ); } else if (result == 5){ g.drawString("Cytoplasm Definition", 100, 100 ); } } public void paintcomponent (Graphics g){ } public static void main(String[] args){ JFrame jframe = new Image(); jframe.setSize(750,750); jframe.setVisible(true); jframe.getContentPane().setBackground(Color.CYAN); jframe.setVisible(true); } 
+5
source share
1 answer

A call to JFrame.setSize will call the paint method. Set the size after initialization and remove it from your constructor.

 //Game Properties setTitle("WheelOP"); //setSize(750, 750); setResizable(false); setVisible(true); setBackground(Color.CYAN); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public static void main(String[] args){ JFrame image = new Image(); image.setSize(750,750); } 

Hope this helps.

Edit: You should also set the background color to the content panel, and not to the actual frame. And we must make it visible by doing all this. So it will look like this:

 public static void main(String[] args){ JFrame jframe = new Image(); jframe.setSize(750,750); jframe.getContentPane().setBackground(Color.CYAN); jframe.setVisible(true); } 
+2
source

Source: https://habr.com/ru/post/1236726/


All Articles