我有一段这样的代码,我正在尝试使用按钮来访问 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();
}
}
}
我的代码中出现以下错误,我不明白为什么...
- 错误找不到符号字符串名称 = textfield.getText();
- 错误找不到符号 textarea.append(name);
- 错误找不到符号 textfield.selectAll();