Java,将布局设置为null [英] Java, setting layout to null

查看:166
本文介绍了Java,将布局设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用null布局,我很多人会说它不应该这样做。有更好的方法吗?



一些代码为例:

  import javax.swing。*; 

公共类Main {
public static void main(String args []){

JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton(Click);

// JFrame,帧
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

// JPanel,panel
panel.setLayout(null); //<< ----这是对的吗?
frame.add(面板);

// JButton,按钮
button.setBounds(25,25,100,60); //<< ----这是对的吗?
panel.add(按钮);

}
}


解决方案


有更好的方法吗?


布局管理器,布局管理器,布局管理器。如果默认提供的布局管理器没有做你想要的,可以使用布局管理器的组合,也可以尝试一些免费提供的布局管理器,比如MigLayout(并根据需要与其他布局管理器结合使用)



使用您的代码......





使用 GridBagLayout





如果你认为这过于戏剧化,那么不同的操作系统会给你带来更糟糕的事情


I have been using a null layout and I alot of people will say it shouldn't be done this way. Is there a better way?

Some code as example:

import javax.swing.*;

public class Main{
public static void main(String args[]){

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JButton button = new JButton("Click");

    //JFrame, frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setSize(500, 500);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    //JPanel, panel
    panel.setLayout(null); //<<---- Is this correct?
    frame.add(panel);

    //JButton, button
    button.setBounds(25, 25, 100, 60); //<<---- Is this correct?
    panel.add(button);

   }
}

解决方案

Is there a better way?

Layout managers, layout managers, layout managers. If the default provided layout managers aren't doing what you want, either use a combination of layout managers or maybe try some of the freely available layout managers, like MigLayout (and use them in combination with other layout managers as required)

With your code...

Using a GridBagLayout

The button is slightly bigger, because it's taking into account the actual requirements of the button (text and font size), but will always add an additional 100 pixels to the width and 60 pixels to the height.

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static void main(String args[]) {

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button = new JButton("Click");

        //JFrame, frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        //JPanel, panel
        panel.setLayout(new GridBagLayout());
        frame.add(panel);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.ipadx = 100;
        gbc.ipady = 60;
        gbc.insets = new Insets(25, 25, 0, 0);
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.anchor = GridBagConstraints.NORTHWEST;

        panel.add(button, gbc);

    }
}

Now, if all I do is add button.setFont(button.getFont().deriveFont(64f)); we end up with...

Your code on the left, my code on the right...

And if you think that's been overly dramatic, different OS's will do worse things to you then that

这篇关于Java,将布局设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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