ตัวอย่างแอพพลิเคชัน ประยุกต์ใช้งาน GUI. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class CarToonRental.

Slides:



Advertisements
Similar presentations
Fondamenti di Java Introduzione alla costruzione di GUI (graphic user interface)
Advertisements

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Chapter 16 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.
User Interfaces II GUI – Awt, Swing, Web
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Addition Facts
Introduction to Swing Components Chapter 14. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
OLD BUSINESS : Lets talk about the next exam Project proposals – Functional Definitions Go over homework – NEW BUSINESS: Chapter 4 today GUIs Assignment.
Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
GridLayout, n*m grid. Column width/row height adjusted to fith the widest/highest Component.
ITK:P1 Lektion 6 Den abstrakta datatypen kö DSV Peter Mozelius.
Graphical User Interfaces
Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some.
Addition 1’s to 20.
JAVA1 1. JFrame. JAVA2 JApplet import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HerhalingAppletH10 extends JApplet implements.
ตัวอย่างการพัฒนา ระบบงาน. 2 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SignInWindow extends JFrame { JPanel panel;
Import javax.swing.*; public class FrameTest { public static void main(String args[]) { JFrame f = new JFrame("Frame Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLO.
1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces.
Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com.
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Web Design & Development Lecture 19. Java Graphics 2.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave.
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components.
1 MSPEL Lektion 3 DSV Peter Mozelius Grafik och spel i Java applets.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Chapter 11 Creating User Interfaces F What is JavaBean? F JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox  JList  JCheckBox.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Graphical User Interfaces Java’s AWT and Swing APIs.
Graphical User Interfaces (Part IV)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
More on GUI We wrap up our examination of Java GUI programming by looking at several other components and their listeners – The components all come from.
Drawing in a frame – Java GUI
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
The Model-View Approach in Java. OK, you’ve got “Hello World” running... What now?
F27SB2 Programming Languages
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
TCU CoSc Programming with Java Handling Events.
Swing: the art of the GUI COMP204, Bernhard Pfahringer.
JAVA 程式語言入門 (II).  版面配置  事件驅動  Ch14_01.java 1. import javax.swing.*; 2. import java.awt.*; 3. class Ch14_01 4. { 5. public static void main(String.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Övning 5. Repetition klasser class Rektangel { private static int antal = 0; private double längd; private double bredd; public Rektangel(double l, double.
Swing Components III Chapter 12 - Student JFrame, Component Methods.
(c) by Elizabeth Sugar Boese.1 Chapter 12 Events on Components - Lecture Slides.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
Class Class 20 Objectives Use JCheckbox, JRadioButton, and a JComboBox in a UID.
Basics of GUI Programming Chapter 11 and Chapter 22.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Java Swing. Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events:
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
Java GUI programming with Swing Tim McKenna
Lecture 16 More GUI programming
Graphical User Interface (pronounced "gooey")
Graphical User Interface (GUI) Components: Part 1 Java How to Program, 9 th Edition Chapter 14.
Java GUI programming with Swing
Presentation transcript:

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

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

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

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

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

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

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

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

กรณีเลือก DragonBall, 1 day, discount 5% 9 กรณีเลือก BirdLand, 3 day, discount 2%

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

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

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

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

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

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

private class CheckBoxListener implements ItemListener { public void itemStateChanged(ItemEvent e) { if (dCheckBox.isSelected() && !nCheckBox.isSelected()) { z = 1; } if (nCheckBox.isSelected()) { z = 2; } 16

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

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

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