java swing 刷新_swing界面刷新问题

本文通过一个简单的Java Swing应用实例,展示了如何实现界面元素的动态更新。在`buttonActionPerformed`方法中,通过改变`labelText`和`fieldText`的文本,演示了在按钮点击事件触发后,界面如何进行多次刷新。同时,注意到程序使用了`Thread.sleep`模拟延迟,以观察界面更新的过程。
摘要由CSDN通过智能技术生成

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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值