Java 使用BoxLayout时,JPanel不居中
几年后,为了回到Java,我一直在做一个愚蠢的小纸牌把戏,我在让JPanel去我想去的地方时遇到了一些困难 给我带来麻烦的部件是精选的卡片和标签。其他一切都很好地位于中心。每个cardRow JPanel可容纳7张卡,mainRow可容纳cardRow,旁边有一个按钮。每张卡都是74x98,所以我尽量保持每样东西的大小 下面是处理所有放置和布局的技巧类Java 使用BoxLayout时,JPanel不居中,java,swing,user-interface,jpanel,layout-manager,Java,Swing,User Interface,Jpanel,Layout Manager,几年后,为了回到Java,我一直在做一个愚蠢的小纸牌把戏,我在让JPanel去我想去的地方时遇到了一些困难 给我带来麻烦的部件是精选的卡片和标签。其他一切都很好地位于中心。每个cardRow JPanel可容纳7张卡,mainRow可容纳cardRow,旁边有一个按钮。每张卡都是74x98,所以我尽量保持每样东西的大小 下面是处理所有放置和布局的技巧类 public Trick() { setMaximumSize(new Dimension(650, 600)); //set the
public Trick()
{
setMaximumSize(new Dimension(650, 600)); //set the size large enough to hold all panels neatly
//Card row setup
trickCards = new Deck().getCards(21);
cardRow1 = new JPanel();
cardRow2 = new JPanel();
cardRow3 = new JPanel();
cardRow1.setPreferredSize(new Dimension(650, 120));
cardRow2.setPreferredSize(new Dimension(650, 120));
cardRow3.setPreferredSize(new Dimension(650, 120));
cardRow1.setLayout(new BoxLayout(cardRow1, BoxLayout.X_AXIS));
cardRow2.setLayout(new BoxLayout(cardRow2, BoxLayout.X_AXIS));
cardRow3.setLayout(new BoxLayout(cardRow3, BoxLayout.X_AXIS));
setToRows();
button1 = new JButton("Row 1");
button1.setMnemonic(KeyEvent.VK_1);
button1.setActionCommand("1");
button2 = new JButton("Row 2");
button2.setMnemonic(KeyEvent.VK_2);
button2.setActionCommand("2");
button3 = new JButton("Row 3");
button3.setMnemonic(KeyEvent.VK_3);
button3.setActionCommand("3");
group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
chosenCard = new JPanel();
chosenCard.setLayout(new BoxLayout(chosenCard, BoxLayout.Y_AXIS));
chosenCard.setPreferredSize(new Dimension(100, 120));
chosenCard.add(new Card(trickCards[1].getCardFace(), -1, -1));
label = new JLabel("Card Trick! Pick any one card and click the row it is in [1, 2, 3]");
label.setPreferredSize(new Dimension(200, 20));
mainRow1 = new JPanel();
mainRow2 = new JPanel();
mainRow3 = new JPanel();
mainRow1.setPreferredSize(new Dimension(750, 120));
mainRow1.add(cardRow1);
mainRow1.add(button1);
mainRow2.setPreferredSize(new Dimension(750, 120));
mainRow2.add(cardRow2);
mainRow2.add(button2);
mainRow3.setPreferredSize(new Dimension(750, 120));
mainRow3.add(cardRow3);
mainRow3.add(button3);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(mainRow1);
add(mainRow2);
add(mainRow3);
add(label);
add(chosenCard);
}
//Refreshes what cards are in the rows
//Usually done after a selection is made
private void setToRows()
{
cardRow1.removeAll();
cardRow2.removeAll();
cardRow3.removeAll();
for(int x = 0; x < trickCards.length / 3; x++)
{
cardRow1.add(trickCards[x]);
cardRow2.add(trickCards[x + (trickCards.length / 3)]);
cardRow3.add(trickCards[x + ((trickCards.length / 3) * 2)]);
}
}
//The listener for the buttons
//Will be expanded
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("1"))
{
System.out.println("1");
label.setText("You chose 1!");
}
else if(e.getActionCommand().equals("2"))
{
System.out.println("2");
}
else if(e.getActionCommand().equals("3"))
{
System.out.println("3");
}
else
System.out.println("There's something wrong with the choice! Set to 0");
}
我猜您的标签居中,但标签中的文字不居中 因此,将文本对齐设置为居中
label.setHorizontalAlignment(JLabel.CENTER);
什么是SCCE?我正在工作,它对我来说被阻止了..抱歉,犯了一个错误,它是,这是一个简短的、自包含的、正确的可编译示例。这很可能是由于使用setPreferredSize的坏习惯造成的。这只是在与体制对抗。“让版面经理替你处理好这件事吧!”纪尧姆波莱特当我这么做时,除了左上角,所有的卡片都被剪掉了。然而,我可以通过强制将带有图像的a级JPanel卡改为75x100的首选尺寸来解决这个问题。但随着chosenCard和label组件的居中,它们仍然偏左。只要您不向我们提供更多信息,我们将无法为您提供更多帮助。我不知道您是如何实现卡的组件的,也不知道程序的其他部分。我能告诉你的是,使用setPreferredSize总是会导致布局问题。尝试添加更多只会引发更多问题。