2
import javax.swing.*;

public class Test {
   public static void main(String[] args) {
     JButton jbtOK = new JButton("OK");
     JButton jbtCancel = new JButton("Cancel");
     JLabel jlblName = new JLabel("Enter your name: ");
     JTextField jtfName = JTextField("Type Name Here");
     JCheckBox jchkBold = new JCheckBox("Bold");
     JCheckBox jchkItalic = new JCheckBox("Italic");
     JRadioButton jrbRed = new JRadioButton("Red");
     JRadioButton jrbYellow = new JRadioButton("Red");
     JComboBox jcboColor = new JComboBox(new String[] {"Freshman",
       "Sophmore", "Junior", "Senior"});
     JPanel panel = new JPanel();
     panel.add(jbtOK);
     panel.add(jbtCancel);
     panel.add(jlblName);
     panel.add(jtfName);
     panel.add(jchkBold);
     panel.add(jchkItalic);
     panel.add(jrbRed);
     panel.add(jrbYellow);
     panel.add(jcboColor);

    JFrame frame = new JFrame();
    frame.add(panel);
    frame.setTitle("Show GUI Components");
    frame.setSize(450, 100);
    frame.setLocation(200, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

我尝试编译时得到的输出

gurbhej@Nanda:~/Dropbox/Programming/Java Programs/Test$ javac Test.java 
Test.java:9: error: cannot find symbol
JTextField jtfName = JTextField("Type Name Here");
                     ^
symbol:   method JTextField(String)
location: class Test
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
gurbhej@Nanda:~/Dropbox/Programming/Java Programs/Test$ 

我从一本书中得到了这个例子,也没有从勘误表中找到任何东西。为什么我会收到此错误?这是我第一次在 Java 中使用 GUI 的东西,所以我不知道如何解决这个问题。JTextField 是否甚至存在于 Java 库中,还是我缺少某些东西?

4

1 回答 1

10

new您之前忘记了JTextField,因此代码将是:

JTextField jtField = new JTextField("");
于 2013-06-19T00:07:39.773 回答