70 likes | 236 Views
Event Handling on List . private class ListListener implements ListSelectionListener { public void valueChanged ( ListSelectionEvent e) { msg = (String) scrollingList.getSelectedValue (); } }. String bookList [] = {" Doraemon ",
E N D
Event Handling on List private class ListListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { msg = (String) scrollingList.getSelectedValue(); } } String bookList[] = {" Doraemon ", " A-Ra-Re ", " DragonBall ", " BirdLand ", " Pung-Pong "}; JListscrollingList = new JList(bookList); scrollingList.setVisibleRowCount(4); scrollingList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); p.add(new JScrollPane(scrollingList)); scrollingList.addListSelectionListener (new ListListener()); 1
private class RadioButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == n1) { day = "1 วัน"; baht = 10.0f; } if (source == n2) { day = "3 วัน"; baht = 30.0f; } } Event Handling on List ButtonGroupbg = new ButtonGroup(); JRadioButton n1 = new JRadioButton("1 day"); bg.add(n1); JRadioButton n2 = new JRadioButton("3 day"); bg.add(n2); p.add(n1); p.add(n2); n1.addActionListener(new RadioButtonListener()); n2.addActionListener(new RadioButtonListener()); 2
private class ComboListener implements ActionListener { public void actionPerformed(ActionEvent e) { int idx = mcombo.getSelectedIndex(); switch (idx) { case 0: d=0.02f; break; case 1: d=0.05f; break; } } } • Event Handling on Combobox String discount[] = {" discount 2% ", " discount 5% "}; JComboBoxmcombo = new JComboBox(discount); p.add(mcombo); mcombo.addActionListener (new ComboListener()); 3
Event Handling on Button JButtoncalcbtn = new JButton("calculation"); JButtonresetbtn = new JButton("reset"); Jbuttonclosebtn = new JButton("close"); p.add(calcbtn); p.add(resetbtn); p.add(closebtn); calcbtn.addActionListener(new ButtonListener()); resetbtn.addActionListener(new ButtonListener()); closebtn.addActionListener(new ButtonListener()); 4
Event Handling on Button private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == calcbtn) { baht = baht - (d*baht); if (nametxt.getText().equals("")) { JOptionPane.showMessageDialog(null,"ป้อนข้อมูลชื่อด้วยค่ะ !!!"); } else if (teltxt.getText().equals("")) { JOptionPane.showMessageDialog(null,"ป้อนข้อมูลเบอร์โทรด้วยค่ะ !!!"); } else if (scrollingList.isSelectionEmpty()) { JOptionPane.showMessageDialog(null, "เลือกรายการที่ต้องการเช่าด้วยค่ะ !!!"); } else if (!n1.isSelected() && (!n2.isSelected())) { JOptionPane.showMessageDialog(null,"เลือกจำนวนวันด้วยค่ะ !!!"); } else { String m = "คุณ " + nametxt.getText() +" เช่า" + msg + day + " = " + new Float(baht).toString()+ " บาท" ; JOptionPane.showMessageDialog(null,m); } } if(source==resetbtn) { nametxt.setText(""); teltxt.setText(""); /* n1.setSelected(false); n2.setSelected(false); */ n0.setSelected(true); scrollingList.clearSelection(); } if (source==closebtn) { dispose(); } } } 5
ผลการทำงานของโปรแกรม (กรณีเลือก DragonBall, 1 day, discount 5%)
ผลการทำงานของโปรแกรม (กรณีเลือก BirdLand, 3 day, discount 2%)