import java.awt.*;
import java.awt.event.*;
public class TestListButton {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyListButton ("下拉表与按钮");
}
}
class MyListButton extends Frame implements ItemListener {
private static final long serialVersionUID = 1L;
String sl;
List l;
Button b;
MyListButton (String s) {
super (s);
this.setLayout(new FlowLayout());
this.setBounds(500, 500, 600, 300);
l = new List(3,false);
l.add("10");
l.add("14");
l.add("18");
b = new Button("10");
l.addItemListener(this);
add(l);
add(b);
this.setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
sl = e.paramString();
b.setLabel(sl);
}
}
本来想让下拉表中选择几,按钮上的文字就变成几的。结果当下拉表中数字改变时,按钮中对应的变成了em=0,emm=1,em=2。麻烦帮忙改一下啊!谢谢了。
------最佳解决方案--------------------------------------------------------
断个点,调试一下。
就知道问题在那了,别人帮忙改,没啥进步的
------其他解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class TestListButton {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MyListButton("下拉表与按钮").setVisible(true);
}
});
}
}
class MyListButton extends Frame implements ItemListener {
private static final long serialVersionUID = 1L;
String sl;
List l;
Button b;
MyListButton(String s) {
super(s);
this.setLayout(new FlowLayout());
this.setBounds(500, 500, 600, 300);
l = new List(3, false);
l.add("10");
l.add("14");
l.add("18");
b = new Button("10");
l.addItemListener(this);
add(l);
add(b);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
@Override
public void itemStateChanged(ItemEvent e) {
b.setLabel(l.getSelectedItem());