Java learning notes (2) graphic user interface

Source: Internet
Author: User

This semester is mainly held in the ACM competition. The competition is over and it may take 15 weeks without knowing it. This week will take the java examination. Let's review java, the purpose of java learning is to allow us to apply what we have learned, so that we can use java to develop small projects, rather than just take the test. Today, I reviewed the graphic user interface, the implementation of the listener has been deepened, including java layout control and gui animation;

Implementation of the code in the book:

The function of this program is to press the button to change the color. The class that changes the color later is gradient, so the effect is not obvious;

I am mainly familiar with layout management and listening, and some usage of 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. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // close jb in the window. addActionListener (this); MyDrawPanel drawpanel = new MyDrawPanel (); jf. add (BorderLayout. SOUTH, jb); // manages the layout of components. add (BorderLayout. CENTER, drawpanel); jf. setSize (500,500); jf. setVisible (true) ;}@ Overridepublic void actionreceivmed (ActionEvent arg0) {// a method of event listening, which is an 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); // the color of the gradient layer may not be written in this way, and the effect is more obvious in g2d. setPaint (gradient); g2d. fillOval (100,100 );}}


Implement monitoring of two buttons:

The main function is to monitor two different buttons with different functions.

The drawing behind 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; // tag public static void main (String [] args) {TwoButtons gui = new TwoButtons (); gui. go ();} public void go () {jf = new JFrame (); jf. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); J Button labelbutton = new JButton ("change Label"); labelbutton. addActionListener (new ActionListener () {// The Listener here uses an anonymous internal class. You can also use an internal class @ Overridepublic void actionreceivmed (ActionEvent arg0) {jl. setText ("ouch! ") ;}}); JButton colorbutton = new JButton (" change circle "); colorbutton. addActionListener (new ActionListener () {@ Overridepublic void actionreceivmed (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 (500,500); 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 animation effect uses some preliminary knowledge of 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. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); draw = new Mydrawpanel (); jf. add (draw); jf. setSize (500,500); jf. setVisible (true); Thread thread1 = new Thread (new Mythread (); thread1.start ();/* for (int I = 0; I <300; I ++) {x ++; y ++; draw. repaint (); try {Thread. sleep (50);} catch (Exception e) {}} */} class Mythread implements Runnable {// here I wrote it using a thread, preliminary knowledge @ Overridepublic void run () {for (int I = 0; I <300; I ++) {x ++; y ++; draw. repaint (); try {Thread. sleep (50);} catch (Exception e) {}}} class Mydrawpanel extends JPanel {public void paintComponent (Graphics g) {g. setColor (Color. green); g. fillOval (x, y, x, y); // the effect of changing x and y to other values is different ;}}}


This basic effect;

These are some basic knowledge to be mastered, starting from the basics.


Zookeeper

Related Article

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.