Reputation: 1
For some reason when I set my JLabel equal to a string it wont appear. I have imported the correct library and added it so it will make the text white and I set it to a font but I am so confused and I am new at java someone help!
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
}
}
Upvotes: 0
Views: 850
Reputation: 285403
You are making erroneous assumptions here. If you place a String into the JLabel and thereby set its contents, if you later change the String variable, this will have no effect on the String displayed by the JLabel. Understand that Strings are immutable, meaning that a String object cannot change ever. A String variable can refer to a new different String object, but the original String object, remains unchanged.
To change what a JLabel displays you must explicitly call setText(newString)
on the JLabel.
Also, you should not override a JFrame's paint method, and even if you could do this, you should never change Swing components, or add components within a paint or paintComponent method. The method that should be overridden is a JPanel's paintComponent method, and it should be for painting and painting only, nothing more.
Lots of errors and problems here. I suggest starting over.
Edit
So again to summarize:
setText(...)
on the JLabel. Changing the original String variable that was used to create the JLabel will have no effect on the JLabel's display.
Edit 2
Upvotes: 3