Button Size (Java)

I created a simple menu in Java, but I can’t figure out how to resize the button. My menu looks like this: Current look

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

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

does not work.

Code where I created the buttons and the field:

 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(); } }); Box boxTlacitek = Box.createVerticalBox(); boxTlacitek.add(Box.createVerticalStrut(5)); boxTlacitek.add(tlacSVG); boxTlacitek.add(Box.createVerticalStrut(10)); boxTlacitek.add(tlacPNG); boxTlacitek.add(Box.createVerticalStrut(10)); boxTlacitek.add(tlacTisk); boxTlacitek.setBorder(BorderFactory.createTitledBorder("Menu")); okno.add(boxTlacitek, BorderLayout.EAST); 

Can you give me advice on how I can resize? Thanks.

+6
source share
2 answers

Different layout managers handle the preferred size differently. Also, setting the size with setSize() not a good idea. Let the layout manager do the layout for you. See the Visual Guide for Layout Managers for more information.

For example, you can create a separate panel containing buttons. Set its location to GridLayout . In this layout, the components occupy all the free space inside their cell, and each cell has exactly the same size. Add this panel to the container. See How to use GridLayout for an example.

Here is a simple demo of GridLayout and GridBagLayout :

enter image description here

 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(); } }); } } 
+12
source

Do not try to directly control the size of your buttons (or components), use the configuration of the layout manager of your container (the menu bar in your case).

To do this, take a look at the difference Swing Layout Managers : Swing Layout Managers

I personally prefer the JGoodies API, which I find much easier for the user and maintain over time: JGoodies Forms API

Hello

+3
source

All Articles