51,410
社区成员
发帖
与我相关
我的任务
分享public class TestFrame extends JFrame implements ItemListener {
JTextField textfield;
public TestFrame() {
super();
String money[] = { "5", "10", "20", "50" };// 定义字符串数组
JComboBox combobox = new JComboBox(money);// 实例化组合框对象
combobox.addItemListener(this);// 添加监听器
textfield = new JTextField(10);// 实例化文本框对象
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
c.add(combobox, BorderLayout.NORTH);
c.add(textfield, BorderLayout.SOUTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
@Override
public void itemStateChanged(ItemEvent e) {
String t_2_money = String.valueOf(e.getItem());// 将Object转化为字符串
int t = Integer.parseInt(t_2_money) * 72; // 将字符串转化为整型乘以72后赋值给t
textfield.setText(String.valueOf(t)); // 将t转化为字符串并在文本框中显示出来
}
public static void main(String[] args) {
new TestFrame();
}
}