创建多个相似按钮/面板的正确方法
我尝试执行以下代码,但它仅在最后一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不起作用的原因是您将相同的按钮添加到不同的网格面板。我认为你需要为每个你想看到它们的地方创建新的。
尝试类似的东西
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