java之事件实现问题
publicclassTtemeventDemoextendsJFrameimplementsItemListener,ActionListener{JRadioButt...
public class TtemeventDemo extends JFrame implements ItemListener,ActionListener{
JRadioButton opt1,opt2;
ButtonGroup btg;
JTextArea ta;
JComboBox comb;
JLabel sex,city;
public TtemeventDemo(String title){
super(title);
setLayout(new FlowLayout(FlowLayout.LEFT);
set=new JLbel(“性别”);
city=new JLabel(“籍贯”);
opt1=new JRadioButton(“男”);
opt2=new JRadioButton(“女”);
btg=new ButtonGroup();
btg.add(opt1);
btg.add(opt2);
opt1.addItemListener(this);
opt2.addItemListener(this);
ta=newJTextArea(8,35);
comb=new JComboBox();
comb.addItem(“北京”);
comb.addItem(“上海”);
comb.addItem(“南京”);
comb.addItem(“广州”);
comb.addItem(“成都”);
comb.addItem(“昆明”);
comb.addItemListener(this);
comb.addActionListener(this);
getContentPane().add(sex);
getContentPane().add(opt1);
getContentPane().add(opt2);
getContentPane().add(city);
getContentPane().add(comb);
getContentPane().add(ta);
setTitle(title);
setSize(300,250);
setVisible(true);
}
public static void main(String args[]){
new TtemeventDemo ("ItemeventDemo");
}
public void itemStateChanged(ItemEvent e){
String str;
if(e.getSource()==opt1)
ta.append(“\n性别:+男);
else if(e.getSource()==opt2)
ta.append(“\n性别:+女);
if(e.getSource()==comb)
{str=comb.getSelectedItem().toString();
ta.append(.........(不想写));
}
}
public void actionPerformed(ActionEvent e){
String str;
if(e.getSource()==comb){
str=comb.getSelectedItem().toString();
ta.append(.......); 展开
JRadioButton opt1,opt2;
ButtonGroup btg;
JTextArea ta;
JComboBox comb;
JLabel sex,city;
public TtemeventDemo(String title){
super(title);
setLayout(new FlowLayout(FlowLayout.LEFT);
set=new JLbel(“性别”);
city=new JLabel(“籍贯”);
opt1=new JRadioButton(“男”);
opt2=new JRadioButton(“女”);
btg=new ButtonGroup();
btg.add(opt1);
btg.add(opt2);
opt1.addItemListener(this);
opt2.addItemListener(this);
ta=newJTextArea(8,35);
comb=new JComboBox();
comb.addItem(“北京”);
comb.addItem(“上海”);
comb.addItem(“南京”);
comb.addItem(“广州”);
comb.addItem(“成都”);
comb.addItem(“昆明”);
comb.addItemListener(this);
comb.addActionListener(this);
getContentPane().add(sex);
getContentPane().add(opt1);
getContentPane().add(opt2);
getContentPane().add(city);
getContentPane().add(comb);
getContentPane().add(ta);
setTitle(title);
setSize(300,250);
setVisible(true);
}
public static void main(String args[]){
new TtemeventDemo ("ItemeventDemo");
}
public void itemStateChanged(ItemEvent e){
String str;
if(e.getSource()==opt1)
ta.append(“\n性别:+男);
else if(e.getSource()==opt2)
ta.append(“\n性别:+女);
if(e.getSource()==comb)
{str=comb.getSelectedItem().toString();
ta.append(.........(不想写));
}
}
public void actionPerformed(ActionEvent e){
String str;
if(e.getSource()==comb){
str=comb.getSelectedItem().toString();
ta.append(.......); 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询