Button size (Java)

I created a simple menu in Java, but I couldn't figure out how to change the size of the button

I want the last button to be the same size as the other buttons

tlacTisk.setSize(10,10);
tlacTisk.setPreferredSize(10,10);

It doesn't work

Code, I create buttons and boxes:

JButton tlacSVG = new JButton();
        tlacSVG.setText("Export do SVG");
        tlacSVG.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                exportujSVG();
            }
        });

        JButton tlacPNG = new JButton();
        tlacPNG.setText("Export do PNG");
        tlacPNG.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                exportujPNG();
            }
        });

        JButton tlacTisk = new JButton();
        tlacTisk.setText("Tisk...");
        tlacTisk.setPreferredSize(new Dimension(50,25));
        tlacTisk.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                tiskni();
            }
        });


        @R_208_2419@ @R_208_2419@Tlacitek = @R_208_2419@.createVertical@R_208_2419@();
        @R_208_2419@Tlacitek.add(@R_208_2419@.createVerticalStrut(5));
        @R_208_2419@Tlacitek.add(tlacSVG);
        @R_208_2419@Tlacitek.add(@R_208_2419@.createVerticalStrut(10));
        @R_208_2419@Tlacitek.add(tlacPNG);
        @R_208_2419@Tlacitek.add(@R_208_2419@.createVerticalStrut(10));
        @R_208_2419@Tlacitek.add(tlacTisk);
        @R_208_2419@Tlacitek.setBorder(BorderFactory.createTitledBorder("Menu"));

        okno.add(@R_208_2419@Tlacitek,BorderLayout.EAST);

Can you tell me how to change the size? thank you.

Solution

Different layout managers handle preferred sizes in different ways. In addition, setting the size using setsize() is not a good idea Let the layout manager make the layout for you For more details and examples, see a visual guide to layout managers

For example, you can create a separate panel containing buttons Set its layout to GridLayout In this layout, components occupy all available space in their cells, and each cell is exactly the same size Add this panel to the container For an example, see how to use GridLayout

This is a simple demonstration of GridLayout and gridbaglayout:

import java.awt.*;
import javax.swing.*;

public class DemoButtons {
    public DemoButtons() {
        final JFrame frame = new JFrame("Demo buttons");
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);

        JPanel buttonPanel = new JPanel(new GridLayout(3,1));
        buttonPanel.add(new JButton("Export do SVG"));
        buttonPanel.add(new JButton("Export do PNG"));
        buttonPanel.add(new JButton("Tisk..."));

        JPanel east = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTH;
        gbc.weighty = 1;
        east.add(buttonPanel,gbc);

        JPanel center = new JPanel(){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200,200);
            }
        };
        center.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        frame.add(east,BorderLayout.EAST);
        frame.add(center);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        SwingUtilities.invokelater(new Runnable() {
            @Override
            public void run() {
                new DemoButtons();
            }
        });
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>