A small program

Source: Internet
Author: User

The main test is Java GUI programming, I heard that Java GUI programming is not much, but in the process of learning is always used system.out.println printing is really boring. And it looks very low, so ...

Before using VB and C # have written some small programs, the concept of the control is not unfamiliar, then start from 1+1=2.

Java's GUI classes are in the AWT package. Java.awt.*, there are many things in it, but I divide 2 kinds: One is the container class container, the other is the non-container (my own set). Container is also divided into Windows and Panel, where Windows is divided into frame and dialog, in fact, look at the Java API documentation I say so is wrong, container subclass more than 2. It's okay, remember the usual ones first. OK, first of all, write a calculator for the calculator, although it is a small program but involves some problems.

This is the class that inherits JFrame

classMyFrameextendsjframe{JTextField num1,num2,num3;    JButton JButton;  PublicMyFrame () {JFrame JFrame=NewJFrame ("Add"); JPanel JPanel=NewJPanel (NewFlowLayout ()); NUM1=NewJTextField (5); Num2=NewJTextField (5); NUM3=NewJTextField (6); JLabel JL=NewJLabel ("+"); JButton=NewJButton ("=");        Jpanel.add (NUM1);        Jpanel.add (JL);        Jpanel.add (NUM2);        Jpanel.add (JButton);                Jpanel.add (NUM3); Jbutton.addactionlistener (NewMonitor ( This));//First Kind//Jbutton.addactionlistener (New Monitor1 (NUM1, num2, num3));//The second KindJframe.setlayout (NewBorderLayout ());        Jframe.add (Jpanel,borderlayout.center);                Jframe.pack (); Jframe.setvisible (true);            Jframe.setdefaultcloseoperation (Jframe.exit_on_close); }}

The use of JButton, JTextField, JPanel and VB almost started quickly. button to add a listener event to calculate the result. But here's a question, to get a reference to an object inside a class (not sure if that's right), how to do it.

1 is to directly num1 num2 num3, as a parameter passed to other classes.

2 Pass the frame as a parameter to another class so that the object inside the frame can be accessed with a reference to the frame. But the premise is that these objects are not private to be accessed.

The first way of writing

  

classMonitorImplementsactionlistener{PrivateMyFrame JF =NULL;  PublicMonitor () {};  PublicMonitor (MyFrame JF) { This. JF =JF; } @Override Public voidactionperformed (ActionEvent e) {//TODO Auto-generated method stubs        intN; N= Integer.parseint (Jf.num1.getText ()) +Integer.parseint (Jf.num2.getText ());    Jf.num3.setText (string.valueof (n)); }}

The parameter of the constructor of the monitor here is the frame control.

The second type of notation

classMonitor1Implementsactionlistener{JTextField num1,num2,num3;  PublicMonitor1 (JTextField N1,jtextfield N2,jtextfield n3) { This. NUM1 =N1;  This. num2 =N2;  This. num3 =N3; } @Override Public voidactionperformed (ActionEvent e) {//TODO Auto-generated method stubs        intN; N= Integer.parseint (Num1.gettext ()) +Integer.parseint (Num2.gettext ());    Num3.settext (string.valueof (n)); }    }

The three parameters of the Monitor1 constructor here are text controls.
The result is

Well, start talking about something else.

1 in the program.

Jframe.setlayout (New BorderLayout ());
Jframe.add (Jpanel,borderlayout.center);
Jframe.pack ();

The pack () result is different from pack after the frame is added to the panel. It seems that I wrote the program too casually.

2 Click on the equals sign here to start the calculation results, this action is only hope that the button to complete, do not want to call other places, then put this monitoring class into the form of internal classes, which involves the use of internal classes of the benefits (data):

A can easily access the member variables of the wrapper class;

b Prevent accidental access, which is syntactically preventing access to classes that should not be accessed from the secondary class.

When do you use internal classes? When you don't want other classes to be accessed.

The modified code is as follows.

  

classMyFrameextendsjframe{JTextField num1,num2,num3;    JButton JButton;  PublicMyFrame () {JFrame JFrame=NewJFrame ("Add"); JPanel JPanel=NewJPanel (NewFlowLayout ()); NUM1=NewJTextField (5); Num2=NewJTextField (5); NUM3=NewJTextField (6); JLabel JL=NewJLabel ("+"); JButton=NewJButton ("=");        Jpanel.add (NUM1);        Jpanel.add (JL);        Jpanel.add (NUM2);        Jpanel.add (JButton);                Jpanel.add (NUM3); Jbutton.addactionlistener (NewMonitor ()); Jframe.setlayout (NewBorderLayout ());        Jframe.add (Jpanel,borderlayout.center);                Jframe.pack (); Jframe.setvisible (true);            Jframe.setdefaultcloseoperation (Jframe.exit_on_close); }    classMonitorImplementsactionlistener{ PublicMonitor () {}; @Override Public voidactionperformed (ActionEvent e) {//TODO Auto-generated method stubs            intN; N= Integer.parseint (Num1.gettext ()) +Integer.parseint (Num2.gettext ());        Num3.settext (string.valueof (n)); }    }}

Finally, say a little. A rare use of an inner class: A class inherits the interface, A, a, B. Interface A and B all use a method F (), but the return value is not the same. How to do it?
The wrapper class implements interface A, and the inner class implements interface B. The code is as follows:

  

 Public classInnerclass { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsAB AB =NewAb ();        System.out.println (Ab.f ()); //System.out.println (Ab.new inner (). f ());Ab.inner ai = ab.Newinner ();    System.out.println (AI.F ()); }}Interfacea{Abstract intf ();}Interfaceb{AbstractString f ();classAbImplementsa{@Override Public intf () {//TODO Auto-generated method stubs        return100; }    classInnerImplementsb{@Override PublicString F () {//TODO Auto-generated method stubs            returnB; }    }}

Execution results are

100
B

It's here today.

 

  

  

  

  

A small program

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.