2011-11-25 4 views
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 개의 통계가 있고, 감소 및 증가 버튼이 있습니다 (슬라이더에 대해 결정). 결국 나는 그들에게 상한선과 하한선을 갖게하고, "사용되지 않은"표지와 모든 좋은 것들을 줄이겠다. 그러나 나는 단지 반복적 인 것을 원하지 않는다.

+1

참조 같은 것을 시도도 [스피너를 사용하는 방법 (http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html). – trashgod

+0

맞춤 통계 패널 또는 그리드 패널 구성 요소를 만들려고 생각하신 적이 있습니까? 당신은'JPanel'을 확장하고 생성시 버튼이나 라벨을 추가하는'GridPanel' 클래스를 만들 수 있습니다. –

+1

감사합니다. Spinner를 사용하기로 결정했습니다. – KisnardOnline

답변

1

작동하지 않는 이유는 다른 격자 패널에 동일한 버튼을 추가하기 때문입니다. 나는 당신이 그들을보고 싶어하는 모든 장소를 위해 새로운 것들을 만들어야한다고 생각합니다.

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

예, 버튼을 4 배 만들려고하고 싶지 않았지만, 너무 많은 것을 만드는 것에 대한 두려움을 극복해야합니다. 나는 객체 지향을 기억해야한다 :) 고마워! – KisnardOnline

관련 문제