Java 是否可以让JLabel根据变量值更改其文本?

Java 是否可以让JLabel根据变量值更改其文本?,java,swing,Java,Swing,我创建了一个JLabel,如果变量计数==-1, 如果变量计数=0,则为“文本B”;如果变量计数=1,则为“文本C” /** * @param args the command line arguments */ public static void main(String args[]) { /**This is where the Login form gets created*/ UserData.popUp(); /**After this t

我创建了一个JLabel,如果变量计数==-1,
如果变量计数=0,则为“文本B”;如果变量计数=1,则为“文本C”

/**
 * @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());
    }
我使用Swing创建了我的界面,您可以在下面看到

/**
 * @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());
    }

/**
 * @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());
    }

/**
 * @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应该在哪里

/**
 * @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());
    }
我尝试创建3个JLabel,并在变量计数值条件适用时更改setVisible(布尔值)。这不起作用,因为我遇到以下错误:

/**
 * @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());
    }
线程“main”java.lang.NullPointerException中出现异常 位于tempconverterUI.TempConverter.main(TempConverter.java:354) C:\Users\x\AppData\Local\NetBeans\Cache\8.1\executor snippets\run.xml:53:Java返回:1

/**
 * @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不能放在GUI中的同一位置(不可能重叠)

/**
 * @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.setText()来更改jLabel中显示的文本,只要变量条件适用。我得到了一个与上面类似的错误(如果不是相同的话)

/**
 * @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());
    }
我已经阅读了其他一些帖子并进行了进一步的研究,发现有些人建议设置ActionListeners,但我不确定它们是否能与简单的变量一起工作,而不是GUI中的组件

/**
 * @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());
    }
我的代码如下:

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());
    }
package-tempconverterUI;
导入javax.swing.JOptionPane;
导入messagebox.UserData;
导入com.sun.jna.Library;
导入com.sun.jna.Native;
导入com.sun.jna.WString;
公共类TempConverter扩展了javax.swing.JFrame{
公共接口someLib扩展库
{
公共int-engStart();
公共int endStop();
公共int engCount();
public WString engGetLastError();
public int engSetAttribute(WString aszAttributeID,WString aszValue);
}
/**
*创建新的表单转换器
*/
公共临时转换器(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
布局是在这里创建的,后面是温度转换方法和无关组件的功能(我认为这与本例无关)

/**
 * @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());
    }
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/**这是创建登录表单的地方*/
UserData.popUp();
/**在此之后,将调用库函数,它将返回变量计数值*/
someLib=(someLib)Native.loadLibrary(“someLib”,someLib.class);
int startResult=lib.engStart();
System.out.println(startResult);
如果(开始结果<0)
{
System.out.println(lib.engGetLastError());
}
System.out.println(UserData.getAcInput());
int setAtResult=lib.engSetAttribute(新的WString(“代码”),UserData.getaciput();
系统输出打印项次(setAtResult);
如果(setAtResult<0)
{
System.out.println(lib.engGetLastError());
}
接下来是一段代码,我应该从中控制要显示的JLabel文本

/**
 * @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());
    }
    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                   
int count=lib.engCount();
系统输出打印项次(计数);
如果(计数==-1)
{
System.out.println(lib.engGetLastError());
}
否则如果(计数==0)
{
}
其他的
{
}
新建TempConverter().setVisible(true);
}
//变量声明-不修改
私有javax.swing.JPanel底部面板;
私有javax.swing.JButton转换按钮;
私有静态javax.swing.JButton按钮;
来自的私有javax.swing.JTextField;
私有javax.swing.jcombox fromCombo;
私有javax.swing.JLabel fromLabel;
私有javax.swing.JLabel标题;
私有javax.swing.JTextField到;
私有javax.swing.JComboBox-toCombo;
私有javax.swing.JLabel-toLabel;
私有javax.swing.JPanel-topPanel;
//变量结束声明
}

/**
 * @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());
    }
如果您能提供一个简单的代码示例,这将是非常棒的,因为我对Java(以及编程)是新手。

问题:

/**
 * @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设置为可见,而是首先将其添加到GUI中,默认情况下保持可见,然后通过
    setText(…)
    简单地设置其文本
  • 为包含JLabel公共方法的类指定允许外部类设置标签文本的功能。类似于
    public void setLabelText(String text)
    ,以及在JLabel上的方法调用
    setText(text)
  • 像调试任何其他NPE一样调试NullPointerException——查看stacktrace,找到抛出它的行,然后回顾代码,查看该行上的键变量为null的原因
  • 更改JLabel的时间和方式将取决于您要侦听的事件。如果是用户输入,则您将希望响应该输入,无论是添加到JButton或JTextField的ActionListener,还是添加到JRadioButton的itemListener
  • 如果您希望监听变量状态的更改,不管变量如何更改,那么使用PropertyChangeSupport和PropertyChangeListener将其设置为“绑定属性”()
  • 关于后者的一个例子:

    /**
     * @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());
        }
    
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    
    import javax.swing.*;
    import javax.swing.event.SwingPropertyChangeSupport;
    
    @SuppressWarnings("serial")
    public class ShowCount extends JPanel {
        private static final int TIMER_DELAY = 1000;
        private JLabel countLabel = new JLabel("                 ");
        private CountModel model = new CountModel();
    
        public ShowCount() {
            model.addPropertyChangeListener(CountModel.COUNT, new ModelListener(this));
    
            setPreferredSize(new Dimension(250, 50));
            add(new JLabel("Count:"));
            add(countLabel);
    
            Timer timer = new Timer(TIMER_DELAY, new TimerListener(model));
            timer.start();
        }
    
        public void setCountLabelText(String text) {
            countLabel.setText(text);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> createAndShowGui());
        }
    
        private static void createAndShowGui() {
            ShowCount mainPanel = new ShowCount();
            JFrame frame = new JFrame("ShowCount");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(mainPanel);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    }
    

    /**
     * @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());
        }
    

    /**
     * @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());
        }
    

    /**
     * @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());
        }
    
    问题:

    /**
     * @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设置为可见,而是首先将其添加到GUI中,默认情况下保持可见,然后通过
    setText(…)
    简单地设置其文本
  • 为包含JLabel公共方法的类指定允许外部类设置标签文本的功能。类似于
    public void setLabelText(String text)
    ,以及在JLabel上的方法调用
    setText(text)
  • 像调试任何其他NPE一样调试NullPointerException--查看stacktrace,找到抛出它的行,