java - 当数字超过 100 时如何解决 jlabel 数字问题?

标签 java swing jlabel

我做了一些简单的游戏,比如 cookies 点击器。它按预期工作,但问题是当点数超过 100 时我无法正确看到它们。我将在下面留下完整的代码。由于某种原因,我把它弄得有点复杂,但现在我遇到的唯一问题是 jLabel...

所以我希望有人能帮助我。

package javaapplication17;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.*;

public class JavaApplication17 {

    public static void main(String[] args) {
        DBP prozor = new DBP();
        prozor.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        prozor.setVisible(true);
        prozor.setLocation(600, 250);
        prozor.setLayout(null);


    }

}
class DBP extends JFrame{
        private JLabel oznaka;
        private int brojac;
        private boolean upgraded1;
        private boolean upgraded2;
        private boolean upgraded3;
        private boolean isup1;
        private boolean isup2;
        private boolean isup3;

        public DBP(){
            setTitle("Cookie Clicker");
            setSize(800, 480);
            setLayout (new FlowLayout(FlowLayout.CENTER, 30, 20));



            oznaka = new JLabel("No points!");

            add(oznaka);
            JButton dugme = new JButton("Click");
            add(dugme);





            JButton upgrade = new JButton("Buy first upgrade!");
            upgrade.setBounds(317, 50, 150, 50);
            add(upgrade);
            upgrade.setVisible(false);

            JButton upgrade2 = new JButton("Buy second upgrade!");
            upgrade2.setBounds(317, 100, 150, 50);
            add(upgrade2);
            upgrade2.setVisible(false);

            JButton upgrade3 = new JButton("Buy third upgrade!");
            upgrade3.setBounds(317, 150, 150, 50);
            add(upgrade3);
            upgrade3.setVisible(false);



            dugme.addActionListener(new ActionListener(){


            public void actionPerformed(ActionEvent e){

                brojac++;

                if(upgraded1){
                    isup1 = true;
                }

                if(upgraded2){
                    isup2 = true;
                }

                if(upgraded3){
                    isup3 = true;
                }

                if (isup1){
                    brojac += -1;
                }

                if(isup1){
                    brojac += 2;
                }

                if(isup3){
                    brojac += 4;
                }

                oznaka.setText("Points " + brojac);
                if (brojac >= 5 && !upgraded1){
                    upgrade.setVisible(true);

                }
                if(brojac >= 10 && !upgraded2){
                    upgrade2.setVisible(true);
                    if (isup1){
                        upgrade2.setBounds(317, 50, 150, 50);
                    }
                }
                if(brojac >= 50 && !upgraded3){
                    upgrade3.setVisible(true);
                    if (isup1 && isup2){
                        upgrade3.setBounds(317, 50, 150, 50);
                        }else if (isup1 && !isup2){
                            upgrade3.setBounds(317, 100, 150, 50);
                            }else {
                                upgrade3.setBounds(317, 150, 150, 50);
                    }
                }
               }
            });


            upgrade.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e){
                    upgraded1 = true;
                    isup1 = true;
                    upgrade.setVisible(false);
                    brojac -= 5;
                    oznaka.setText("Points " + brojac);
                 }
            });

            upgrade2.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e){
                    upgraded2 = true;
                    isup2 = true;
                    upgrade2.setVisible(false);
                    brojac -= 10;
                    oznaka.setText("Points " + brojac);
                 }
            });

            upgrade3.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e){
                    upgraded3 = true;
                    isup3 = true;
                    upgrade3.setVisible(false);
                    brojac -= 50;
                    oznaka.setText("Points " + brojac);
                 }
            });

}
}

最佳答案

设置首选尺寸:

oznaka.setPreferredSize(new Dimension(100, 20));

或者

将初始文本设置得足够大(附加空格):

oznaka = new JLabel("No points!       ");

关于java - 当数字超过 100 时如何解决 jlabel 数字问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57578659/

相关文章:

java - 将 Countrylist 模型、数据和 Arraylist 从 Java 重写为 kotlin

java:包 jdk.incubator.foreign 在 Java 15 中不可见错误

java - JTextField - Java swing - 没有光标?

java - 如何在几秒钟内更改和恢复 JTable 中的单元格背景?

java - Maven2 和 Swing 项目 : Build & run swing application

java - 更改 JLabel 字体大小

java - 如何验证属性/自定义配置文件?

java - 集成 Angular4 作为前端,Java 作为后端

java - 居中 JTextField(不是字段内的文本!)

java - 无法使用 GridBagLayout 将 JLabel 替换为其他 JLabel