1 / 20

บทที่ 16

บทที่ 16. ตัวอย่าง แอพพลิเค ชันประยุกต์ใช้งาน GUI. แอพพลิเค ชันเช่ายืมแผ่นซีดีการ์ตูน (1). import java.awt.*; import java.awt.event .*; import javax.swing .*; import javax.swing.event .*;  public class CarToonRental extends JFrame {

dick
Download Presentation

บทที่ 16

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. บทที่ 16 ตัวอย่างแอพพลิเคชันประยุกต์ใช้งาน GUI

  2. แอพพลิเคชันเช่ายืมแผ่นซีดีการ์ตูน (1) import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;  public class CarToonRental extends JFrame { JPanel p; ButtonGroupbg; JComboBoxmcombo; JListscrollingList; JRadioButton n0, n1, n2; JLabelnamelbl, tellbl; JButtoncalcbtn, resetbtn,closebtn; JTextFieldnametxt, teltxt; String msg = "", day = ""; float d, baht;   public CarToonRental(String title) { super(title); String bookList[] = {"Doraemon","A-Ra-Re“,"DragonBall","BirdLand","Pung-Pong"}; p = new JPanel(); p.setLayout(new GridBagLayout()); namelbl = new JLabel("Name"); tellbl = new JLabel("Tel. no."); nametxt = new JTextField(10); teltxt = new JTextField(10); bg = new ButtonGroup(); n0 = new JRadioButton(); bg.add(n0); n1 = new JRadioButton("1 day"); bg.add(n1); n2 = new JRadioButton("3 day"); bg.add(n2); calcbtn=new JButton("calculation");resetbtn=new JButton("reset");closebtn=new JButton("close");

  3. แอพพลิเคชันเช่ายืมแผ่นซีดีการ์ตูน (2) scrollingList = new JList(bookList); scrollingList.setVisibleRowCount(4); scrollingList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); String discount[] = {" discount 2% ", " discount 5% "}; mcombo = new JComboBox(discount); AddPanel x = new AddPanel(); x.addItem(p, namelbl, 0, 0, 2, 1, GridBagConstraints.WEST); x.addItem(p, nametxt, 2, 0, 4, 1, GridBagConstraints.CENTER); x.addItem(p, tellbl, 0, 1, 2, 1, GridBagConstraints.WEST); x.addItem(p, teltxt, 2, 1, 4, 1, GridBagConstraints.CENTER); x.addItem(p, new JScrollPane(scrollingList), 0, 2, 2, 2, GridBagConstraints.WEST); x.addItem(p, n1, 2, 2, 2, 1, GridBagConstraints.WEST); x.addItem(p, n2, 4, 2, 2, 1, GridBagConstraints.WEST); x.addItem(p, mcombo, 2, 3, 4, 1, GridBagConstraints.CENTER); x.addItem(p, calcbtn, 0, 6, 2, 1, GridBagConstraints.CENTER); x.addItem(p, resetbtn, 2, 6, 2, 1, GridBagConstraints.CENTER); x.addItem(p, closebtn, 4, 6, 2, 1, GridBagConstraints.CENTER);

  4. แอพพลิเคชันเช่ายืมแผ่นซีดีการ์ตูน (3) calcbtn.addActionListener(new ButtonListener()); resetbtn.addActionListener(new ButtonListener()); closebtn.addActionListener(new ButtonListener()); n1.addActionListener(new RadioButtonListener()); n2.addActionListener(new RadioButtonListener()); mcombo.addActionListener(new ComboListener()); scrollingList.addListSelectionListener(new ListListener()); add(p); } private class ListListener implements ListSelectionListener {   public void valueChanged(ListSelectionEvent e) { msg = (String) scrollingList.getSelectedValue(); } }

  5. แอพพลิเคชันเช่ายืมแผ่นซีดีการ์ตูน (4) 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; } } }  private class ComboListener implements ActionListener {  public void actionPerformed(ActionEvent e) { intidx = mcombo.getSelectedIndex(); switch (idx) { case 0: d = 0.02f; break; case 1: d = 0.05f; break; } } }

  6. แอพพลิเคชันเช่ายืมแผ่นซีดีการ์ตูน (5) 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); } }

  7. แอพพลิเคชันเช่ายืมแผ่นซีดีการ์ตูน (6) if (source == resetbtn) { nametxt.setText(""); teltxt.setText(""); n1.setSelected(false); n2.setSelected(false); n0.setSelected(true); scrollingList.clearSelection(); }   if (source == closebtn) { dispose(); } } }   public static void main(String args[]) { CarToonRental f = new CarToonRental("CarToon DVD Rental"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(350, 250); f.setVisible(true); } }

  8. คลาส AddPanel import javax.swing.*; import java.awt.*;  public class AddPanel {  void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align) { GridBagConstraintsgc = new GridBagConstraints(); gc.gridx = x; gc.gridy = y; gc.gridwidth = width; gc.gridheight = height; gc.insets = new Insets(5, 5, 5, 5); gc.anchor = align; gc.fill = GridBagConstraints.NONE; p.add(c, gc); } }

  9. ผลการทำงานของแอพพลิเคชันเช่ายืมแผ่นซีดีการ์ตูนผลการทำงานของแอพพลิเคชันเช่ายืมแผ่นซีดีการ์ตูน กรณีเลือก DragonBall, 1 day, discount 5% กรณีเลือก BirdLand, 3 day, discount 2%

  10. แอพพลิเคชันคำนวณค่าแรงล่วงเวลา(1)แอพพลิเคชันคำนวณค่าแรงล่วงเวลา(1) import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.text.DecimalFormat; public class EmployeeOT extends JFrame{ public double salary, rate, hr, ot; public int z; JPanel p; JCheckBoxdCheckBox, nCheckBox; JRadioButtondRadioButton, mRadioButton, xRadioButton; JLabelslbl, rlbl, hlbl, sumlbl; JTextFieldstxt, rtxt, htxt; JButtonokbtn, resetbtn, closebtn; ButtonGroup group; Font fn = new Font("Microsoft Sans Serif", Font.BOLD, 14); final int WINDOW_WIDTH = 400; final int WINDOW_HEIGHT = 300;

  11. แอพพลิเคชันคำนวณค่าแรงล่วงเวลา (2) public EmployeeOT(String title) { super(title); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); dRadioButton = new JRadioButton("รายวัน"); dRadioButton.setBackground(Color.white); dRadioButton.setFont(fn); mRadioButton = new JRadioButton("รายเดือน"); mRadioButton.setBackground(Color.white); mRadioButton.setFont(fn); xRadioButton = new JRadioButton(); group = new ButtonGroup(); group.add(dRadioButton); group.add(mRadioButton); group.add(xRadioButton); dCheckBox = new JCheckBox("กะกลางวัน"); dCheckBox.setBackground(Color.white); dCheckBox.setFont(fn); nCheckBox = new JCheckBox("กะกลางคืน"); nCheckBox.setBackground(Color.white); nCheckBox.setFont(fn);

  12. แอพพลิเคชันคำนวณค่าแรงล่วงเวลา (3) slbl= new JLabel("เงินเดือน"); slbl.setFont(fn); stxt = new JTextField(10); rlbl = new JLabel("ค่าแรง (บาท/ชั่วโมง)"); rlbl.setFont(fn); rtxt = new JTextField(10); hlbl = new JLabel("จำนวนชั่วโมง OT"); hlbl.setFont(fn); htxt = new JTextField(10); okbtn = new JButton("Calculation"); okbtn.setFont(fn); resetbtn = new JButton("Reset"); resetbtn.setFont(fn); closebtn = new JButton("Close"); closebtn.setFont(fn); sumlbl = new JLabel("", SwingConstants.CENTER); sumlbl.setFont(fn); sumlbl.setOpaque(true); sumlbl.setBackground(Color.blue); sumlbl.setForeground(Color.white); sumlbl.setPreferredSize(new Dimension(300, 30));

  13. แอพพลิเคชันคำนวณค่าแรงล่วงเวลา (4) dRadioButton.addActionListener(new RadioButtonListener()); mRadioButton.addActionListener(new RadioButtonListener()); dCheckBox.addItemListener(new CheckBoxListener()); nCheckBox.addItemListener(new CheckBoxListener()); okbtn.addActionListener(new ButtonListener()); resetbtn.addActionListener(new ButtonListener()); closebtn.addActionListener(new ButtonListener()); p = new JPanel(); p.setLayout(new GridBagLayout()); p.setBackground(Color.white);

  14. แอพพลิเคชันคำนวณค่าแรงล่วงเวลา (5) AddPanelx = new AddPanel(); x.addItem(p, dRadioButton, 0, 0, 1, 1, GridBagConstraints.WEST); x.addItem(p, mRadioButton, 1, 0, 2, 1, GridBagConstraints.WEST); x.addItem(p, dCheckBox, 0, 1, 1, 1, GridBagConstraints.WEST); x.addItem(p, nCheckBox, 1, 1, 2, 1, GridBagConstraints.WEST); x.addItem(p, slbl, 0, 2, 1, 1, GridBagConstraints.WEST); x.addItem(p, stxt, 1, 2, 2, 1, GridBagConstraints.WEST); x.addItem(p, rlbl, 0, 3, 1, 1, GridBagConstraints.WEST); x.addItem(p, rtxt, 1, 3, 2, 1, GridBagConstraints.WEST); x.addItem(p, hlbl, 0, 4, 1, 1, GridBagConstraints.WEST); x.addItem(p, htxt, 1, 4, 2, 1, GridBagConstraints.WEST); x.addItem(p, okbtn, 0, 5, 1, 1, GridBagConstraints.CENTER); x.addItem(p, resetbtn, 1, 5, 1, 1, GridBagConstraints.CENTER); x.addItem(p, closebtn, 2, 5, 1, 1, GridBagConstraints.CENTER); x.addItem(p, sumlbl, 0, 6, 3, 1, GridBagConstraints.CENTER); add(p); }

  15. แอพพลิเคชันคำนวณค่าแรงล่วงเวลา (6) private class RadioButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == dRadioButton) { stxt.setText("0.00"); stxt.setEnabled(false); rtxt.setText(""); rtxt.setEnabled(true); htxt.setText(""); sumlbl.setText(""); } if (source == mRadioButton) { stxt.setText(""); stxt.setEnabled(true); rtxt.setText("0.00"); rtxt.setEnabled(false); htxt.setText(""); sumlbl.setText(""); } } }

  16. แอพพลิเคชันคำนวณค่าแรงล่วงเวลา (7) private class CheckBoxListener implements ItemListener{ public void itemStateChanged(ItemEvent e) { if (dCheckBox.isSelected() && !nCheckBox.isSelected()) { z = 1; } if (nCheckBox.isSelected()) { z = 2; } } }

  17. แอพพลิเคชันคำนวณค่าแรงล่วงเวลา (8) private class ButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e) { if (e.getSource() == okbtn) { if (!dRadioButton.isSelected() && !mRadioButton.isSelected()) { JOptionPane.showMessageDialog(null, "เลือกข้อมูลประเภทพนักงานด้วยค่ะ !!!"); } else if (!dCheckBox.isSelected() && !nCheckBox.isSelected()) { JOptionPane.showMessageDialog(null, "เลือกช่วงเวลาทำ OT ด้วยค่ะ !!!"); } else if (stxt.getText().equals("")) { JOptionPane.showMessageDialog(null, "ป้อนข้อมูลเงินเดือนด้วยค่ะ !!!"); } else if (rtxt.getText().equals("")) { JOptionPane.showMessageDialog(null, "ป้อนข้อมูลค่าแรงรายวันด้วยค่ะ !!!"); } else if (htxt.getText().equals("")) { JOptionPane.showMessageDialog(null, "ป้อนข้อมูลจำนวนชั่วโมง OT ด้วยค่ะ !!!"); }

  18. แอพพลิเคชันคำนวณค่าแรงล่วงเวลา (9) else { salary = new Double(stxt.getText()); rate = new Double(rtxt.getText()); hr = new Double(htxt.getText()); if (salary == 0) { ot = z * rate * hr; } else { ot = z * salary / 30 / 10 * hr; } DecimalFormat fm = new DecimalFormat("#,##0.00"); String ans = "TOTAL OT = " + fm.format(ot) + " BAHT "; sumlbl.setText(ans); } }

  19. แอพพลิเคชันคำนวณค่าแรงล่วงเวลา (10) if (e.getSource() == resetbtn) { xRadioButton.setSelected(true); stxt.setText(""); stxt.setEnabled(true); rtxt.setText(""); rtxt.setEnabled(true); htxt.setText(""); sumlbl.setText(""); dCheckBox.setSelected(false); nCheckBox.setSelected(false); } if (e.getSource() == closebtn) { System.exit(0); } } } public static void main(String args[]) { EmployeeOT f = new EmployeeOT("Employee OT Calculation"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }

  20. ผลการทำงานของแอพพลิเคชันคำนวณค่าแรงล่วงเวลา

More Related