简体   繁体   English

我如何在 Java 中更改 JButton 大小?

[英]How i can change JButton size in java?

I am learning java GUI programming and I would like to create button.我正在学习 java GUI 编程,我想创建按钮。 Problem is when button is created it is huge.问题是当按钮被创建时它是巨大的。 How I can resize button?如何调整按钮大小?

JFrame frame = new JFrame("Ikkuna <3");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel teksti = new JLabel("assdad", SwingConstants.CENTER);
    teksti.setFont(new Font("Eras Demi ITC", Font.PLAIN, 32));
    teksti.setForeground(Color.BLUE);//tekstin väri
    ImageIcon img = new ImageIcon("C:/Users/account/Documents/NetBeansProjects/eka/src/eka/Kuvat/Trollface.PNG"); //Tämä on kuvake(icon). 
    frame.setIconImage(img.getImage());

    JButton nappula = new JButton("Start");
    frame.getContentPane().add(nappula);
    nappula.setPreferredSize(new Dimension(83, 291));
    nappula.setLocation(500, 350);

    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    teksti.setPreferredSize(new Dimension(300, 100));

    frame.getContentPane().add(teksti, BorderLayout.PAGE_START); 
    frame.setLocationRelativeTo(null);                          
    
    frame.pack();
    frame.setVisible(true);

I also tried this:我也试过这个:

nappula.setPreferredSize(new Dimension(83, 291));

You Can set the fonts to the button and change its size您可以为按钮设置字体并更改其大小

Font f = new Font("Times New Roman",Font.BOLD,20);
nappula.setFont(f);

Set it of any size change 20 to any value...... Try it....将其设置为任意大小更改 20 为任意值......试试吧......

You're using default layout from the looks of it.从外观上看,您正在使用默认布局。 Try using尝试使用

nappula.setSize(new Dimension(x, y));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM