Java Button Width

I tried using this and not working

singleplayerButton.setBounds(20, 20, 200, 100); 

I don't know why, can anyone help me with this?

My full page code is here

 package gmine; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class gmine implements ActionListener { JFrame interfaceFrame; JButton singleplayerButton, multiplayerButton, optionsButton, quitButton; public gmine() { JFrame.setDefaultLookAndFeelDecorated(false); interfaceFrame = new JFrame("G-Mine B0.4"); interfaceFrame.setSize(800,600); interfaceFrame.setLayout(new GridLayout(9,1, 20, 15)); singleplayerButton = new JButton("SinglePLayer"); singleplayerButton.addActionListener(this); interfaceFrame.add(singleplayerButton); singleplayerButton.setBounds(20, 20, 200, 100); multiplayerButton = new JButton("MultiPlayer"); multiplayerButton.addActionListener(this); interfaceFrame.add(multiplayerButton); optionsButton = new JButton("Options"); optionsButton.addActionListener(this); interfaceFrame.add(optionsButton); quitButton = new JButton("Quit"); quitButton.addActionListener(this); interfaceFrame.add(quitButton); interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); interfaceFrame.setVisible(true); } public void actionPerformed(ActionEvent a) { } public static void main(String[] args) { new gmine(); } } 

im trying to make buttons smaller so without touching the side of the page.

+4
source share
4 answers

I personally used the layout manager, which will give you more flexibility in determining how the buttons are laid out, and makes great efforts to honor your preferred size of your components, but gives you the freedom to make adjustments as necessary ..

For me it's GridBagLayout

enter image description here

 public class ButtonsLayout { public static void main(String[] args) { new ButtonsLayout(); } public ButtonsLayout() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new MenuPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class MenuPane extends JPanel { public MenuPane() { setLayout(new GridBagLayout()); JButton singleplayerButton = new JButton("SinglePLayer"); JButton multiplayerButton = new JButton("MultiPlayer"); JButton optionsButton = new JButton("Options"); JButton quitButton = new JButton("Quit"); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.ipadx = 20; gbc.ipady = 20; add(singleplayerButton, gbc); gbc.gridy++; add(multiplayerButton, gbc); gbc.gridy++; add(optionsButton, gbc); gbc.gridy++; add(quitButton, gbc); } } } 

Here I used ipadx and ipady for GridBagConstraints to increase the width and height of the components through the layout manager, and also use the HORIZONTAL padding to make all components the same width.

Take a look

For more information

+9
source
  • First of all, when you use the LayoutManager , then the position of the component and its size are controlled by the LayoutManager direct parent component . > when the component is hosted ..
    For instance:
    Putting a JButton on a JFrame, then the JFrame LayoutManager controls the position and size of the JButton.

  • I would also recommend using GroupLayout , which was developed by the NetBeans team in 2005, and you can use WindowsBuilderPro , now free from Google.

+4
source

The GridLayout Manager will give an equal size to all components. You can try using a different layout manager, and then use setPreferredSize(Dimension preferredSize) for your components to sort them correctly.

As another example, you can also place a JPanel on top of your JFrame and put buttons there.

+2
source

I tried using one of the answers above. Now the program will not be displayed. it just ends.

 package gmine; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class gmine { JFrame interfaceFrame; JButton singleplayerButton, multiplayerButton, optionsButton, quitButton; public void ButtonsLayout() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("G-Mine"); interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); interfaceFrame.setLayout(new BorderLayout()); interfaceFrame.setSize(800,600); interfaceFrame.add(new MenuPane()); interfaceFrame.pack(); interfaceFrame.setLocationRelativeTo(null); interfaceFrame.setVisible(true); } }); } public class MenuPane extends JPanel { public MenuPane() { setLayout(new GridBagLayout()); JButton singleplayerButton = new JButton("SinglePLayer"); JButton multiplayerButton = new JButton("MultiPlayer"); JButton optionsButton = new JButton("Options"); JButton quitButton = new JButton("Quit"); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.ipadx = 20; gbc.ipady = 20; add(singleplayerButton, gbc); gbc.gridy++; add(multiplayerButton, gbc); gbc.gridy++; add(optionsButton, gbc); gbc.gridy++; add(quitButton, gbc); } } public static void main(String[] args) { new gmine(); } 

}

+1
source

Source: https://habr.com/ru/post/1442950/


All Articles