当我一次只想要一个答案时,我的代码会打印出多个答案,你能帮我一次只打印一个吗?
我也想要帮助,这样当它运行时,你可以按下一个按钮,刷新页面,产生一个新的问题,无论如何.
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);
}
Run Code Online (Sandbox Code Playgroud)
调用JFrame.setSize将调用该paint方法。初始化后设置大小并将其从构造函数中删除。
//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);
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
编辑: 您还应该将背景颜色设置为内容窗格而不是实际框架。完成所有这些操作后,我们应该将其设置为可见。所以它看起来像这样:
public static void main(String[] args){
JFrame jframe = new Image();
jframe.setSize(750,750);
jframe.getContentPane().setBackground(Color.CYAN);
jframe.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)