【问题标题】:Graphing with JLabel for looks [null layout] Java Swing *ERROR使用 JLabel 绘制外观 [null layout] Java Swing *ERROR
【发布时间】:2019-04-13 07:15:28
【问题描述】:

我正在尝试使用 JLabels 构建学生图表

我的应用程序执行 sql 查询并为每个月的条目数返回一个 int 值,如果没有找到它返回 0。

//scale variable is an int

//ene is a return value from another method that runs before

//in case there are no students in january I dont have to graph anything
//so this 'if' doesnt run

Scale = 50;  

if (Ene != 0) { 

            System.out.println(Ene + " ene stdds");

// this prints out 11 ene stdds

            double EneBH = 449 * (Ene / Scale);
            int EneBHeight = (int) Math.round(EneBH);
            int EneBYLocal = 612 - EneBHeight;

            EneP.setBounds(76, EneBYLocal, 7, EneBHeight);
            EneP.setVisible(true);

        } else {

//if the last if didnt run I want to know if it hidd the label

            System.out.println("HIDDEN ENE");
            EneP.setVisible(false);
        }

*Ene P 是第一个用于绘图的 jlabel,它看起来是灰色的,位于 enero 区域。

*EneP 打印出 11 个学生但从未出现,不打印隐藏的 ene,它只是不显示

*EneP 如果我解决它或者你解决它,将具有与其他 jlabels 相同的代码,请

【问题讨论】:

标签: java swing interface helper


【解决方案1】:

我找到了解决我自己问题的方法

解决方案很简单,如果你将 Ene(保存为整数值的查询的返回变量)除以 Scale 变量,它也是一个整数,它会导致 java 返回 0

所以我自己发现了这一点,方法是在每一行之后放置一个 system.out.print() 并打印每个变量的值。

我注意到 11/50 不应该返回 0,所以我将 scale 变量更改为 double 并且仍然返回 0;但我都改了,现在效果很好。

Scale = 50;  

if (Ene != 0) { 

        System.out.println(Ene + " ene stdds");

// this prints out 11 ene stdds

        double EneBH = 449 * (Ene / Scale);
        int EneBHeight = (int) Math.round(EneBH);
        int EneBYLocal = 612 - EneBHeight;

        EneP.setBounds(76, EneBYLocal, 7, EneBHeight);
        EneP.setVisible(true);

    } else {

  //if the last if didnt run I want to know if it hidd the label

        System.out.println("HIDDEN ENE");
        EneP.setVisible(false);
    }

enter image description here

【讨论】:

    猜你喜欢
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多