简体   繁体   English

迁移到java 8时的Swing布局问题

[英]Swing layout issue on migration to java 8

My swing application in java 5 which had a display like 我在java 5中的swing应用程序有一个像

这个

After migrating to java 8, zoomed up and displays only a part of it like 迁移到java 8后,放大并仅显示其中的一部分 这个

I saw this and tried setting J2D_D3D as environment variable and also tried passing it as a vm parameter. 我看到了这个,并尝试将J2D_D3D设置为环境变量,并尝试将其作为vm参数传递。 But didn't solved the issue. 但没有解决问题。 Any idea what this could be? 知道这可能是什么?

For reference, here's an example the doesn't have the problem. 作为参考,这是一个没有问题的例子。 It uses a GridLayout(0, 1) with congruent gaps and border. 它使用具有全等间隙和边界的GridLayout(0, 1) Resize the enclosing frame to see the effect. 调整封闭框架的大小以查看效果。 Experiment with Box(BoxLayout.Y_AXIS) as an alternative. Box(BoxLayout.Y_AXIS)作为替代方案。

I suspect the original code (mis-)uses some combination of setXxxSize() or setBounds() , which will display the effect shown if the chosen Look & Feel has different geometry specified by the buttons' UI delegate. 我怀疑原始代码( setXxxSize() )使用setXxxSize()setBounds()某种组合,如果所选外观具有按钮的UI委托指定的不同几何,则会显示所显示的效果。

图片

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM