Removing the space around buttons in a GridBagLayout

I have this nasty source written to demonstrate the game’s screen layout mentioned for another question . It places the buttons (or labels selected at startup) in the GridBagLayout .

If you decide not to use the buttons when prompted (before the GUI), the entire graphical interface will be nice and compact without spaces. But if you decide to use the buttons, they will (if your setting is similar to mine) look something like this.

enter image description here

Pay attention to the red horizontal lines. This shows the color of the BG panel. These lines are not visible when the GUI uses labels. Extend the GUI a bit to see that it does not even put a red line after each line (there are nine lines) - although each line uses buttons (the same components).

How to remove extra vertical space when using buttons?

I suppose this should be what I forget to do when setting up buttons or row weights, but I can't figure that out!

 import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; import java.net.URL; import javax.imageio.ImageIO; public class SoccerField { private JPanel ui = null; int[] x = {0, 35, 70, 107, 142, 177, 212, 247, 282, 315}; int[] y = {0, 45, 85, 140, 180, 225, 265, 280, 320, 345}; boolean buttons; SoccerField() { initUI(); } public void initUI() { int result = JOptionPane.showConfirmDialog(ui, "Use buttons?"); buttons = result == JOptionPane.OK_OPTION; if (ui != null) { return; } ui = new JPanel(new GridBagLayout()); ui.setBackground(Color.RED); try { URL url = new URL("http://i.stack.imgur.com/9E5ky.jpg"); BufferedImage img = ImageIO.read(url); BufferedImage field = img.getSubimage(100, 350, 315, 345); BufferedImage[] bi = subSampleImageColumns(field); BufferedImage[][] fieldParts = new BufferedImage[bi.length][]; for (int ii=0; ii<bi.length; ii++) { fieldParts[ii] = subSampleImageRows(bi[ii]); } for (int ii=0; ii<fieldParts[0].length; ii++) { for (int jj=0; jj<fieldParts.length; jj++) { addImageToPanel(ui, fieldParts[ii][jj], ii, jj); } } } catch (Exception ex) { ex.printStackTrace(); } } private void addImageToPanel(JPanel panel, BufferedImage img, int row, int col) { Insets insets = new Insets(0,0,0,0); GridBagConstraints gbc = new GridBagConstraints( row, col, 1, 1, .5, .5, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0); ImageIcon ii = new ImageIcon(img); JButton b = new JButton(ii); b.setBorder(null); b.setBorderPainted(false); b.setContentAreaFilled(false); Component c = buttons ? b : new JLabel(ii); panel.add(c, gbc); } private BufferedImage[] subSampleImageColumns(BufferedImage img) { System.out.println("Image Size: " + img.getWidth() + "," + img.getHeight()); BufferedImage[] imageRows = new BufferedImage[x.length - 1]; for (int ii = 0; ii < x.length - 1; ii++) { BufferedImage bi = img.getSubimage( x[ii], 0, x[ii + 1] - x[ii], img.getHeight()); imageRows[ii] = bi; } return imageRows; } private BufferedImage[] subSampleImageRows(BufferedImage img) { BufferedImage[] imageRows = new BufferedImage[y.length - 1]; for (int ii = 0; ii < y.length - 1; ii++) { BufferedImage bi = img.getSubimage( 0, y[ii], img.getWidth(), y[ii + 1] - y[ii]); imageRows[ii] = bi; } return imageRows; } public JComponent getUI() { return ui; } public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception useDefault) { } SoccerField o = new SoccerField(); JFrame f = new JFrame(o.getClass().getSimpleName()); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setLocationByPlatform(true); f.setContentPane(o.getUI()); f.pack(); //f.setMinimumSize(f.getSize()); f.setVisible(true); } }; SwingUtilities.invokeLater(r); } } 
+7
java layout-manager swing jbutton gridbaglayout
source share
1 answer

It is noted that the preferred height of the buttons is apparently not calculated correctly for the buttons (in some cases).

In cases where the height of the mark is 40, the height of the button is 41.

Its like a button will always resize to an odd number?

I changed the code:

 //int[] y = {0, 45, 85, 140, 180, 225, 265, 280, 320, 345}; int[] y = {0, 45, 86, 139, 180, 225, 266, 279, 320, 345}; 

and it seems to work.

Ah, I just found the reason. You also need to:

 b.setFocusPainted(false); 
+6
source share

All Articles