标题: 怎样才能解决? [打印本页] 作者: 王新年 时间: 2014-1-7 16:34 标题: 怎样才能解决? 这个程序我在CMD里用appletviewer运行,结果有错误,提示是ComboBoxDemo.class不是public对象或没有公共构造函数!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Example64{
public static void main(String[] args)
{ ComboBoxDemo myComboBoxGUI=new ComboBoxDemo();
}
}
class ComboBoxDemo extends JFrame implements ActionListener,ItemListener{
public static final int Width=350;
public static final int Height=150;
String porList[]={"踢足球","打篮球","打排球"};
JTextField text;
JComboBox comboBox;
public ComboBoxDemo() {
setSize(Width,Height);
setTitle("组合框使用示意程序");
Container conPane=getContentPane();
conPane.setBackground(Color.BLUE);
conPane.setLayout(new FlowLayout());
comboBox=new JComboBox(porList);
comboBox.addActionListener(this);
comboBox.addItemListener(this);
comboBox.setEditable(true);
conPane.add(comboBox);
text=new JTextField();
conPane.add(text);
comboBox.setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==comboBox)
{
text.setText(comboBox.getSelectedItem().toString());
}
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==comboBox)
{
text.setText(comboBox.getSelectedItem().toString());
}
}
}
我的html文件是这么写的:
<html>
<body>
<applet
code="ComboBoxDemo.class" width=320 height=180>
</applet>
</body>
</html>
求技术分作者: 零敢 时间: 2014-1-7 16:43
class Example64 {
public static void main(String[] args) {
ComboBoxDemo myComboBoxGUI = new ComboBoxDemo();
}
}
class ComboBoxDemo extends JFrame implements ActionListener, ItemListener {
public static final int Width = 350;
public static final int Height = 150;
String porList[] = {"踢足球", "打篮球", "打排球"};
JTextField text;
JComboBox comboBox;
public ComboBoxDemo() {
setSize(Width, Height);
setTitle("组合框使用示意程序");
Container conPane = getContentPane();
conPane.setBackground(Color.BLUE);
conPane.setLayout(new FlowLayout());
comboBox = new JComboBox(porList);
comboBox.addActionListener(this);
comboBox.addItemListener(this);
comboBox.setEditable(true);
conPane.add(comboBox);
text = new JTextField(5);
conPane.add(text);
comboBox.setVisible(true);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == comboBox) {
text.setText(comboBox.getSelectedItem().toString());
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == comboBox) {
text.setText(comboBox.getSelectedItem().toString());
}
}
}
该这样就好了,frame要setVisible(true)才显示。textfield创建要设置长度。好像这个问题我以前回答过一样的问题。