In-depth application analysis of Java Event Registration _java

Source: Internet
Author: User
Tags anonymous gettext readline
Personal understanding of the last three questions:
1 The program starts with the main function and assumes that the main function is not static, it is not realistic to instantiate the class first and then call the Main method. Secondly, the main method of static modification is stored in the static storage area, that is, after creating a class, the main function already exists, after removing the static modification, the compilation can pass, but cannot execute.
2) After the discovery of the API to find the Bufferedread object ReadLine () read the data is, read there is a line of lines, directly with the readLine to judge the time has been read into a row, in the output of the data will be interlaced output.
Copy Code code as follows:

FileReader file=new FileReader ("C:\\123.txt");
BufferedReader br1=new BufferedReader (file);
Read a line when judging
while ((Br1.readline ())!=null)
{//output is the contents of the second row
System.out.println (Br1.readline ());
}

So using a temporary string variable to store the read data, the program changes so that it can:
Copy Code code as follows:

FileReader file=new FileReader ("C:\\123.txt");
BufferedReader br1=new BufferedReader (file);

String cd;
while ((Cd=br1.readline ())!=null)
{
System.out.println (CD);
}

3) If the client, input stream, the initialization of the output stream all put into the Send button event, the program will reach the effect I want, click on the connection will have a client connection up, but always feel that there will be other security risks, one day it will leak.
Today to record here is a teacher arranged with a small program, to achieve a prototype of the calculator, which only add and subtract operations, the button in which the registration has a little new understanding, or the code posted out first.
Copy Code code as follows:

Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.*;
public class Comboboxtest extends jframe{
Private JButton done =new JButton ("Done");
Private JButton clear=new JButton ("clear");
Private JLabel label = new JLabel ("Please choose serverid:0 (+) and1 (-)");

Public Comboboxtest () {
Add a combo box and set two options
Final JComboBox C = new JComboBox ();
int [] array = {0,1};
C.additem (Array[0]);
C.additem (array[1]);
Final JTextField operand1=new JTextField (10); Add the first operand as an input text box, accounting for 8 columns
Final JLabel t=new JLabel ("+"); Initialize the middle operator to the "+" number
Final JTextField operand2=new JTextField (10); Second operator
Final JTextField result=new JTextField (4); The text field of the result, initialized to 4 columns

Registers an event with the combo box C and triggers the corresponding event when the combo box option is changed
C.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
if (C.getselectedindex () ==0)//option is "0", the middle operator is displayed with the "+" number
T.settext ("+");
Else T.settext ("-");
}
});
Register an event with a button to perform a different action when the middle operator is not at the same time
Done.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
if (C.getselectedindex () ==0)
{
When the middle operator is a "+" number, an addition of two operands is performed, and the Get () method of the Text field returns a string for casting
int A=integer.parseint (Operand1.gettext ()) +integer.parseint (Operand2.gettext ());
Result.settext ("=" + "" +a+ "); Set results to display corresponding results
}
else {
Subtract two operands when the middle operator is "-"
int A=integer.parseint (Operand1.gettext ())-integer.parseint (Operand2.gettext ());
Result.settext ("=" + "" +a+ ");
}
}
});
Registers an event with the button clear, emptying the contents of two operands and results
Clear.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Operand1.settext (""); Number of empty operations 1
Operand2.settext (""); Number of empty operations 2
Result.settext (""); Empty result Box
}
});
SetLayout (New FlowLayout ());
Add (label);
Add (c);
Add (OPERAND1);
Add (t);
Add (Operand2);
Add (result);
Add (done);
Add (clear);
SetSize (350,140);
SetVisible (TRUE);
}

public static void Main (string[] args) {
New Comboboxtest ();
}
}

An anonymous class is used to register events with the box, done, and clear buttons in the above code, and this class is created to add events to the corresponding component, as well as to use the "clear" button inside to make an example.
Implements a unique interface function within the ActionListener abstract class that defines an object for a Buttonlistener listener
Copy Code code as follows:

Class Buttonlistener implements actionlistener{
public void actionperformed (ActionEvent e) {
Operand1.settext (""); Number of empty operations 1
Operand2.settext (""); Number of empty operations 2
Result.settext (""); Empty result Box
}
}

The class attribute needs to define an Buttonlistener object attribute:
Copy Code code as follows:

Private Buttonlistener clearaction = new Buttonlistener ();

The final step is to register the event object for this button listener to the button:
Copy Code code as follows:

Clear.addactionlistener (clearaction);

Personal Summary:
This kind of way of registering an event is roughly the process of Buttonlistener = "ActionListener => registered to the button, compared to the anonymous class, the disadvantage is that the amount of code is a bit more, but suppose you have n plan to have this
function of the button and the event implementation of the method is more complex, you can implement a ActionListener object, while defining n Buttonlistener listener objects, the same event implementation register to the button on it, Anonymous classes, by contrast, have a lot of work to do under this scenario, and the amount of code can soar.
You can also put all event processing into a function through the event E.getsource () method, which seems to be easier to maintain, emphasizing the implementation of abstract functions within the interface in class declarations.
Copy Code code as follows:

public class Comboboxtest extends JFrame implements ActionListener

The specific implementation process is as follows:
Copy Code code as follows:

public void actionperformed (ActionEvent e) {
if (E.getsource () ==c) {
if (C.getselectedindex () ==0)//option is "0", the middle operator is displayed with the "+" number
T.settext ("+");
Else T.settext ("-");
}

if (E.getsource () ==done) {
if (C.getselectedindex () ==0)
{
When the middle operator is a "+" number, an addition of two operands is performed, and the Get () method of the Text field returns a string for casting
int A=integer.parseint (Operand1.gettext ()) +integer.parseint (Operand2.gettext ());
Result.settext ("=" + "" +a+ "); Set results to display corresponding results
}
else {
Subtract two operands when the middle operator is "-"
int A=integer.parseint (Operand1.gettext ())-integer.parseint (Operand2.gettext ());
Result.settext ("=" + "" +a+ ");
}
}
if (E.getsource () ==clear) {
Operand1.settext (""); Number of empty operations 1
Operand2.settext (""); Number of empty operations 2
Result.settext (""); Empty result Box
}

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.