更新jlabel [英] updating jlabel

查看:174
本文介绍了更新jlabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更新方法中的jlabel时遇到问题。这里是我的代码:

  JLabel curStatus = new JLabel(); 
JButton jbtnSubmit;

public static void main(String [] args){
test gui = new test();
gui.startGUI();
// gui.setCurStatus(testing!); <<似乎在这里工作,
//但是当我从另一个类调用它,它不想运行。
}

//为用户设置GUI结束
public void startGUI(){
//这些都是必需的GUI部件
new JTextArea();
final JFrame jfrm = new JFrame(my program);
jfrm.setLayout(new FlowLayout());
jfrm.setSize(300,300);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jbtnSubmit = new JButton(Submit);

jfrm.add(jbtnSubmit);
jfrm.add(curStatus);
jfrm.setVisible(true);
}

public void setCurStatus(String inCurStatus){
curStatus.setText(inCurStatus);
curStatus.setVisible(true);
}

发生了什么,是标签curStatus没有出现。例如,这里是一个调用:

  gui1.setCurStatus(现在正在运行诊断...请稍候! 


解决方案

您的问题似乎是错误的引用。 / p>

以下是创建GUI的方法:

  public static void main (String [] args){
test gui = new test();
gui.startGUI();
// gui.setCurStatus(testing!); <<似乎在这里工作,
//但是当我从另一个类调用它,它不想运行。
}

您创建了test对象符合Java命名约定的方式)。因为它是在main内部声明的,所以这个变量只有main内部的范围,并且在其他地方是可见的。



然后你告诉我们你正在调用方法:

  gui1.setCurStatus(现在正在运行诊断...请稍候! 

gui1变量引用一个测试类对象,但它可能引用一个



为了解决这个问题,你可以使用自动显示的测试对象必须确保在当前显示的测试对象上调用setCurStatus。如何做到这一点取决于你的代码的其余部分,你拒绝显示我们,尽管我们要求你这样做。



编辑:根据你最新的发布代码(仍然不会为我编译,因为它缺少一个方法, createTasksFile(),我的假设是正确的,你在不是显示的gui对象上调用 setCurStatus(...)

  public static String [] runDiagnostics()throws IOException {

gui gui1 = new gui(); //(A)
gui1.setCurStatus ...请等待!);

在行(A)创建一个新的gui对象调用setCurStatus就可以了,但它不是正在显示的GUI对象,而是一个完全不同的和不相关的对象。这只是关系,它是一个与显示的类相同的对象,但是解决方案是获取对显示的GUI的引用,并在该对象和该对象上调用此方法。



此外 ,Robin的假设是正确的,即使你解决这个问题,你会被困在一个Swing并发问题。 JLabel不会更新,因为Swing线程正在尝试打开一个文件:

  public static String [] runDiagnostics IOException {

gui gui1 = new gui();
gui1.setCurStatus(现在正在运行诊断...请稍候!

int i = 0;
int errorsI = 0;
File f = new File(tasks.txt);
String [] errors = {,,,,};

//尝试创建文件三次
do {
f.createNewFile();
i ++;
} while(!f.exists()&& i< 3);

所以我们都是对的。解决方案是在后台线程打开你的文件,SwingWorker将在这里很好地工作。



编辑2

所以要修复引用问题,使用gui参数将gui的引用传递到runDiagnostics方法。然后在此参数上调用setCurStatus方法。例如:

  public static String [] runDiagnostics(gui gui1)throws IOException {

! gui gui1 = new gui(); // !!不再需要
gui1.setCurStatus(现在正在运行诊断...请稍候!

调用方法时必须传递GUI:

  //! results = taskBckg.runDiagnostics(); 
results = taskBckg.runDiagnostics(gui);

此外,请编辑所有代码,以便遵循Java命名约定。所有类名都应以大写字母开头。这使得其他人更容易理解你的代码在做什么。


I am having trouble updating a jlabel in a method. here is my code:

    JLabel curStatus = new JLabel("");
JButton jbtnSubmit;

public static void main(String[] args) {
    test gui = new test();
    gui.startGUI();
    // gui.setCurStatus("testing!"); << seems to work here, 
    //but when i call it from another class, it doesn't want to run.
}

// Set up the GUI end for the user
public void startGUI() {
    // These are all essential GUI pieces
    new JTextArea("");
    final JFrame jfrm = new JFrame("my program");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(300, 300);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jbtnSubmit = new JButton("Submit");

    jfrm.add(jbtnSubmit);
    jfrm.add(curStatus);
    jfrm.setVisible(true);
}

public void setCurStatus(String inCurStatus) {
    curStatus.setText(inCurStatus);
    curStatus.setVisible(true);
}

what is happening, is that the label, curStatus is not appearing. for example, here is a call:

gui1.setCurStatus("Now running diagnostics... Please wait!");

解决方案

Your problem appears to be one of misplaced references.

Here is how you create your GUI:

  public static void main(String[] args) {
     test gui = new test();
     gui.startGUI();
     // gui.setCurStatus("testing!"); << seems to work here,
     // but when i call it from another class, it doesn't want to run.
  }

You create your "test" object (which should be named "Test" by the way to conform to Java naming conventions) inside of your main method. Since it is declared inside of main, this variable has scope only inside of main and is visible no where else.

You then tell us that you are calling the method like so:

gui1.setCurStatus("Now running diagnostics... Please wait!");

The gui1 variable refers to a test class object but it likely refers to a different object than the test object that is being displayed since the original displayed test object is only refered to by a variable local to the main method.

To solve this, you must make sure to call setCurStatus on the currently displayed test object. How to do this depends on the rest of your code, something you've refused to show us despite our requests for you to do so.

Edit: Based on your latest bit of posted code (which still won't compile for me since it is missing a method, createTasksFile(), my assumptions are correct, you are calling setCurStatus(...) on a gui object that is not the displayed one:

  public static String[] runDiagnostics() throws IOException {

     gui gui1 = new gui(); // (A)
     gui1.setCurStatus("Now running diagnostics... Please wait!");

On line (A) you create a new gui object and call setCurStatus on it, but it is not the GUI object that is being displayed but a completely different and unrelated object. It's only relation is that it is an object of the same class as the one being displayed but that's it. The solution is to get a reference to the displayed GUI and call this method on that object, and that object only.

Also, Robin's assumptions are correct, in that even if you fix this, you're going to be stuck with a Swing concurrency issue. The JLabel won't update because the Swing thread is trying to open a file:

  public static String[] runDiagnostics() throws IOException {

     gui gui1 = new gui();
     gui1.setCurStatus("Now running diagnostics... Please wait!");

     int i = 0;
     int errorsI = 0;
     File f = new File("tasks.txt");
     String[] errors = { "", "", "", "", "" };

     // try to create the file three times
     do {
        f.createNewFile();
        i++;
     } while (!f.exists() && i < 3);

So we're both right. The solution to this is to open your file on a background thread, a SwingWorker would work nicely here.

Edit 2
So to fix the reference problem, pass a reference of the gui into the runDiagnostics method using a gui parameter. Then call the setCurStatus method on this parameter. For example:

  public static String[] runDiagnostics(gui gui1) throws IOException {

     //!! gui gui1 = new gui(); // !! no longer needed
     gui1.setCurStatus("Now running diagnostics... Please wait!");

You would have to pass the GUI in when calling the method:

        //!! results = taskBckg.runDiagnostics();
        results = taskBckg.runDiagnostics(gui);

Also, please edit all your code so that it follows Java naming conventions. All class names should begin with a capital letter. This makes it much easier for others to understand what your code is doing.

这篇关于更新jlabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆