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();
}
}
尝试的风格,这是导致我有些疼痛now..Want居中PLAYER1,Player2,Player3,Player4的JLabel与他们一起JTextField的。同样居中'输入玩家姓名'JLabel,这似乎已经破坏了这些变化。GUI布局加成
_GridBagLayout(我一直试图避免)._不能使用它的人的话。它非常灵活和强大。然而,它需要一点点经验才能完全理解它(这与编写HTML中的'
现在做到了GridBagLayout方式,因为我无法以其他方式工作。然而,现在我试图去设计它,简单的东西已经停止工作myPanel.add(新的JLabel(“ENTER PLAYER NAMES”,JLabel.CENTER),grid); IE浏览器。这不再是JLabel的中心。更新了上面的代码,以便您可以看到我所做的事情。 – 2013-03-24 01:00:48
增加了grid.anchor = GridBagConstraints.CENTER; 似乎与JTextFields有关,为什么事物不能在中心完美对齐,因为当我从10缩小到1时,所有东西都靠近中心。任何人有任何想法? – 2013-03-24 01:56:12
回答
我可以建议嵌套布局(未经测试)吗?
来源
2013-03-23 22:33:12 personak
刚刚去过那里,加入: JPanel myPanel2 = new JPanel(); myPanel2.add(new JLabel(“Enter Player Names”)); myPanel.add(myPanel2); 正好在myPanel的初始化之下。它将标签置于顶部,但出于某种原因,for循环中的JLabel现在不显示在屏幕上,但是JTextFields却可以。猜猜我做错了什么? – 2013-03-23 22:46:43
应该有三个面板:''topPanel''带''(2,1)''的GridLayout'',''添加'labelPanel',然后'add's'TablePanelPanel',就像您的旧面板一样。 – personak 2013-03-23 22:58:23
相关问题