欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

探讨 Swing 界面更新的问题(转述)

最编程 2024-02-06 09:19:48
...
package com.longyg.test; public class MainFrame extends javax.swing.JFrame { public MainFrame() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel = new javax.swing.JLabel(); labelText = new javax.swing.JLabel(); jTextField = new javax.swing.JLabel(); fieldText = new javax.swing.JTextField(); button = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel.setText("JLabel:"); labelText.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jTextField.setText("JTextField: "); button.setText("click"); button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(10, 10, 10) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(button) .addGroup(layout.createSequentialGroup() .addComponent(jLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(labelText, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jTextField) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fieldText, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(17, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel) .addComponent(labelText, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTextField) .addComponent(fieldText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(button) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void buttonActionPerformed(java.awt.event.ActionEvent evt) { changeText("Button clicked"); try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } changeText("Start to change text..."); try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } for (int i = 0; i < 10; i++) { changeText((i+1)+""); try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } } changeText("action end"); } private void changeText(String text) { labelText.setText(text); fieldText.setText(text); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton button; private javax.swing.JTextField fieldText; private javax.swing.JLabel jLabel; private javax.swing.JLabel jTextField; private javax.swing.JLabel labelText; // End of variables declaration }