Personal understanding of the last three questions:
1) The program is first executed from the main function. If the main function is not static, it is unrealistic to instantiate the class and then call the main method. secondly, the main method modified with static is stored in the static storage area. That is to say, after a class is created, the main function already exists. After the static modification is removed, the compilation can be passed, but cannot be executed.
2) After querying the API, we found that the data read by the readLine () side of the BufferedRead object is that when reading a line break, the data is read into a row, data is output in different rows.
Copy codeThe Code is as follows: FileReader file = new FileReader ("C: \ 123.txt ");
BufferedReader br1 = new BufferedReader (file );
// A row has been read during judgment
While (br1.readLine ())! = Null)
{// Output the content of the second line
System. out. println (br1.readLine ());
}
So you can use a temporary string variable to store the read data. You can modify the program as follows:Copy codeThe Code is 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 you put the initialization of the client, input stream, and output stream into the Send button event, the program will achieve the desired effect. After you click Connect, the client will be connected, but I always think that there will be other security risks, and one day it will be exposed.
Today, we want to record a small program that the teacher arranged in the classroom. This is the prototype of a calculator, which only involves addition and subtraction operations. We have a new understanding of Button registration, or paste the code first.Copy codeThe Code is 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 the input text box, which occupies 8 Columns
Final JLabel t = new JLabel ("+"); // The operator in the initialization center is "+ ".
Final JTextField operand2 = new JTextField (10); // The second operator
Final JTextField result = new JTextField (4); // The text field of the result, which occupies 4 columns during initialization.
// Register an event for Composite Box c. The event triggered when the options of the Composite Box change.
C. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
If (c. getSelectedIndex () = 0) // when the option is "0", make the operator in the middle display the "+ ".
T. setText ("+ ");
Else t. setText ("-");
}
});
// Register an event for button Done. Different operations are performed when the operator in the middle is different.
Done. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
If (c. getSelectedIndex () = 0)
{
// When the operator in the middle is "+", add two operands. The get () method of the text field returns a string and performs forced conversion.
Int a = Integer. parseInt (operand1.getText () + Integer. parseInt (operand2.getText ());
Result. setText ("=" + "" + a + ""); // set the result to display the corresponding result.
}
Else {
// When the operator in the middle is "-", subtract two operands.
Int a = Integer. parseInt (operand1.getText ()-Integer. parseInt (operand2.getText ());
Result. setText ("=" + "" + a + "");
}
}
});
// Register an event for the "clear" button to clear the content of the two operands and the result.
Clear. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Operand1.setText (""); // clears operand 1
Operand2.setText (""); // clears operand 2
Result. setText (""); // clear the 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 ();
}
}
In the above Code, the options box, "done", and "clear" buttons are used to register events. The class is created to add events to corresponding components, you can also use the "clear" button in it as an example.
Implement a unique interface function in the ActionListener abstract class. To this end, define an object of the ButtonListener listener.Copy codeThe Code is as follows: class ButtonListener implements ActionListener {
Public void actionreceivmed (ActionEvent e ){
Operand1.setText (""); // clears operand 1
Operand2.setText (""); // clears operand 2
Result. setText (""); // clear the result box
}
}
You need to define the object property of a ButtonListener in class attributes:Copy codeThe Code is as follows: private ButtonListener clearaction = new ButtonListener ();
The last step is to register the event object of the button listener to the button:Copy codeThe Code is as follows: clear. addActionListener (clearaction );
Personal summary:
The general process of registering an event is as follows: ButtonListener = "ActionListener => Register a button. Compared with the Anonymous class, the disadvantage is that the amount of code is a little large, but suppose you have N plans to do this.
When the function buttons and event implementation methods are complex, you can implement an ActionListener object, define N ButtonListener listener objects at the same time, and register the same event implementation to the button, in contrast, anonymous classes have a lot of work in this case, and the amount of code will surge.
You can also use event e. the getSource () method puts all event processing into a function, which seems to be easier to maintain. In the class declaration, it is necessary to emphasize the implementation of Abstract Functions in the interface.Copy codeThe Code is as follows: public class ComboBoxTest extends JFrame implements ActionListener
The specific implementation process is as follows:Copy codeThe Code is as follows: public void actionreceivmed (ActionEvent e ){
If (e. getSource () = c ){
If (c. getSelectedIndex () = 0) // when the option is "0", make the operator in the middle display the "+ ".
T. setText ("+ ");
Else t. setText ("-");
}
If (e. getSource () = done ){
If (c. getSelectedIndex () = 0)
{
// When the operator in the middle is "+", add two operands. The get () method of the text field returns a string and performs forced conversion.
Int a = Integer. parseInt (operand1.getText () + Integer. parseInt (operand2.getText ());
Result. setText ("=" + "" + a + ""); // set the result to display the corresponding result.
}
Else {
// When the operator in the middle is "-", subtract two operands.
Int a = Integer. parseInt (operand1.getText ()-Integer. parseInt (operand2.getText ());
Result. setText ("=" + "" + a + "");
}
}
If (e. getSource () = clear ){
Operand1.setText (""); // clears operand 1
Operand2.setText (""); // clears operand 2
Result. setText (""); // clear the result box
}