当我一次只想要一个答案时,我的代码会打印出多个答案,你能帮我一次只打印一个吗?
我还希望得到帮助,这样当它运行时,你可以按下一个按钮,刷新页面,产生一个新的问题,无论如何都是这样。
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);
}
答案 0 :(得分:2)
调用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);
}
希望这会有所帮助。
修改强> 您还应将背景颜色设置为内容窗格而不是实际框架。我们应该在完成所有这些后将其设置为可见。所以它看起来像这样:
public static void main(String[] args){
JFrame jframe = new Image();
jframe.setSize(750,750);
jframe.getContentPane().setBackground(Color.CYAN);
jframe.setVisible(true);
}