简单程序停止编译过程的 Java 错误,请帮助
我是这里的初学者,但我处理这个未知问题已经有一段时间了。请查看我编写的这个简单程序并解释为什么它不能为我编译。问题是我从一位老师那里复制了这段代码,并且在他的机器上运行良好。然后,当我尝试在我的机器上运行该示例时,出现 13 个错误!
这是第一堂课:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class radio extends JFrame{
private JTextField tf;
private Font pf;
private Font bf;
private Font itf;
private Font bif;
private JRadioButton pb;
private JRadioButton bb;
private JRadioButton ib;
private JRadioButton bib;
private ButtonGroup group;
public radio(){
super("raido buttonseses");
setLayout(new FlowLayout());
tf = new JTextField("buggedy buggedy boo", 25);
add(tf);
pb = new JRadioButton("plain", true);
bb = new JRadioButton("bold", false);
ib = new JRadioButton("italic", false);
bib = new JRadioButton("bold and italic", false);
add(pb);
add(bb);
add(ib);
add(bib);
group = new ButtonGroup();
group.add(pb);
group.add(bb);
group.add(ib);
group.add(bib);
pf = new Font("Serif", Font.PLAIN, 14);
bf = new Font("Serif", Font.BOLD, 14);
itf = new Font("Serif", Font.ITALIC, 14);
bif = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
tf.setFont(pf);
//wait for event to happen pass in font obj to constructor
pb.addItemListener(new HandlerClass(pf));
bb.addItemListener(new HandlerClass(bf));
ib.addItemListener(new HandlerClass(itf));
bib.addItemListener(new HandlerClass(bif));
}
private class HandlerClass implements ItemListener{
private Font font;
// font obj gets variable font
public HandlerClass(Font f){
font = f;
}
//sets font to font obj that was passed in
public void itemStateChanged(ItemEvent event){
tf.setFont(font);
}
}
}
然后这是我一直在尝试运行的第二个主要课程:
import javax.swing.JFrame;
public class radiobutton{
public static void main(String[] args) {
radio go = new radio();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}
我知道有人正在看这个并认为答案是如此明显,但对于像我这样的初学者来说,它并不是很清楚。以下是我尝试编译时返回的错误:
javac radiobutton.java
./JFrame.java:1: JFrame is already defined in this compilation unit
import javax.swing.JFrame;
^
radiobutton.java:7: cannot find symbol
symbol : method setDefaultCloseOperation(int)
location: class radio
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
^
radiobutton.java:8: cannot find symbol
symbol : method setSize(int,int)
location: class radio
go.setSize(300,200);
^
radiobutton.java:9: cannot find symbol
symbol : method setVisible(boolean)
location: class radio
go.setVisible(true);
^
./JFrame.java:8: cannot find symbol
symbol : variable EXIT_ON_CLOSE
location: class JFrame
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
^
./radio.java:19: cannot find symbol
symbol : constructor JFrame(java.lang.String)
location: class JFrame
super("raido buttonseses");
^
./radio.java:20: cannot find symbol
symbol : method setLayout(java.awt.FlowLayout)
location: class radio
setLayout(new FlowLayout());
^
./radio.java:22: cannot find symbol
symbol : class JTExtField
location: class radio
tf = new JTExtField("buggedy buggedy boo", 25);
^
./radio.java:23: cannot find symbol
symbol : method add(javax.swing.JTextField)
location: class radio
add(tf);
^
./radio.java:30: cannot find symbol
symbol : method add(javax.swing.JRadioButton)
location: class radio
add(pb);
^
./radio.java:31: cannot find symbol
symbol : method add(javax.swing.JRadioButton)
location: class radio
add(bb);
^
./radio.java:32: cannot find symbol
symbol : method add(javax.swing.JRadioButton)
location: class radio
add(ib);
^
./radio.java:33: cannot find symbol
symbol : method add(javax.swing.JRadioButton)
location: class radio
add(bib);
^
13 errors
我的猜测是这与 java 函数导入中的星号有关。我走在正确的轨道上吗?感谢您查看这个愚蠢的问题,非常感谢您的帮助。
I am a beginner here, but I have been dealing with this unknown issue for a while now. Please view this simple program that I have coded and explain why it will not compile for me. The thing is that I have copied this code from a teacher and it works fine on his machine. Then when I tried to run the example on my machine I get 13 errors!
Here is the first class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class radio extends JFrame{
private JTextField tf;
private Font pf;
private Font bf;
private Font itf;
private Font bif;
private JRadioButton pb;
private JRadioButton bb;
private JRadioButton ib;
private JRadioButton bib;
private ButtonGroup group;
public radio(){
super("raido buttonseses");
setLayout(new FlowLayout());
tf = new JTextField("buggedy buggedy boo", 25);
add(tf);
pb = new JRadioButton("plain", true);
bb = new JRadioButton("bold", false);
ib = new JRadioButton("italic", false);
bib = new JRadioButton("bold and italic", false);
add(pb);
add(bb);
add(ib);
add(bib);
group = new ButtonGroup();
group.add(pb);
group.add(bb);
group.add(ib);
group.add(bib);
pf = new Font("Serif", Font.PLAIN, 14);
bf = new Font("Serif", Font.BOLD, 14);
itf = new Font("Serif", Font.ITALIC, 14);
bif = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
tf.setFont(pf);
//wait for event to happen pass in font obj to constructor
pb.addItemListener(new HandlerClass(pf));
bb.addItemListener(new HandlerClass(bf));
ib.addItemListener(new HandlerClass(itf));
bib.addItemListener(new HandlerClass(bif));
}
private class HandlerClass implements ItemListener{
private Font font;
// font obj gets variable font
public HandlerClass(Font f){
font = f;
}
//sets font to font obj that was passed in
public void itemStateChanged(ItemEvent event){
tf.setFont(font);
}
}
}
Then here is the second main class that I have been trying to run:
import javax.swing.JFrame;
public class radiobutton{
public static void main(String[] args) {
radio go = new radio();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}
I know someone is looking at this and thinking that the answer is so obvious, but to a beginner like me it is not very clear. Here are the errors that I have returned to me when I try to compile:
javac radiobutton.java
./JFrame.java:1: JFrame is already defined in this compilation unit
import javax.swing.JFrame;
^
radiobutton.java:7: cannot find symbol
symbol : method setDefaultCloseOperation(int)
location: class radio
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
^
radiobutton.java:8: cannot find symbol
symbol : method setSize(int,int)
location: class radio
go.setSize(300,200);
^
radiobutton.java:9: cannot find symbol
symbol : method setVisible(boolean)
location: class radio
go.setVisible(true);
^
./JFrame.java:8: cannot find symbol
symbol : variable EXIT_ON_CLOSE
location: class JFrame
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
^
./radio.java:19: cannot find symbol
symbol : constructor JFrame(java.lang.String)
location: class JFrame
super("raido buttonseses");
^
./radio.java:20: cannot find symbol
symbol : method setLayout(java.awt.FlowLayout)
location: class radio
setLayout(new FlowLayout());
^
./radio.java:22: cannot find symbol
symbol : class JTExtField
location: class radio
tf = new JTExtField("buggedy buggedy boo", 25);
^
./radio.java:23: cannot find symbol
symbol : method add(javax.swing.JTextField)
location: class radio
add(tf);
^
./radio.java:30: cannot find symbol
symbol : method add(javax.swing.JRadioButton)
location: class radio
add(pb);
^
./radio.java:31: cannot find symbol
symbol : method add(javax.swing.JRadioButton)
location: class radio
add(bb);
^
./radio.java:32: cannot find symbol
symbol : method add(javax.swing.JRadioButton)
location: class radio
add(ib);
^
./radio.java:33: cannot find symbol
symbol : method add(javax.swing.JRadioButton)
location: class radio
add(bib);
^
13 errors
My guess is that this has something to do with the asterisk in the import of the java functions. Am I on the right track here? Thank you for having a look at this silly problem and any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个盲目的尝试,因为我已经有一段时间没有接触Java了,但是你是在编译radio.java,然后编译radiobutton.java吗?
This is a shot in the dark as I haven't touched Java for a while, but are you compiling radio.java, and then compiling radiobutton.java?
此错误消息引用另一个源文件 (JFrame.java)。就像 aioobe 一样,我打赌您在同一目录(包)中有一个名为 JFrame.java 的文件。
要进行测试,请尝试以下代码:
This error message refers to another source file (JFrame.java). Like aioobe I bet that you have a file named JFrame.java in the same directory (package).
To test, try this code:
它对我来说编译得很好。
错误可能是这样的:您的源目录中有另一个名为
JFrame.java
的文件。删除这个文件! JFrame 已在 API 中定义。错误消息...
...表明
./JFrame.java
与radiobutton
存在于同一包中。(作为旁注,我想提一下,根据 Java 编码约定,您应该始终以大写字母开头您的类名。)
It compiles just fine for me.
The error is probably this: You have another file called
JFrame.java
in your source directory. Remove this file! JFrame is already defined in the API.The error message...
...reveals that
./JFrame.java
exists in the same package asradiobutton
.(As a side note I want to mention that according to the Java coding conventions, you should always start your class names with a capital letter.)