Java Interface Programming (2) ------ button, text input box and text area
The window is created in the previous section. This is the container of other components. In this section, create a button.
Create button. You only need to call the JButton constructor where you want to appear.
A JButton is a component that has its own small window and can be automatically re-painted as part of the update process. That is to say, you do not need to show a button or other types of controls, as long as they are placed on the form, they can automatically draw themselves. Use the example in the previous section and modify it as follows:
Public class SwingTest extends JFrame {private static JLabel label; private static SwingTest st; private JButton b1; private JButton b2; public SwingTest () {super (Hello Swing); setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setSize (300,100); setVisible (true); // set the form layout setLayout (new FlowLayout (); label = new JLabel (A Label); add (label ); // add the button b1 = new JButton (Button 1); b2 = new JButton (Button 2); add (b1); add (b2 );} /*** @ param args * @ throws InterruptedException */public static void main (String [] args) throws InterruptedException {SwingUtilities. invokeLater (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubst = new SwingTest () ;}});/* TimeUnit. SECONDS. sleep (1); SwingUtilities. invokeLater (new Runnable () {@ Overridepublic void run () {label. setText (Hi, this is a different );}});*/}}A layout manager is introduced here. A new layout manager of the FlowLayout type is provided before any components are added to the JFrame ". The layout manager is a tool used by the Panel to implicitly determine the position of the control on the form. The default layout is the BoardLayout management layout. Every addition of a control completely overwrites other controls, so it is not applicable here. FlowLayout allows the control to be continuously distributed from left to right on the form, from top to bottom. The effect is as follows:
With buttons, you must have corresponding events. Therefore, we will add a click event for the buttons below.
Before adding an event, add a text input box JTextField, click the event class in the new listener button, and then bind the listener to the button, click the button to display the button name in the text input box. The complete code is as follows:
Public class SwingTest extends JFrame {private static JLabel label; private static SwingTest st; private JButton b1; private JButton b2; private JTextField txt; public SwingTest () {super (Hello Swing ); setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setSize (300,100); setVisible (true); // set the form layout setLayout (new FlowLayout (); // Add label Label = new JLabel (A label ); add (label); // add the text input box txt = new JTextField (10); add (txt); // add the button b1 = new JButton (Button 1 ); b2 = new JButton (Button 2); add (b1); add (b2); // add event b1.addActionListener (new ButtonListener (); b2.addActionListener (new ButtonListener ());} /*** @ param args * @ throws InterruptedException */public static void main (String [] args) throws InterruptedException {SwingUtilities. invokeLater (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubst = new SwingTest () ;}});/* TimeUnit. SECONDS. sleep (1); SwingUtilities. invokeLater (new Runnable () {@ Overridepublic void run () {label. setText (Hi, this is a different) ;}}); */} class ButtonListener implements ActionListener {@ Overridepublic void actionreceivmed (ActionEvent event) {// TODO Auto-generated method stub // display the button name in TextField. txt. setText (JButton) event. getSource ()). getText ());}}}The effect is as follows:
In this way, a click event is added for the button. click the button to trigger the corresponding event and display its name in the text input box. Similarly, you can write the event listener as its internal class, which is more convenient, that is, you can write it:
private ActionListener bl = new ActionListener() {@Overridepublic void actionPerformed(ActionEvent event) {// TODO Auto-generated method stubtxt.setText(((JButton)event.getSource()).getText());}};
Here, JTextField can only enter a single line of text, while in the text area, JTextArea can enter multiple lines of text and more functions, and append () is also a common method, in SwingTest () add:
// Add the text area txa = new JTextArea (5, 10); add (txa );
You can create a text area with 5 rows and 10 columns. To reflect the append () method, we add two buttons, click one to add a line of text, click the other to clear all text, and add the following code:
// Add the button b1 = new JButton (add data); b2 = new JButton (clear data); add (b1); add (b2); b1.addActionListener (new ActionListener () {@ Overridepublic void actionreceivmed (ActionEvent arg0) {// TODO Auto-generated method stubtxa. append (this is JText Area) ;}}); b2.addActionListener (new ActionListener () {@ Overridepublic void actionreceivmed (ActionEvent arg0) {// TODO Auto-generated method stubtxa. setText ();}});The effect is as follows:
As you can see, you can enter multiple lines of text in the text area. Of course, there are other functions.