0

出于某种原因,当我将我的 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              
        }           
}
4

1 回答 1

3

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:

  • Don't override a JFrame's paint method.
  • If you must do drawing in a Swing GUI, do it in a JPanel or JComponent's paintComponent method.
  • Be sure to call the super paintComponent method first thing in your paintComponent override.
  • Use this method for painting and painting only. Don't add or remove components from the GUI, don't do any program logic in this method. You cannot control when or if it gets called and so cannot rely on it. Also, the method must be blazing fast else your program will be perceived as slow.
  • Don't set position of components. Use the layout managers, often multiple layouts by using multiple JPanels, some nested in others, each with its own layout.
  • To change a JLabel's text, call 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

  • You will need to understand the layout managers, that for instance a JFrame uses BorderLayout by default. So anything added to it without specifying where in relation to the BorderLayout constants gets added BorderLayout.CENTER and covers over all previously added components. Consider using a different layout such as FlowLayout for your simple GUI.
于 2013-09-25T19:38:04.460 回答