2015-10-19 73 views
1
public class string implements ActionListener{ 

    JLabel jlab; 

    public void actionPerformed(ActionEvent Ae){ 
     jlab.setText(Ae.getActionCommand()); 
    } 

     public static void main(String args[]){ 
      JFrame j = new JFrame("HP"); 
      j.setSize(300,300); 
      j.setLayout(new FlowLayout()); 
      j.setDefaultCloseOperation(j.EXIT_ON_CLOSE); 
      j.setVisible(true);  

     jlab=new JLabel("Here"); 
     j.add(jlab); 

      JButton j1=new JButton("Button1"); 
      j1.setActionCommand("Your pressed Button 1"); 

      j1.addActionListener(this); 

      j.add(j1); 

      JButton j2=new JButton("Button2"); 
      j2.setActionCommand("Your pressed Button 1"); 
      j2.addActionListener(this); 
      j.add(j2); 
    } 
    } 

我想用同一個EventHandler處理多個按鈕的操作。點擊按鈕時,標籤的文字將相應更改。用於多個按鈕的ActionListener-Swing

此代碼顯示錯誤

Non static variable this cannot be referenced from a static context. 

我怎樣才能糾正這種代碼?

+2

大部分在main方法的東西應該被移動到一個構造或非靜態'的init()'方法。這將解決眼前的問題 - 因爲「這個」將存在。 –

+0

參見http://stackoverflow.com/search?tab=relevance&q=%5bjava%5d%20Non%20static%20variable%20this%20cannot%20be%20referenced%20from%20a%20static%20context.%20 –

回答

1

有兩個解決問題的方法:

  1. 使用構造

    public class buttons1 implements ActionListener{ 
    
         JLabel jlab; 
    
         buttons1(){ 
    
    
           JFrame j = new JFrame("HP"); 
           j.setSize(300,300); 
           j.setLayout(new FlowLayout()); 
           j.setDefaultCloseOperation(j.EXIT_ON_CLOSE); 
           j.setVisible(true);  
    
           jlab=new JLabel("Here"); 
           j.add(jlab); 
    
           JButton j1=new JButton("Button1"); 
           j1.setActionCommand("Your pressed Button 1"); 
    
           j1.addActionListener(this); 
    
           j.add(j1); 
    
           JButton j2=new JButton("Button2"); 
           j2.setActionCommand("Your pressed Button 2"); 
           j2.addActionListener(this); 
           j.add(j2); 
    
    
         } 
    
         public void actionPerformed(ActionEvent Ae){ 
    
            jlab.setText(Ae.getActionCommand()); 
    
         } 
    
    
    
         public static void main(String args[]){ 
          new buttons1(); 
         } } 
    
  2. 或者內的主要創建同一個類的對象,並把它傳遞給addActionListener方法。也使JLabel靜態。

    public class buttons2 implements ActionListener{ 
        static JLabel jlab; 
    
        public void actionPerformed(ActionEvent Ae){ 
    
              jlab.setText(Ae.getActionCommand()); 
    
             } 
    
    
    
        public static void main(String args[]){ 
    
         buttons2 s = new buttons2(); 
    
         JFrame j = new JFrame("HP"); 
          j.setSize(300,300); 
          j.setLayout(new FlowLayout()); 
          j.setDefaultCloseOperation(j.EXIT_ON_CLOSE); 
          j.setVisible(true);  
    
          jlab=new JLabel("Here"); 
          j.add(jlab); 
    
          JButton j1=new JButton("Button1"); 
          j1.setActionCommand("Your pressed Button 1"); 
    
          j1.addActionListener(s); 
    
          j.add(j1); 
    
          JButton j2=new JButton("Button2"); 
          j2.setActionCommand("Your pressed Button 2"); 
          j2.addActionListener(s); 
          j.add(j2); 
         } 
        } 
    
0

瞭解什麼是static變量以及它的工作原理非常重要。靜態變量不能從非靜態上下文中引用。

import javax.swing.JLabel; 
import javax.swing.JFrame; 
import javax.swing.JButton; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import java.awt.FlowLayout; 

public class Stackoverflow implements ActionListener{ 

    private JLabel jlab; /* It's a good practice to make your variables private or protected */ 



    public static void main(String args[]){ 
     new Stackoverflow(); 
    } 
    public Stackoverflow(){ 
      JFrame j = new JFrame("HP"); 
      j.setSize(300,300); 
      j.setLayout(new FlowLayout()); 
      j.setDefaultCloseOperation(j.EXIT_ON_CLOSE); 
      j.setVisible(true);  

      jlab=new JLabel("Here"); 
      j.add(jlab); 

      JButton j1=new JButton("Button1"); 
      j1.setActionCommand("Your pressed Button 1"); 

      j1.addActionListener(this); 

      j.add(j1); 

      JButton j2=new JButton("Button2"); 
      j2.setActionCommand("Your pressed Button 2"); 
      j2.addActionListener(this); 
      j.add(j2); 
    } 
    public void actionPerformed(ActionEvent Ae){ 
     jlab.setText(Ae.getActionCommand()); 
    } 
}