他の Java ファイルにある ClassA に getPercentage() メソッドがあり、別の Java ファイルの ClassB にあるプログレス バーを更新したいと考えています。
1 に答える
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 を実行し、他の画面のパーセンテージ バーを更新します。
于 2013-06-18T15:33:03.710 に答える