我正在做一个游戏的菜单,我在MenuPanel上实现了三个JButtons,但是我不能改变JButtons的大小和位置。我已经尝试了setBounds函数,但没有任何结果。有谁可以帮我?
public class MenuPanel extends JPanel implements ActionListener
{
private JButton playKnop, highScoreKnop, quitKnop;
private MijnProject mainVenster;
public MenuPanel(MijnProject mainVenster)
{
this.mainVenster = mainVenster;
playKnop = new JButton("Play");
playKnop.addActionListener(this);
playKnop.setBounds(200, 200, 20, 20);
quitKnop = new JButton("Quit");
quitKnop.addActionListener(this);
highScoreKnop = new JButton("High Scores");
highScoreKnop.addActionListener(this);
this.add(playKnop);
this.add(quitKnop);
this.add(highScoreKnop);
}发布于 2019-04-30 05:42:01
您可以使用setSize(int width, int height)方法。例如,如果你想要一个宽100px,高20px的按钮作为你的quitKnop按钮,你可以这样写
quitKnop.setSize(100,20);
至于位置,你应该看看布局管理器,正如@Code-Apprentice在你的问题下面的评论中首先提到的那样。它们将允许您组织您的按钮。您应该使用那里的页边距来添加空格。如果希望按钮转到特定点,可以使用setLocation(int x, int y)方法,但我不建议在这种情况下使用该方法。
https://stackoverflow.com/questions/55910972
复制相似问题