创建多个相似按钮/面板的正确方法

发布于 2024-12-18 02:50:07 字数 1100 浏览 4 评论 0原文

我尝试执行以下代码,但它仅在最后一个 GridLayout (情报统计)上显示(减号/加号按钮):

JButton plusButton = new JButton("+");
JButton minusButton = new JButton("-");

statStrengthGridPanel = new JPanel(new GridLayout(1,3));
statStrengthGridPanel.add(minusButton);
statStrengthGridPanel.add(new JLabel("10"));
statStrengthGridPanel.add(plusButton);

statConstitutionGridPanel = new JPanel(new GridLayout(1,3));
statConstitutionGridPanel.add(minusButton);
statConstitutionGridPanel.add(new JLabel("10"));
statConstitutionGridPanel.add(plusButton);

statDexterityGridPanel = new JPanel(new GridLayout(1,3));
statDexterityGridPanel.add(minusButton);
statDexterityGridPanel.add(new JLabel("10"));
statDexterityGridPanel.add(plusButton);

statIntelligenceGridPanel = new JPanel(new GridLayout(1,3));
statIntelligenceGridPanel.add(minusButton);
statIntelligenceGridPanel.add(new JLabel("10"));
statIntelligenceGridPanel.add(plusButton);

我知道我可以做一些事情,就像我为面板所做的那样名称(有多个),但我一开始就不想为面板这样做。我正在尝试使用最佳实践,并且不希望我的代码重复。有什么建议吗?

目标是拥有 4 个统计数据,通过递减和递增按钮分配点(我决定不使用滑块)。最终我会让它们有上限和下限,减少“未使用”标签,以及所有这些好东西,但我只是不想重复。

I have the below Code which I tried to do, but it only shows(the minus/plus button) on the last GridLayout (Intelligence stat):

JButton plusButton = new JButton("+");
JButton minusButton = new JButton("-");

statStrengthGridPanel = new JPanel(new GridLayout(1,3));
statStrengthGridPanel.add(minusButton);
statStrengthGridPanel.add(new JLabel("10"));
statStrengthGridPanel.add(plusButton);

statConstitutionGridPanel = new JPanel(new GridLayout(1,3));
statConstitutionGridPanel.add(minusButton);
statConstitutionGridPanel.add(new JLabel("10"));
statConstitutionGridPanel.add(plusButton);

statDexterityGridPanel = new JPanel(new GridLayout(1,3));
statDexterityGridPanel.add(minusButton);
statDexterityGridPanel.add(new JLabel("10"));
statDexterityGridPanel.add(plusButton);

statIntelligenceGridPanel = new JPanel(new GridLayout(1,3));
statIntelligenceGridPanel.add(minusButton);
statIntelligenceGridPanel.add(new JLabel("10"));
statIntelligenceGridPanel.add(plusButton);

I know I can do something like I did for the Panel names(have multiple ones), but I do not want to do that for the Panels in the first place. I am trying to use best practice and don't want my code to be repetitive. Any suggestions??

The goal is to have 4 stats, to assign points to, with decrement and increment buttons(I decided against sliders). Eventually I will have their have upper and lower limits, decrement the "unused" label, and all of that good stuff, but I just want not to be repetitive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2024-12-25 02:50:07

它不起作用的原因是您将相同的按钮添加到不同的网格面板。我认为你需要为每个你想看到它们的地方创建新的。
尝试类似的东西

statStrengthGridPanel = new JPanel(new GridLayout(1,3));
statStrengthGridPanel.add(new JButton("-"));
statStrengthGridPanel.add(new JLabel("10"));
statStrengthGridPanel.add(new JButton("+"));

The reason why its not working is that you are adding the same buttons to different gridpanels. I think that you need to create new ones for every place you want to see them.
Try something like

statStrengthGridPanel = new JPanel(new GridLayout(1,3));
statStrengthGridPanel.add(new JButton("-"));
statStrengthGridPanel.add(new JLabel("10"));
statStrengthGridPanel.add(new JButton("+"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文