Swing GUI布局添加 public void testDialog() { JPanel myPanel=newjpanel(newgridbaglayout()); GridBagConstraints grid=新的GridBagConstraints(); grid.gridx=0;//移动对象 grid.gridy=0;//向下移动对象 添加(新的JLabel(“输入玩家名称”,JLabel.CENTER),网格); grid.gridy++; JTextField tfNames[]=新的JTextField[getNumOfPlayers()]; //循环遍历参与者的数量,初始化JLabel和JTextField并将其添加到JPanel 对于(int i=0;i
尝试设置这种样式现在给我带来了一些痛苦..想将Player1,Player2,Player3,Player4 jLabel和它们的JTextFields放在中间吗。同时将“输入玩家名称”JLabel居中,该标签似乎已因更改而中断。我可以建议嵌套布局(未经测试)吗Swing GUI布局添加 public void testDialog() { JPanel myPanel=newjpanel(newgridbaglayout()); GridBagConstraints grid=新的GridBagConstraints(); grid.gridx=0;//移动对象 grid.gridy=0;//向下移动对象 添加(新的JLabel(“输入玩家名称”,JLabel.CENTER),网格); grid.gridy++; JTextField tfNames[]=新的JTextField[getNumOfPlayers()]; //循环遍历参与者的数量,初始化JLabel和JTextField并将其添加到JPanel 对于(int i=0;i,swing,user-interface,layout,grid-layout,Swing,User Interface,Layout,Grid Layout,尝试设置这种样式现在给我带来了一些痛苦..想将Player1,Player2,Player3,Player4 jLabel和它们的JTextFields放在中间吗。同时将“输入玩家名称”JLabel居中,该标签似乎已因更改而中断。我可以建议嵌套布局(未经测试)吗 刚刚尝试了一下,添加了:JPanel myPanel2=newjpanel();myPanel2.add(新JLabel(“输入玩家名称”);myPanel.add(myPanel2);就在myPanel的初始化下方。它将标签置于顶部,
刚刚尝试了一下,添加了:JPanel myPanel2=newjpanel();myPanel2.add(新JLabel(“输入玩家名称”);myPanel.add(myPanel2);就在myPanel的初始化下方。它将标签置于顶部,但由于某种原因,for循环中的jlabel现在不会显示在屏幕上,而JTextFields会显示。猜我做错了什么?应该有三个面板:
topPanel
,它的GridLayout
为(2,1)
,它添加slabelPanel
,然后添加sNameTablePanel
,就像你以前的面板一样。GridBagLayout(我一直试图避免)。不能使用它的人的话。它非常灵活和强大。然而,完全理解它需要一点经验(这几乎和用HTML编写
代码一样难理解)。现在就用GridBagLayout的方式完成了,因为我无法用另一种方式工作。然而,现在我正在尝试设计它,简单的事情已经停止了myPanel.add(newjlabel(“输入玩家名称”,JLabel.CENTER),grid);也就是说,不再以JLabel为中心。更新了上面的代码,以便您可以看到我所做的操作;这似乎与JTextFields有关,因为当我将它的大小从10减小到1时,所有的东西都离中心越来越近,所以东西在中心没有完全对齐。有人有什么想法吗?
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();
}
}
-------------------------
| panel with JLabel |
-------------------------
| panel with GridLayout |
-------------------------