written in front:Top two blogs we introduced the basic structure of the simple Java GUI and the event monitoring mechanism respectively. This time we will introduce the dual event (multi event) listening mechanism and introduce the inner class. 1. Design Tasks
Design a GUI, including basic components: button (two), label (one), Random color Circle-panel (one), re
()). GetText ()); - } -}Run resultsThere are many deficiencies in this programming approach, such as readability, poor reusability, and so on. It's more popular now. Use inner classes, especially anonymous inner classes, to implement the Listener class interface.1 Importjava.awt.event.ActionEvent;2 ImportJava.awt.event.ActionListener;3 4 ImportJavax.swing.JButton;5 ImportJavax.swing.JFrame;6 7 Public classJButtonEvent1extendsJFrame {8 PublicJButtonEvent1 () {9JButton button=NewJBu
Return the image source code, re-open a class paste canPackage Cn.littlepage.game;import Java.awt.image;import Java.awt.image.bufferedimage;import java.io.IOException; Import Java.net.url;import Javax.imageio.ImageIO;PublicClassGameutil {//The tool class is best to privatize the constructor./** Load Picture Code * Returns the picture object for the specified picture*/private Gameutil () {}/* * * Returns the picture object of the specified path file * @param path * @return */public static image
"AWT"surface) programming. the various elements of Note: AWT has a certain flaw in cross-platform, and the newer programming method is swing (in the Javax.swing Class). Java--gui
");}}});Add an activity listener for the menu item CloseitemClose_item.addactionlistener (new ActionListener () {@OverrideThe Actionperformed abstract method adds a processing action.public void actionperformed (ActionEvent e) {TODO auto-generated method stubsSystem.exit (0);}});The form calls the Addwindowlistener method, passing an interface Class (Listener adapter) Windowadapter.F.addwindowlistener (New Windowadapter () {Replication Windowclosing method.public void windowclosing (WindowEvent
1.awt:component: Buttons, icons, etc. that can be displayedwindow can be displayed independently, the panel can accommodate other elements, but it cannot be displayed as a separate window for the application.Applets are not usedThe basic difference between AWT and swing: AWT is a local method-based C/s + + program that runs faster, and swing is an AWT-based Java program that runs slower. For an embeddedApplications, the hardware resources of the targe
TextField (20);//Create a new text input window // Listen action, implement 7 actions in Windowadapter class, F.addwindowlistener in Java.awt.* package (new Windowadapter () {//close window to call public void Windowclosing (WindowEvent e) {System.out.println ("window closing-----" +e.tostring ()); System.exit (0);} Call public void Windowactivater (WindowEvent e) {System.out.println ("active") as soon as the window is activated;} Open the window and call public void windowopened (WindowEvent e
"Implementation Interface"AnalysisThe whole with default borderlayout (border layout), divided into upper and lower, respectively, with three JPanel storage;On: A picture, with JLabel;Under: Three buttons JButtonMiddle: Stored with a jtabbedpane (tab)Each option is a GridLayout (grid layout)Four JLabel, one JTextField, one jpassword, one JButton, two jcheckbox (check box)CodePackage Start;import java.awt.*;//must be introduced in two packages import Javax.swing.*;//public class Main extends jfra
Button tap to listen
Trigger a listener event by clicking the button1Button_20 =NewJButton ("20");//Create a Button object Button_202Button_20.addactionlistener (NewActionListener () {//To Add a button listener event3 Public voidactionperformed (ActionEvent e) {4 if(Textfield_6.gettext (). Equals ("")) {5Textfield_6.settext ("20"); //Add content to the target text box 6}Else{7 intsum = Integer.parseint (Textfield_6.gettext ()) +20;8 textfield
(40 0, 500,BUFFEREDIMAGE.TYPE_INT_RGB);//double-buffered private jlabel[] Lbls = new Jlabel[4];p ublic clock () {This.settitle ("clock"); This.setsize (+), this.setdefaultcloseoperation (exit_on_close); this.setresizable(false); This.setlocationrelativeto (null); This.setlayout (null); timer = new Timer (n, this); lbl = new JLabel (str); Displays the digital time Lbl.setfont (new Font ("Consolas", Font.plain, Panax Notoginseng)), Lbl.setbounds ((), This.add (LBL); for (int i = 0; i
need to implement ActionListener method actionperformed Complete Event listening process private JPanel panel;private JButton jButton1, jbutton2;public static void Main (string[] args) {Events events = new events (); Public Events () {panel = new JPanel (); jButton1 = new JButton ("Switch to red") JButton2 = new JButton ("Switch to Blue"); This.add (JButton1, B Orderlayout.north); This.add (panel, borderlayout.center); This.add (JButton2, Borderlayout.south);//register for monitoring, monitorin
There are currently three creation templates to generate the application form:The first type: Public class Implements actionlistener{// your component (member variable) Public Menudemo () {// constructor method JFrame frame=new JFrame ("menu form"); Frame.setsize (300,300); Frame.setlocation (300,200); Frame.setresizable (false); Frame.setvisible (true);The second type: public class Demodemo "extends jframe{ // your component pu
the specified string commands in a separate process. the theRuntime r =runtime.getruntime (); - Try { theR.exec ("notepad"); the}Catch(IOException E1) { the //TODO auto-generated Catch block94 e1.printstacktrace (); the } the the }98 About }); - 101 //Setting the window to turn off listener events102F.addwindowlistener (NewWindowadapter () {103 Public
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.