Java Learning Note (ii) graphical user interface

Source: Internet
Author: User
Tags event listener

This semester is mainly in the ACM game up, the game is over. Unknowingly 15 weeks, this week will be the Java exam, review Java bar. The purpose of Java learning is to let us learn from it, so that we can use Java to develop small projects. Instead of just coping with the exam, I reviewed it today. Graphical user interface, the implementation of the monitoring has deepened some, Java layout control, GUI animation;

The implementation of the Code on the book:

The function of this program is to press the button, will change the color, the latter change the color of the class is a gradient, so the effect is not obvious;

The main is familiar with, layout management and listening to the writing, there are some ways to use the Paintcomponent class;

Import Java.awt.borderlayout;import java.awt.color;import Java.awt.gradientpaint;import Java.awt.Graphics;import Java.awt.graphics2d;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jbutton;import Javax.swing.jframe;public class GUI implements ActionListener {JFrame jf;public static void Main (string[] args) {GUI gui=new gui (); Gui.go ();} public void Go () {jf=new JFrame ();         JButton jb=new JButton ("Change Color"); Jf.setdefaultcloseoperation (jframe.exit_on_close);//form Close Jb.addactionlistener (this); Mydrawpanel drawpanel =new Mydrawpanel (); Jf.add (BORDERLAYOUT.SOUTH,JB);    Layout Management of Components Jf.add (Borderlayout.center,drawpanel);    Jf.setsize (500, 500); Jf.setvisible (TRUE);} @Overridepublic void actionperformed (ActionEvent arg0) { //event listener a notation, external class jf.repaint ();}}     public class Mydrawpanel extends Jpanel{public void paintcomponent (Graphics g)  //drawing {graphics2d g2d= (graphics2d) G; Gradientpaint gradient=new Gradientpaint (70,70,color.blue,150, 150,color.orange);//Gradient color   can not write so, the effect is more obvious g2d.setpaint (gradient); G2d.filloval (70,70,100,100);}}


Implement two-button snooping:

The basic function is to implement two different button listeners, each with different functions.

The following drawing is the same as the previous program.

Import Java.awt.borderlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import    Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.jlabel;public class TwoButtons {JFrame JF; JLabel JL;//Label public static void Main (string[] args) {twobuttons gui =new twobuttons (); Gui.go ();} public void Go () {jf=new JFrame (); jf.setdefaultcloseoperation (Jframe.exit_on_close); JButton labelbutton=new JButton ("Change Label") Labelbutton.addactionlistener (new ActionListener () {// The listener here uses an anonymous inner class and can also use the inner class @overridepublic void actionperformed (ActionEvent arg0) {jl.settext ("ouch!");}); JButton colorbutton=new JButton ("Change Circle") Colorbutton.addactionlistener (new ActionListener () {@ overridepublic void actionperformed (ActionEvent e) {jf.repaint ();}}); Jl=new JLabel ("I am a label"); Mydrawpanel drawpanel=new Mydrawpanel (); Jf.add (Borderlayout.south,colorbutton); Jf.add (BorderLayout.CENTER, Drawpanel); Jf.add (Borderlayout.east,labelbutton); Jf.add (borderlayout.west,jl); jf.setsize (+); jf.setvisible (True);}} 
public class Mydrawpanel extends Jpanel{public void paintcomponent (Graphics g) {graphics2d g2d= (graphics2d) G;     Gradientpaint gradient=new Gradientpaint (70,70,color.blue,150,150,color.orange);     G2d.setpaint (gradient);     G2d.filloval (70,70,100,100);}}

This is the general interface;

The effect of the animation. Use the initial knowledge of some threads.

Import Java.awt.color;import java.awt.graphics;import Javax.swing.jframe;import Javax.swing.jpanel;public class simpleanimation {int x=80; int y=80;      Mydrawpanel draw;public static void Main (string[] args) {simpleanimation simpleanimation=new simpleanimation (); Simpleanimation.go ();} public void Go () {JFrame jf=new JFrame (); Jf.setdefaultcloseoperation (jframe.exit_on_close);d raw=new Mydrawpanel (); Jf.add (draw); Jf.setsize (+); jf.setvisible (true);                Thread Thread1=new Thread (new Mythread ()); Thread1.start ();/*for (int i=0;i<300;i++) {x++;y++;d raw.repaint (); try { Thread.Sleep (50);} catch (Exception e) {}}*/}class Mythread implements runnable{//here is a thread written.  Preliminary knowledge @overridepublic void Run () {for (int i=0;i<300;i++) {x++;y++;d raw.repaint (); try {thread.sleep (50);} catch (Exception e) {}}}} class Mydrawpanel extends JPanel {public void paintcomponent (Graphics g) {G.setcolor (Co   Lor.green); G.filloval (x,y,x,y);//This x and Y change to other values will have different effects;}}}



Here the image will leave traces when it changes;

How do we eliminate these traces?

Class Mydrawpanel extends JPanel {public   void paintcomponent (Graphics g) {   g.setcolor (color.white);   G.fillrect (0, 0, this.getwidth (), This.getheight ());   G.setcolor (color.green);   G.filloval (x,y,x,y);   }   }

Add a piece of code that fills the entire panel with the original background color before each new circle is drawn;

So what we are seeing is moving the drawing and eliminating the traces of the middle.

The basic effect of this;

Review these are some of the basic knowledge to master, starting from the foundation.


??

Java Learning Note (ii) graphical user interface

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.