我已经成功编译并运行了以下代码,但是小程序窗口除了空白之外什么都没有显示,可能是什么问题?
我不想在这里创建一个小程序我想创建一个 Jframe 程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//<applet code="calc.class" width=400 height=200></applet>
public class calc extends JFrame implements ActionListener {
JFrame f1;
JPanel p1 = new JPanel();
JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15,
l16, l17, l18, l19, l20, l21, l22, l23, l24;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15,
b16;
JTextField t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11;
public void init() {
f1 = new JFrame("Administrator");
p1.setLayout(new GridLayout(3, 2));
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(b1);
p1.add(b2);
f1.add(p1);
f1.setSize(300, 300);
f1.setVisible(true);
}
public void actionPerformed(ActionEvent AE) {
}
public void main(String[] args) {
calc s = new calc();
s.init();
}
}
在 CMD 窗口中,我得到以下异常
java.lang.ClassCastException: calc cannot be cast to java.applet.Applet
at sun.applet.AppletPanel.createApplet(AppletPanel.java:795)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:724)
at sun.applet.AppletPanel.run(AppletPanel.java:378)
at java.lang.Thread.run(Thread.java:722)
编辑
根据您的评论,我添加了一个主类并将文件名更改为cool.java,但它仍然无法正常工作
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//<applet code="calc.class" width=400 height=200></applet>
public class calc extends JFrame implements ActionListener
{
JFrame f1;
JPanel p1=new JPanel();
JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15,l16,l17,l18,l19,l20,l21,l22,l23,l24;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11;
public void init()
{
f1=new JFrame("Administrator");
p1.setLayout(new GridLayout(3,2));
p1.add(l1);p1.add(t1);p1.add(l2);
p1.add(b1);p1.add(b2);
f1.add(p1);
f1.setSize(300,300);
f1.setVisible(true);
}
public void actionPerformed(ActionEvent AE)
{
}
}
public class cool
{
public static void main(String[]args)
{
calc s=new calc();
s.init();
}
}