当前位置: 代码迷 >> J2EE >> 大家看看 小弟我是初学者
  详细解决方案

大家看看 小弟我是初学者

热度:233   发布时间:2016-04-22 02:41:16.0
大家看看 我是菜鸟
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class WindowChoice extends JFrame implements ItemListener,ActionListener{
JComboBox choice;
JTextField text;
JTextArea area;
JButton add,del;
public WindowChoice(){
init();
setLocation(300,300);
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
choice = new JComboBox();
text = new JTextField();
area = new JTextArea(10,10);
choice.addItem("音乐天地");
choice.addItem("网络游戏");
choice.addItem("单机游戏");
choice.addItem("聊天室");
add = new JButton("添加");
add = new JButton("删除");
add.addActionListener(this);
text.addActionListener(this);
del.addActionListener(this);
choice.addItemListener(this);
add(choice);
add(text);
add(add);
add(new JScrollPane(area));
}
public void itemStateChanged(ItemEvent e){
String name = choice.getSelectedItem().toString();
int index = choice.getSelectedIndex();
area.setText("\n",+index+":"+name);

}
public void actionPerformed(ActionEvent e){
if(e.getSource() == add || e.getSource() == text){
String name = text.getText();
if(name.length()>0){
choice.addItem(name);
choice.setSelectedItem(name);
area.append("\n列表添加:"+name);

}

}
else if(e.getSource() == del){
area.append("\n列表删除:"+choice.getSelectedItem());
choice.remove(choice.getSelectedIndex());
}
}
}
这里出了什么问题啊 为什么不能运行 啊

------解决方案--------------------
你代码里面有两处错误,还没写main函数,改成下面就可以了
Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class WindowChoice extends JFrame implements ItemListener,ActionListener{    JComboBox choice;    JTextField text;    JTextArea area;    JButton add,del;    public WindowChoice(){        init();        setLocation(300,300);        setSize(800,600);        setVisible(true);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    void init(){        setLayout(new FlowLayout());        choice = new JComboBox();        text = new JTextField();        area = new JTextArea(10,10);        choice.addItem("音乐天地");        choice.addItem("网络游戏");        choice.addItem("单机游戏");        choice.addItem("聊天室");        add = new JButton("添加");        del = new JButton("删除");//这里有修改过        add.addActionListener(this);        text.addActionListener(this);        del.addActionListener(this);        choice.addItemListener(this);        add(choice);        add(text);        add(add);        add(new JScrollPane(area));    }    public void itemStateChanged(ItemEvent e){        String name = choice.getSelectedItem().toString();        int index = choice.getSelectedIndex();        area.setText("\n"+index+":"+name);  //这里修改过    }    public void actionPerformed(ActionEvent e){        if(e.getSource() == add || e.getSource() == text){            String name = text.getText();            if(name.length()>0){                choice.addItem(name);                choice.setSelectedItem(name);                area.append("\n列表添加:"+name);            }        }        else if(e.getSource() == del){            area.append("\n列表删除:"+choice.getSelectedItem());            choice.remove(choice.getSelectedIndex());        }    }    public static void main(String[] args) {        new WindowChoice();    }}
------解决方案--------------------
  相关解决方案