出于某种原因,当我将我的 JLabel 设置为一个字符串时,它不会出现。我已经导入了正确的库并添加了它,所以它会使文本变白,我将它设置为一种字体,但我很困惑,我是 java 新手,有人帮忙!
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
public class math extends JFrame{
//Variables
public Font text = new Font("Comic Sans MS", Font.BOLD, 50);
public Color bordercolor = new Color(255,178,102);
public int anser;
public int random1;
public int random2;
public Random randomnumber1 = new Random();
public Random randomnumber2 = new Random();
public String problem;
public JTextField textbox = new JTextField(2);
public JLabel textanser = new JLabel(problem);
public math(){
setBackground(Color.BLACK);
setLocationRelativeTo(null);
setTitle("Test");
setSize(1300,650);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// addMouseListener(new ml());
}
//Paint method
public void paint(Graphics g){
g.setColor(bordercolor);
g.fillRect(0, 0, 50, 700);
g.fillRect(1250,0, 50,700);
g.fillRect(0,20,1250,50);
g.fillRect(0,600,1500,50);
//Addition Math Problem
random1 = 1+randomnumber1.nextInt(10);
random2 = 1+randomnumber2.nextInt(10);
problem = random1 + " + " + random2;
anser = random1 + random2;
textanser.setLocation(585,275);
textanser.setFont(text);
textanser.setText(problem);
textanser.setForeground(Color.white);
//Problem on screen
g.setFont(text);
g.setColor(Color.WHITE);
add(textanser);
//g.drawString(problem,585,275);
System.out.println(anser);
//Text Box
textbox.setLocation(600, 300);
textbox.setSize(100,30);
add(textbox);
//Action Listener
textbox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String input = textbox.getText();
double intanser = Double.parseDouble(input);
//Comparing user anser and real anser
if(anser != intanser ){
JOptionPane.showMessageDialog(null, "Incorrect the correct anser was " + anser);
}
//If anser is correct
if(anser == intanser){
JOptionPane.showMessageDialog(null, "Correct!");
}
}
});
//Integer
}
public static void main(String[] args) {
math math = new math();
}
public void mouseClicked() {
// TODO Auto-generated method stub
}
}