2012-04-08 32 views
1

我有一塊這樣的,在這裏我想使用按鈕來訪問一個JTextField代碼...使用文本字段和按鈕來初始化變量

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

public class NameGameFrame extends JFrame 
{ 
    public static void main(String[] args) 
    { 

     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setTitle("Name Game"); 
     frame.setLocation(500,400); 
     frame.setSize(500,500); 

     JPanel panel = new JPanel(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 

     JLabel label = new JLabel("Enter the Name or Partial Name to search:"); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.insets = new Insets(2,10,10,10); 

     panel.add(label,c); 

     JTextArea textarea = new JTextArea(5,30); 
     panel.add(textarea); 

     JTextField textfield = new JTextField(20); 

     JButton button = new JButton("Search"); 
     c.gridx = 1; 
     c.gridy = 1; 
     panel.add(button,c); 

     panel.add(textfield); 

     frame.getContentPane().add(panel, BorderLayout.NORTH); 
     frame.setVisible(true); 

    } 
    static class Action implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      String name = textfield.getText(); 
      textarea.append(name); 
      textfield.selectAll(); 
     } 
    } 
} 

我收到以下錯誤在我的代碼中,我不明白爲什麼...

  1. 錯誤找不到符號String name = textfield.getText();
  2. 錯誤找不到符號textarea.append(name);
  3. 錯誤找不到符號textfield.selectAll();
+0

關於'pack()/ setVisible()'的代碼應該進入[Event Dispatcher Thread](http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html#exceptions) – 2012-04-08 06:05:30

回答

4

我認爲這些錯誤的所有3的原因是相同的 - 你需要移動的變量出更高的水平,使這兩種方法都可以訪問他們...

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

public class NameGameFrame extends JFrame 
{ 

    // Moved these 2 variables to be class-level 
    static JTextField textfield = new JTextField(20); 
    static JTextArea textarea = new JTextArea(5,30); 

    public static void main(String[] args) 
    { 

     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setTitle("Name Game"); 
     frame.setLocation(500,400); 
     frame.setSize(500,500); 

     JPanel panel = new JPanel(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 

     JLabel label = new JLabel("Enter the Name or Partial Name to search:"); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.insets = new Insets(2,10,10,10); 

     panel.add(label,c); 

     panel.add(textarea); 

     JButton button = new JButton("Search"); 
     c.gridx = 1; 
     c.gridy = 1; 
     panel.add(button,c); 

     panel.add(textfield); 

     frame.getContentPane().add(panel, BorderLayout.NORTH); 
     frame.setVisible(true); 

    } 
    static class Action implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      String name = textfield.getText(); 
      textarea.append(name); 
      textfield.selectAll(); 
     } 
    } 
} 

與Java和大多數其他語言,請確保在嘗試解決其他語言之前解決第一個錯誤 - 通常第一個錯誤可能會在以後導致其他錯誤。

+0

是正確的,比我快:) .. – SenthilPrabhu 2012-04-08 01:25:09

+0

謝謝,我一直得到一個非靜態變量現在不能從靜態上下文錯誤引用,這些更改。 – 2012-04-08 01:30:32

+0

您需要使變量靜態 - 請參閱上面編輯的代碼。 – wattostudios 2012-04-08 01:32:04

2

該聲明和使用在不同的功能; 所以你不能訪問它。