【问题标题】:JLabel Not AppearingJLabel 未出现
【发布时间】:2013-10-01 13:29:21
【问题描述】:

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

【问题讨论】:

    标签: java swing jframe paint jlabel


    【解决方案1】:

    您在这里做出了错误的假设。如果将 String 放入 JLabel 并设置其内容,如果稍后更改 String 变量,这将不会影响 JLabel 显示的 String。了解字符串是不可变的,这意味着字符串对象永远不会改变。一个 String 变量可以引用一个新的不同的 String 对象,但原来的 String 对象保持不变。

    要更改 JLabel 显示的内容,您必须在 JLabel 上显式调用 setText(newString)

    此外,您不应该重写 JFrame 的paint 方法,即使您可以这样做,也不应该更改 Swing 组件,或在 paint 或 paintComponent 方法中添加组件。应该重写的方法是JPanel的paintComponent方法,它应该只用于绘画和绘画,仅此而已。

    这里有很多错误和问题。我建议重新开始。


    编辑
    所以再次总结一下:

    • 不要覆盖 JFrame 的绘制方法。
    • 如果您必须在 Swing GUI 中进行绘图,请在 JPanel 或 JComponent 的 paintComponent 方法中进行。
    • 请务必在您的 paintComponent 覆盖中首先调用 super paintComponent 方法。
    • 仅将此方法用于绘画和绘画。不要在 GUI 中添加或删除组件,不要在此方法中执行任何程序逻辑。您无法控制何时或是否调用它,因此不能依赖它。此外,该方法必须非常快,否则您的程序会被认为很慢。
    • 不要设置组件的位置。使用布局管理器,通常使用多个 JPanel 来实现多个布局,其中一些嵌套在其他 JPanel 中,每个都有自己的布局。
    • 要更改 JLabel 的文本,请在 JLabel 上调用 setText(...)。更改用于创建 JLabel 的原始 String 变量不会影响 JLabel 的显示。

    编辑 2

    • 您需要了解布局管理器,例如 JFrame 默认使用 BorderLayout。因此,添加到其中的任何内容都没有指定与 BorderLayout 常量相关的位置,都会添加 BorderLayout.CENTER 并覆盖所有先前添加的组件。考虑为您的简单 GUI 使用不同的布局,例如 FlowLayout。

    【讨论】:

    • 首先我不需要重写paint方法,即使我知道,其次将代码更改为 setText 并且没有出现任何东西 PS他们不需要废弃代码
    • @user2816767:好的,然后保留您的代码,但向我们展示您的代码没有覆盖覆盖,以及从 ActionListener 调用 JLabel 上的 setText(...) 的代码。另请参阅有关 GUI 布局的编辑答案。
    • 如何不覆盖我的绘制方法? (对不起,我有点菜鸟)
    • @user2816767:不存在绘制方法;摆脱它。而是在类的构造函数中构建 GUI,而不是在绘制方法中。
    • 我很困惑,我刚刚做了 setText(problem);并将其添加到 JFrame 并没有发生任何事情
    猜你喜欢
    • 2016-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多