Download presentation
Presentation is loading. Please wait.
Published byDelilah Smitherman Modified over 10 years ago
1
ตัวอย่างแอพพลิเคชัน ประยุกต์ใช้งาน GUI
2
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class CarToonRental extends JFrame { JPanel p; ButtonGroup bg; JComboBox mcombo; JList scrollingList; JRadioButton n0, n1, n2; JLabel namelbl, tellbl; JButton calcbtn, resetbtn,closebtn; JTextField nametxt, 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"); 2
3
scrollingList = new JList(bookList); scrollingList.setVisibleRowCount(4); scrollingList.setSelectionMode(ListSelectionModel.SINGLE_SELEC TION); 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); 3
4
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(); } 4
5
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) { int idx = mcombo.getSelectedIndex(); switch (idx) { case 0: d = 0.02f; break; case 1: d = 0.05f; break; } 5
6
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); } 6
7
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); } 7
8
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) { GridBagConstraints gc = 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); } 8
9
กรณีเลือก DragonBall, 1 day, discount 5% 9 กรณีเลือก BirdLand, 3 day, discount 2%
10
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; JCheckBox dCheckBox, nCheckBox; JRadioButton dRadioButton, mRadioButton, xRadioButton; JLabel slbl, rlbl, hlbl, sumlbl; JTextField stxt, rtxt, htxt; JButton okbtn, resetbtn, closebtn; ButtonGroup group; Font fn = new Font("Microsoft Sans Serif", Font.BOLD, 14); final int WINDOW_WIDTH = 400; final int WINDOW_HEIGHT = 300; 10
11
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); 11
12
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)); 12
13
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); 13
14
AddPanel x = 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); } 14
15
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(""); } 15
16
private class CheckBoxListener implements ItemListener { public void itemStateChanged(ItemEvent e) { if (dCheckBox.isSelected() && !nCheckBox.isSelected()) { z = 1; } if (nCheckBox.isSelected()) { z = 2; } 16
17
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 ด้วยค่ะ !!!"); } 17
18
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); } 18
19
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); } 19
20
20
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.