我是Java的新手,我正在开发一个简单的applet作为家庭作业项目...我编译它没有任何语法错误,但它不会显示在Main中除了空白屏幕和“小程序开始“。请问有关如何修复它的一些校对/建议吗?感谢
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class Module6Project extends JApplet
{
public static void main (String[] args) {
JFrame f=new JFrame("Module6Project");
f.setBackground(Color.blue);
f.getContentPane().setLayout(null);
f.setSize(500,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField first = new JTextField("First name", 150);
JTextField second = new JTextField("Last name", 200);
JTextField third = new JTextField("DoB", 75);
JTextField fourth = new JTextField("Address", 350);
JTextField fifth = new JTextField("Additional comments", 250);
JButton OK = new JButton("OK");
JButton Cancel = new JButton("Cancel");
}}
答案 0 :(得分:1)
main()
。接下来,你有一个Applet(你不想要一个JFrame)。最后,您需要将组件添加到容器中。我通常使用JPanel
,但您可以使用JApplet
或JFrame
。
class Module6Project extends JApplet {
@Override
public void init() {
// Not a new Frame, "this" applet.
this.setBackground(Color.blue);
this.getContentPane().setLayout(null);
// this.setSize(500, 500);
JTextField first = new JTextField("First name",
150);
JTextField second = new JTextField("Last name",
200);
JTextField third = new JTextField("DoB", 75);
JTextField fourth = new JTextField("Address", 350);
JTextField fifth = new JTextField(
"Additional comments", 250);
JButton OK = new JButton("OK");
JButton Cancel = new JButton("Cancel");
// add everything to the container (or it won't be visible)
this.add(first);
this.add(second);
this.add(third);
this.add(fourth);
this.add(fifth);
this.add(OK);
this.add(Cancel);
}
}