[英]proper way to create multiple similar buttons/panels
我嘗試執行以下代碼,但僅在最后一個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個統計數據,並使用遞減和遞增按鈕來分配點(我根據滑塊決定)。 最終,我將使它們有上限和下限,減少“未使用”的標簽,以及所有這些好東西,但我只是不想重復。
它不起作用的原因是您將相同的按鈕添加到不同的網格面板。 我認為您需要為每個想要看到它們的地方創建新的。 嘗試類似
statStrengthGridPanel = new JPanel(new GridLayout(1,3));
statStrengthGridPanel.add(new JButton("-"));
statStrengthGridPanel.add(new JLabel("10"));
statStrengthGridPanel.add(new JButton("+"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.