java是否可以让JLabel根据变量值更改其文本?
我创建了一个JLabel,如果变量计数==-1,
如果变量计数=0,则为“文本B”;如果变量计数=1,则为“文本C”
我使用Swing创建了我的界面,您可以在下面看到
红色矩形显示JLabel应该在哪里
我尝试创建3个JLabel,并在变量计数值条件适用时更改setVisible(布尔值)。这不起作用,因为我遇到以下错误:
线程“main”java中出现异常。lang.NullPointerException 在坦佩鲁伊。温度转换器。main(TempConverter.java:354) C:\Users\x\AppData\Local\NetBeans\Cache\8.1\executor snippets\run。xml:53:Java返回:1
而且JLabel不能放在GUI中的同一位置(不可能重叠)
我试过使用jLabel。setText()以在变量条件应用时更改JLabel中显示的文本。我得到了一个与上面类似的错误(如果不是相同的话)
我已经阅读了其他一些帖子并进行了进一步的研究,发现有些人建议设置ActionListeners,但我不确定它们是否能与简单的变量一起工作,而不是GUI中的组件
我的代码如下:
package tempconverterUI;
import javax.swing.JOptionPane;
import messageBoxes.UserData;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.WString;
public class TempConverter extends javax.swing.JFrame {
public interface someLib extends Library
{
public int engStart();
public int endStop();
public int engCount();
public WString engGetLastError();
public int engSetAttribute(WString aszAttributeID, WString aszValue);
}
/**
* Creates new form TempConverter
*/
public TempConverter() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
布局是在这里创建的,后面是温度转换方法和无关组件的功能(我认为这与本例无关)
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/**This is where the Login form gets created*/
UserData.popUp();
/**After this the Library functions are called, which will return the variable count value*/
someLib lib = (someLib) Native.loadLibrary("someLib", someLib.class);
int startResult = lib.engStart();
System.out.println(startResult);
if (startResult < 0)
{
System.out.println(lib.engGetLastError());
}
System.out.println(UserData.getAcInput());
int setAtResult = lib.engSetAttribute(new WString("CODE"), UserData.getAcInput());
System.out.println(setAtResult);
if (setAtResult < 0)
{
System.out.println(lib.engGetLastError());
}
接下来是一段代码,我应该从中控制要显示的JLabel文本
int count = lib.engCount();
System.out.println(count);
if (count == -1)
{
System.out.println(lib.engGetLastError());
}
else if (count == 0)
{
}
else
{
}
new TempConverter().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JPanel bottomPanel;
private javax.swing.JButton convertButton;
private static javax.swing.JButton button;
private javax.swing.JTextField from;
private javax.swing.JComboBox<String> fromCombo;
private javax.swing.JLabel fromLabel;
private javax.swing.JLabel title;
private javax.swing.JTextField to;
private javax.swing.JComboBox<String> toCombo;
private javax.swing.JLabel toLabel;
private javax.swing.JPanel topPanel;
// End of variables declaration
}
在此方面的任何帮助都将不胜感激。如果您还可以包含一个简单的代码示例,这将是非常棒的,因为我对Java(以及编程)是新手
# 1 楼答案
问题:
setText(...)
设置其文本即可李>public void setLabelText(String text)
,在JLabel上的方法调用setText(text)
李>关于后者的一个例子: