Java,将布局设置为null

编程入门 行业动态 更新时间:2024-10-10 21:31:07
本文介绍了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

本文发布于:2023-10-28 10:50:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   布局   Java   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!