2017-08-15 58 views
1

我只想在jlabel计数。我想,我已经尝试了网站上发布的所有解决方案,但是我找不到任何解决方案。我是初学者,有一个月可以学习Java。如果我的问题太愚蠢,我很抱歉。为什么我不能在JLabel中更改变量?

package asdf; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.JLabel; 

public class asd extends JFrame implements ActionListener { 
int a=0; // variable 
private JFrame frame; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       asd window = new asd(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public asd() { 
    super(); 
    Timer time=new Timer(1000, this); 
    time.start(); 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 

    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    JPanel panel = new JPanel(); 
    panel.setBounds(20, 11, 137, 111); 
    frame.getContentPane().add(panel); 
    panel.setLayout(null); 

    ***JLabel Jtable = new JLabel(); 
    Jtable.setBounds(0, 25, 127, 58); 
    Jtable.setText("" + a); 
    panel.add(Jtable);*** 

    System.out.println(a); //it is counting on console but in Jlabel variable is not. 

} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    a++; 
    initialize(); 
} 
} 

我只想在jlabel计数。我想,我已经尝试了网站上发布的所有解决方案,但是我找不到任何解决方案。我是初学者,有一个月可以学习Java。如果我的问题太愚蠢,我很抱歉。

+0

什么是您想要更改的变量的**名称**? –

+0

我建议你只在类加载时调用initialize()方法一次。然后将您的Jtable.setText()移动到actionPerformed()方法。 尽管你的类正在扩展JFrame的子类,所以“this”应该是JFrame的一个实例;这意味着您可以在initialize()方法中使用诸如“this.setBounds()”之类的语句。 此外,最好的做法是以小写字母开头命名变量。 [jTable而不是Jtable] – Jeremy

+0

Hi @ Eee请遵循http://www.oracle.com/technetwork/java/codeconventions-135099.html – abcOfJavaAndCPP

回答

0

您不应该初始化新框架和所有执行的操作的组件,您应该只更新标签的文本。您可以通过JLabel.setText https://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setText(java.lang.String)

int a = 0; // variable 
private JFrame frame; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       asd window = new asd(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public asd() { 
    super(); 
    Timer time=new Timer(1000, this); 
    time.start(); 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 

private JLabel label; 
private void initialize() { 

    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    JPanel panel = new JPanel(); 
    panel.setBounds(20, 11, 137, 111); 
    frame.getContentPane().add(panel); 
    panel.setLayout(null); 

    label = new JLabel(); 
    label.setBounds(0, 25, 127, 58); 
    label.setText("" + a); 
    panel.add(label); 

    System.out.println(a); //it is counting on console but in Jlabel variable is not. 

} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    a++; 

    label.setText("" + a); 

} 
+0

亿次指定的Java命名约定,感谢您的回答。我之前尝试过,出现错误:“标签无法解析”。我整整一天都在放松。整天我工作写它:“标签=新JLabel();”十亿倍感谢你。你是英雄。你是真正的乔恩斯诺。 – Eee

+0

@Eee没问题。如果您对此感到满意,请将此标记为接受的答案。 – user

相关问题