0
public void testDialog()
{
JPanel myPanel = new JPanel(new GridBagLayout());
GridBagConstraints grid = new GridBagConstraints();
grid.gridx=0; //Moves Things across
grid.gridy=0; //Moves things down
myPanel.add(new JLabel("ENTER PLAYER NAMES", JLabel.CENTER), grid);
grid.gridy++;
JTextField tfNames [] = new JTextField[getNumOfPlayers()];
//Loops through the number of players, initialising and adding a JLabel and JTextField to the JPanel
for(int i=0;i < getNumOfPlayers();i++)
{
grid.gridx = 0;
tfNames[i] = new JTextField(10);
myPanel.add(new JLabel("Player " + (i+1), JLabel.CENTER), grid);
grid.gridx ++;
myPanel.add(tfNames[i], grid);
grid.gridy+=1;
}
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Initial Dialog", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null);
//Declaring the array of strings which holds the players name
playerNames = new String [getNumOfPlayers()];
//If the 'OK' button is clicked loop through the number of players checking to see if all the JTextFields aren't empty
//If they are recall this method otherwise store the input player names in the playerName array.
if(result == JOptionPane.OK_OPTION)
{
for(int i=0;i < getNumOfPlayers();i++)
{
if(tfNames[i].getText().equals(""))
{
testDialog();
}
else
{
playerNames[i] = tfNames[i].getText();
}
}
}
//If 'Cancel' button is clicked go back to the previous dialog
else if (result == JOptionPane.CANCEL_OPTION)
{
numPlayersDialog();
}
//If 'x' in top right hand corner of the screen is clicked go back to previous dialog
else
{
numPlayersDialog();
}
}
이 스타일을 사용하면 현재 고통을 느끼고 있습니다. Player1, Player2, Player3, Player4 JLabels을 JTextField와 함께 가운데에 배치하려고합니다. 또한 'Change Player Names'JLabel을 변경하여 변경 한 것 같습니다.GUI 레이아웃 추가
_GridBagLayout (사용을 피하려고 노력했습니다.) 사용할 수없는 사람들의 말입니다. 그것은 매우 유연하고 강력합니다. 그러나 HTML을 완전히 이해하는 데 약간의 경험이 필요합니다 (HTML에서 '
GridBagLayout 방식으로 처리 했으므로 다른 방법으로 처리 할 수 없습니다. 그러나, 지금은 그것을 스타일을 노력하고있어 간단한 것들이 myPanel.add (새로운 JLabel ("Enter PLAYER NAMES", JLabel.CENTER), grid) 작업을 중단했습니다. 예. 그것은 더 이상 JLabel을 중심에 두지 않습니다. 위 코드를 업데이트하여 내가 한 일을 볼 수 있습니다. –
추가됨 grid.anchor = GridBagConstraints.CENTER; JTextFields와 관련이있는 것처럼 보입니다. 왜냐하면 모든 것이 가운데에서 완벽하게 정렬되지 않는 이유는 크기를 10에서 1로 줄이면 모든 것이 가운데에 가까워지기 때문입니다. 누구든지 아이디어가있어? –
답변
중첩 된 레이아웃 (테스트 안 함)을 제안 할 수 있습니까?
출처
2013-03-23 22:33:12 personak
그냥 거기에 가야, 추가 : JPanel myPanel2 = 새로운 JPanel(); myPanel2.add (새 JLabel ("플레이어 이름 입력")); myPanel.add (myPanel2); myPanel의 초기화 바로 아래입니다. 그것은 Label을 맨 위에 놓았지만 어떤 이유로 for 루프의 JLabel이 화면에 표시되지 않지만 JTextField는 않습니다. 내가 뭔가 잘못한 것 같니? –
'topPanel'에는'(2,1)'의'GridLayout'이 있고,'labelPanel'을 추가하고, 이전 패널과 같은'namesTablePanel'을'add'합니다. – personak
관련 문제