2017-08-15 61 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

相關問題