我希望將計數器添加到我的按鈕我也想要了解如何設置每個計數器,以便一旦計算機達到設定的計數數量時,我可能會在計算機上打開一個文件如何將計數器添加到GUI
package layout;
import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BoxLayoutDemo {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
addAButton("Section 1", pane);
addAButton("Section 2", pane);
addAButton("Section 3", pane);
addAButton("Section 4", pane);
addAButton("Section 5", pane);
addAButton("Section 6", pane);
addAButton("Section 7", pane);
addAButton("Section 8", pane);
addAButton("Section 9", pane);
}
private static void addAButton(String text, Container container) {
JButton button = new JButton(text);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
container.add(button);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Counter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
`
請幫助我是一個noob,它花了我數週才找到如何做到這一點。
歡迎來到SO。你的具體問題是什麼? *「一旦達到我設置的計數設定數量......」*什麼?直到您找到有心理人士的論壇回覆,我會建議您仔細重新閱讀您的帖子,如果可以的話,以確保發佈前有意義。 – 2012-04-26 17:54:38
順便說一下'addAButton(「Section 1」,pane); .. addAButton(「Section 9」,pane);'在你掌握循環之前做GUI編程是一個奇怪的方法。看起來好像試圖在沒有地基,沒有結構的房子上繪製排水溝。 – 2012-04-26 17:58:16
對不起,我的意思是,當它達到一個設定的計數值時,它會打開我準備好的6個.exe文件之一。 – user1359327 2012-04-26 18:47:02