Swing layout issue when migrating to java 8

My swing app in java 5 that had a type display

this

After switching to java 8, zoom in and show only part of it as this

I saw this one and tried to set J2D_D3D as an environment variable, and also tried passing it as a vm parameter. But this did not solve the problem. Any idea what this could be?

+5
source share
1 answer

For reference, here an example does not have a problem. It uses GridLayout(0, 1) with congruent spaces and border. Resize the frame to see the effect. Experiment with Box(BoxLayout.Y_AXIS) as an alternative.

I suspect that the source code (mis-) uses some combination of setXxxSize() or setBounds() that will display the displayed effect if the selected Look and Feel has a different geometry specified by the delegate of the button UI.

image

 import java.awt.EventQueue; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /** @see https://stackoverflow.com/a/31078625/230513 */ public class Buttons { private void display() { JFrame f = new JFrame("Buttons"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(new GridLayout(0, 1, 10, 10)); p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); for (int i = 0; i < 3; i++) { p.add(new JButton("Button " + (i + 1))); } f.add(p); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Buttons()::display); } } 
+3
source

All Articles