1 MainFrame.java2
3 packagecom.longyg.test;4
5 public class MainFrame extendsjavax.swing.JFrame {6
7 publicMainFrame() {8 initComponents();9 }10
11 @SuppressWarnings("unchecked")12 //
13 private voidinitComponents() {14
15 jLabel = newjavax.swing.JLabel();16 labelText = newjavax.swing.JLabel();17 jTextField = newjavax.swing.JLabel();18 fieldText = newjavax.swing.JTextField();19 button = newjavax.swing.JButton();20
21 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);22
23 jLabel.setText("JLabel:");24
25 labelText.setBorder(javax.swing.BorderFactory.createEtchedBorder());26
27 jTextField.setText("JTextField: ");28
29 button.setText("click");30 button.addActionListener(newjava.awt.event.ActionListener() {31 public voidactionPerformed(java.awt.event.ActionEvent evt) {32 buttonActionPerformed(evt);33 }34 });35
36 javax.swing.GroupLayout layout = newjavax.swing.GroupLayout(getContentPane());37 getContentPane().setLayout(layout);38 layout.setHorizontalGroup(39 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)40 .addGroup(layout.createSequentialGroup()41 .addGap(10, 10, 10)42 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)43 .addComponent(button)44 .addGroup(layout.createSequentialGroup()45 .addComponent(jLabel)46 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)47 .addComponent(labelText, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))48 .addGroup(layout.createSequentialGroup()49 .addComponent(jTextField)50 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)51 .addComponent(fieldText, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))52 .addContainerGap(17, Short.MAX_VALUE))53 );54 layout.setVerticalGroup(55 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)56 .addGroup(layout.createSequentialGroup()57 .addGap(20, 20, 20)58 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)59 .addComponent(jLabel)60 .addComponent(labelText, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))61 .addGap(18, 18, 18)62 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)63 .addComponent(jTextField)64 .addComponent(fieldText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))65 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)66 .addComponent(button)67 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))68 );69
70 pack();71 }//
72
73 private voidbuttonActionPerformed(java.awt.event.ActionEvent evt) {74 changeText("Button clicked");75 try{76 Thread.sleep(1000);77 } catch(InterruptedException ex) {78 ex.printStackTrace();79 }80 changeText("Start to change text...");81 try{82 Thread.sleep(1000);83 } catch(InterruptedException ex) {84 ex.printStackTrace();85 }86 for (int i = 0; i < 10; i++) {87 changeText((i+1)+"");88 try{89 Thread.sleep(1000);90 } catch(InterruptedException ex) {91 ex.printStackTrace();92 }93 }94 changeText("action end");95 }96
97 private voidchangeText(String text) {98 labelText.setText(text);99 fieldText.setText(text);100 }101
102 /**
103 *@paramargs the command line arguments104 */
105 public static voidmain(String args[]) {106 java.awt.EventQueue.invokeLater(newRunnable() {107
108 public voidrun() {109 new MainFrame().setVisible(true);110 }111 });112 }113 //Variables declaration - do not modify
114 privatejavax.swing.JButton button;115 privatejavax.swing.JTextField fieldText;116 privatejavax.swing.JLabel jLabel;117 privatejavax.swing.JLabel jTextField;118 privatejavax.swing.JLabel labelText;119 //End of variables declaration
120 }