【发布时间】:2012-04-26 17:34:01
【问题描述】:
我希望将计数器添加到我的按钮我还想学习如何设置每个计数器,以便一旦达到我在
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();
}
});
}
}
`
请帮助我是个菜鸟,我花了好几个星期才找到如何做到这一点。
【问题讨论】:
-
欢迎来到 SO。你的具体问题是什么? “一旦达到我在..中设置的计数设置数。” 什么?在你找到一个有通灵者可以回复的论坛之前,我建议你仔细重读你的帖子,如果可以的话,给朋友看,以确保在发帖前有意义。
-
BTW
addAButton("Section 1", pane); .. addAButton("Section 9", pane);在掌握循环之前进行 GUI 编程是一种奇怪的处理方式。这似乎是试图在没有地基、没有结构的房子上画排水沟。 -
对不起,我的意思是当它达到设定的计数时,它将打开我准备好的 6 个 .exe 文件之一。
-
@user1359327 :明智的做法是与为您的主题提供答案的人进行互动,以便他们能够更好地了解情况,了解您到底卡在哪里以及如何正是为了帮助你,继续前进:-)
标签: java swing user-interface counter