如何改变JButton的位置和大小?

问题描述 投票:1回答:1

我正在开发一个游戏菜单,我在MenuPanel上实现了三个JButton,但是,我无法改变JButton的大小或位置。我已经尝试了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);

    }
java eclipse jframe jpanel jbutton
1个回答
0
投票

您可以使用setSize(int width, int height)方法。例如,如果你想要一个100px宽和20px高的按钮用于你的quitKnop按钮,你会写这行

quitKnop.setSize(100,20);

就位置而言,您应该查看布局管理器,正如@ Code-Apprentice在您的问题下面的评论中首先提到的那样。它们将允许您整理按钮。你应该使用那里的边距来增加空间。如果您希望按钮转到特定点,可以使用setLocation(int x, int y)方法,但我不建议这种情况。

© www.soinside.com 2019 - 2024. All rights reserved.