我只想创建一个JComboBox。并且,我将以下代码粘贴到https://www.compilejava.net/中,窗口中没有显示任何内容。那么,代码有什么问题?或者在线编译器有问题吗?如果是这样,有没有可用的在线编译器?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test{
private JFrame f; //window
private JPanel p; //layout
private JComboBox<String> j; //pull down menu
public static void main(String[] args){
new test();
}
public test(){
f = new JFrame("title of window");
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
p.setBackground(Color.YELLOW);
String[] bookTitles = new String[] {"Effective Java", "Head First Java",
"Thinking in Java", "Java for Dummies"};
JComboBox<String> bookList = new JComboBox<>(bookTitles);
p.add(bookList);
f.add(p);
}
}