使用 Java 布局管理器的目的是什么? [英] What is the purpose of using Java Layout Managers?

查看:22
本文介绍了使用 Java 布局管理器的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎每当我试图创建一个程序时,我总是使用 Java 中的 setLayout(null); 命令结束,因为我喜欢绝对定位我正在放置的任何东西摆动组件.从我读到的每个人都一直在说使用布局管理器来简化编码过程,但它如何简化它?平台系统之间绝对定位会出现什么问题?

解决方案

当您使用

import java.awt.EventQueue;导入 java.awt.FontMetrics;导入 javax.swing.JFrame;导入 javax.swing.JLabel;导入 javax.swing.SwingUtilities;/*** @see https://stackoverflow.com/a/37801762/230513* @see https://stackoverflow.com/a/12532237/230513*/公共类邪恶{private static final String S = "明天的中奖号码:42,";私有最终 JLabel 标签 = new JLabel(S + "3, 1, 4, 1, 5, 9");私人无效显示(){JFrame f = new JFrame("Evil");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setLayout(null);FontMetrics fm = label.getFontMetrics(label.getFont());int w = SwingUtilities.computeStringWidth(fm, S) + 8;int h = fm.getHeight();label.setBounds(0, 0, w, h);f.添加(标签);f.setSize(w, h * 3);f.setLocationRelativeTo(null);f.setVisible(true);}公共静态无效主(字符串 [] args){EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){新的邪恶().显示();}});}}

It seems like whenever I am trying to create a program I always wind up using the setLayout(null); command in Java because I like to absolutely position whatever it is I'm putting onto my swing components. From what I read everyone keeps saying to use layout managers to simply the coding process, but how does it simplify it? What is the problem that arises with absolute positioning between platform systems?

解决方案

When you use a layout, invoking pack() "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents." When you don't, you have to try to calculate the bounds yourself. If (when) you get it wrong, as shown in the somewhat contrived example below, users will blame you—and not without some justification. A related example regarding non-resizable containers is seen here.

import java.awt.EventQueue;
import java.awt.FontMetrics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 * @see https://stackoverflow.com/a/37801762/230513
 * @see https://stackoverflow.com/a/12532237/230513
 */
public class Evil {

    private static final String S = "Tomorrow's winning lottery numbers: 42, ";
    private final JLabel label = new JLabel(S + "3, 1, 4, 1, 5, 9");

    private void display() {
        JFrame f = new JFrame("Evil");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(null);
        FontMetrics fm = label.getFontMetrics(label.getFont());
        int w = SwingUtilities.computeStringWidth(fm, S) + 8;
        int h = fm.getHeight();
        label.setBounds(0, 0, w, h);
        f.add(label);
        f.setSize(w, h * 3);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Evil().display();
            }
        });
    }
}

这篇关于使用 Java 布局管理器的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆