2015-07-20 90 views
0

所以,目前我是自學Java,我想用一個小按鈕和一個使用Swing的文本字段編寫一個簡單的計算器。雖然我設法實際創建了一個文本字段,其中填充了文本變量,但我無法使按鈕更改字段中的文本。我不斷收到關於內部類無法更改外部變量的錯誤。什麼是完成我的目標的最佳方式,爲什麼?使用內部類(Java)修改變量

在此先感謝,下面的代碼:

退房哪裏我的第一個按鈕的動作(Add按鈕)是,這就是我在測試更改文本字段的能力。

/** 
* Created by Ray on 7/19/2015. 
*/ 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class main extends JFrame { 
    double x = 0; 
    double y = 0; 
    double z = 0; 

    public main() { 
     initUI(); 
    } 

    private void initUI() { 

     setLayout(null); 

     JPanel text = new JPanel(); 
     text.setLayout(new BorderLayout()); 
     text.setBounds(100, 10, 200, 25); 

     JScrollPane pane = new JScrollPane(); 
     JTextArea area = new JTextArea(); 

     area.setLineWrap(true); 
     area.setWrapStyleWord(true); 
     area.setBounds(100, 10, 200, 25); 
     pane.getViewport().add(area); 
     text.add(pane); 
     String contents = "Test"; 
     area.setText(contents); 
     JButton addButton = new JButton("Add"); 
     addButton.setToolTipText("Addition operation."); 
     addButton.setBounds(10, 10, 80, 25); 
     addButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 
       contents = "Pressed"; 
      } 
     }); 
     JButton subButton = new JButton("Sub"); 
     subButton.setToolTipText("Subtraction operation."); 
     subButton.setBounds(10, 40, 80, 25); 
     subButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 
       System.exit(0); 
      } 
     }); 
     JButton mulButton = new JButton("Mul"); 
     mulButton.setToolTipText("Multiplication operation."); 
     mulButton.setBounds(10, 70, 80, 25); 
     mulButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 
       System.exit(0); 
      } 
     }); 
     JButton divButton = new JButton("Div"); 
     divButton.setToolTipText("Division operation."); 
     divButton.setBounds(10, 100, 80, 25); 
     divButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 
       System.exit(0); 
      } 
     }); 
     add(text); 
     add(addButton); 
     add(subButton); 
     add(mulButton); 
     add(divButton); 

     setTitle("Simple example"); 
     setSize(500, 300); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       main ex = new main(); 
       ex.setVisible(true); 
      } 
     }); 
    } 
} 
+0

只是變量'contents'是本地'initUI'方法,所以你只需要把它聲明爲'final'在使用之前\ –

+0

避免使用'null'佈局,像素完美的佈局是現代UI設計中的幻想。影響組件的個體大小的因素太多,其中沒有一個可以控制。 Swing旨在與佈局管理人員一起工作,放棄這些將導致無法結束的問題和問題,您將花費越來越多的時間來嘗試糾正 – MadProgrammer

+0

您是否嘗試過使用'area.append(「一些新文本」) ;'? – MadProgrammer

回答

0
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class HelloWorld extends JFrame { 
double x = 0; 
double y = 0; 
double z = 0; 
public HelloWorld() { 
    initUI(); 
} 


private void initUI() { 

    setLayout(null); 

    JPanel text = new JPanel(); 
    text.setLayout(new BorderLayout()); 
    text.setBounds(100, 10, 200, 25); 

    JScrollPane pane = new JScrollPane(); 
    JTextArea area = new JTextArea(); 

    area.setLineWrap(true); 
    area.setWrapStyleWord(true); 
    area.setBounds(100, 10, 200, 25); 
    pane.getViewport().add(area); 
    text.add(pane); 
    String contents1="Text"; 
    area.setText(contents1); 
    JButton addButton = new JButton("Add"); 
    addButton.setToolTipText("Addition operation."); 
    addButton.setBounds(10, 10, 80, 25); 
    addButton.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      final String contents = "Pressed"; 
     } 
    }); 
    JButton subButton = new JButton("Sub"); 
    subButton.setToolTipText("Subtraction operation."); 
    subButton.setBounds(10, 40, 80, 25); 
    subButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      System.exit(0); 
     } 
    }); 
    JButton mulButton = new JButton("Mul"); 
    mulButton.setToolTipText("Multiplication operation."); 
    mulButton.setBounds(10, 70, 80, 25); 
    mulButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      System.exit(0); 
     } 
    }); 
    JButton divButton = new JButton("Div"); 
    divButton.setToolTipText("Division operation."); 
    divButton.setBounds(10, 100, 80, 25); 
    divButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      System.exit(0); 
     } 
    }); 
    add(text); 
    add(addButton); 
    add(subButton); 
    add(mulButton); 
    add(divButton); 

    setTitle("Simple example"); 
    setSize(500, 300); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
} 



public static void main(String[] args) { 

    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      HelloWorld ex = new HelloWorld(); 
      ex.setVisible(true); 
     } 
    }); 
} 
} 

你只需要聲明兩個變量,我們可以使用此方法只最終局部變量內class.Or聲明一個變量最終並使用相同的名稱,文本和按鈕,你不要這不想要。 所以,如果你在外面定義了一個普通的字符串,你可以正常地將它用於文本字段,但是你不能在方法本地內部類中使用它。 如果你在外面定義了一個最終變量,只需將它添加到方法local inner class中,但不要試圖再次更改它,因爲它是Final。

0

如果您將delogle變量String contents作爲該類的成員,那麼您將不會面臨此問題。在代碼中,將其稱爲contents以便不覆蓋它,因此請從initGui()方法中刪除任何String contents = ...聲明。

public class main extends JFrame { 
double x = 0; 
double y = 0; 
double z = 0; 
String contents = "Test"; 

public main.... 
0

我建議你在實例和一個類(靜態)變量之間的差異讀了。

contentsmain的實例變量,這意味着您只能在main的上下文中引用它。創建新的ActionListener時,您正在創建新的Anonymous實例ActionListener,並嘗試從新創建的ActionListener而不是main的實例中訪問contents

您可以通過直接從ActionEvent訪問JButton解決您的問題:

addButton.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent event) { 
     Component comp = (Component) event.getSource(); 
     if (comp instanceof JButton) { 
      ((JButton)comp).setText("Pressed"); 
     }  
    } 
});