首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何更改JButton的位置和大小?

如何更改JButton的位置和大小?
EN

Stack Overflow用户
提问于 2019-04-30 05:21:05
回答 1查看 35关注 0票数 1

我正在做一个游戏的菜单,我在MenuPanel上实现了三个JButtons,但是我不能改变JButtons的大小和位置。我已经尝试了setBounds函数,但没有任何结果。有谁可以帮我?

代码语言:javascript
复制
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);

    }
EN

回答 1

Stack Overflow用户

发布于 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)方法,但我不建议在这种情况下使用该方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55910972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档