2013-06-18 24 views

回答

1

它很容易。

參見下面的完整的示例

A類

import java.awt.EventQueue; 

public class ClassA { 

    private JFrame frame; 
    private JProgressBar progressBar; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ClassA window = new ClassA(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public ClassA() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     progressBar = new JProgressBar(); 
     progressBar.setStringPainted(true); 
     progressBar.setBounds(10, 89, 291, 34); 
     frame.getContentPane().add(progressBar); 
     frame.setVisible(true); 
    } 

    public void updateProgressBar(int value) { 
     progressBar.setValue(value); 
    } 
} 

B類

import java.awt.EventQueue; 

public class ClassB { 

    private JFrame frame; 
    private static int i = 0; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ClassB window = new ClassB(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public ClassB() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     final ClassA a = new ClassA(); 

     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     JButton btnUpdate = new JButton("Update Value"); 
     btnUpdate.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       i = i + 10; 
       a.updateProgressBar(i); 
      } 
     }); 
     btnUpdate.setBounds(10, 52, 109, 23); 
     frame.getContentPane().add(btnUpdate); 
    } 
} 

現在運行B類和其他更新屏幕上的百分比吧。

+0

謝謝先生真的很感謝幫助! – Datta

+0

大拇指並接受爲答案。謝謝 :) – Makky