本文介绍了BoxLayout:无法设置子组件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 JFrame
- SuperTest
和 JPanel
- SuperLogin
.登录面板具有用户名和密码输入字段以及一个登录按钮.我希望它看起来像这样:
但是它看起来像下面的图片,输入字段的高度和宽度太大.
SuperTest.java :
import javax.swing.*;公共类SuperTest扩展了JFrame {公共SuperTest(){添加(新的SuperLogin());setVisible(true);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);setSize(600,400);}公共静态void main(String [] args){SuperTest测试=新的SuperTest();}}
SuperLogin.java :
import javax.swing.*;导入java.awt.*;公共类SuperLogin扩展了JPanel {私人JButton loginButton =新的JButton("Login");私人TextField usernameField =新的文本字段();私人TextField passwordField =新的TextField();公共SuperLogin(){BoxLayout布局=新的BoxLayout(this,BoxLayout.Y_AXIS);setLayout(layout);add(new JLabel("Login")));添加(用户名字段);添加(passwordField);添加(loginButton);componentSetup();}私有无效componentSetup(){loginButton.setSize(20,10);usernameField.setSize(100,50);passwordField.setSize(100,50);loginButton.setMinimumSize(new Dimension(20,10));usernameField.setMinimumSize(new Dimension(100,50));passwordField.setMinimumSize(new Dimension(100,50));loginButton.setPreferredSize(new Dimension(20,10));usernameField.setPreferredSize(new Dimension(100,50));passwordField.setPreferredSize(new Dimension(100,50));}}
我读到设置最小值,首选大小就足够了,但是看起来好像还不够.
解决方案
我将所有内容都放在一个类中.代码后的解释.
import java.awt.Component;导入java.awt.EventQueue;导入javax.swing.BorderFactory;导入javax.swing.Box;导入javax.swing.BoxLayout;导入javax.swing.JButton;导入javax.swing.JFrame;导入javax.swing.JLabel;导入javax.swing.JPanel;导入javax.swing.JPasswordField;导入javax.swing.JTextField;导入javax.swing.WindowConstants;公共类SuperOne实现了Runnable {私有JButton loginButton;私有JFrame框架;私有JPasswordField passwordField;私有JTextField usernameField;@Override公共无效run(){showGui();}私人JPanel createLoginPanel(){JPanel loginPanel =新的JPanel();BoxLayout布局=新的BoxLayout(loginPanel,BoxLayout.PAGE_AXIS);loginPanel.setLayout(layout);loginPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));JLabel loginLabel = new JLabel("Login");loginLabel.setAlignmentX(Component.CENTER_ALIGNMENT);usernameField = new JTextField(10);usernameField.setAlignmentX(Component.CENTER_ALIGNMENT);passwordField =新的JPasswordField(10);passwordField.setAlignmentX(Component.CENTER_ALIGNMENT);loginButton = new JButton("login");loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);loginPanel.add(loginLabel);loginPanel.add(Box.createVerticalStrut(15));loginPanel.add(usernameField);loginPanel.add(Box.createVerticalStrut(5));loginPanel.add(passwordField);loginPanel.add(Box.createVerticalStrut(5));loginPanel.add(loginButton);返回loginPanel;}私人无效的showGui(){frame = new JFrame();frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);frame.add(createLoginPanel());frame.pack();frame.setLocationByPlatform(true);frame.setVisible(true);}/*** 从这里开始.*/公共静态void main(String [] args){EventQueue.invokeLater(new SuperOne());}}
- 处理GUI组件的所有代码都必须在事件调度线程(EDT)上运行.尽管不是强制性的,但我还是想通过调用
EventQueue.invokeLater()
显式启动EDT. - 使用
BoxLayout
上的教程参考网页,该教程显示在I have a
JFrame
-SuperTest
andJPanel
-SuperLogin
. The login panel has the username and password input fields and a login button. I want it to look like this:but it looks like the pic below, with input fields having too huge height and width.
SuperTest.java:
import javax.swing.*; public class SuperTest extends JFrame { public SuperTest() { add(new SuperLogin()); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(600, 400); } public static void main(String[] args) { SuperTest test = new SuperTest(); } }
SuperLogin.java:
import javax.swing.*; import java.awt.*; public class SuperLogin extends JPanel { private JButton loginButton = new JButton("Login"); private TextField usernameField = new TextField(); private TextField passwordField = new TextField(); public SuperLogin() { BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS); setLayout(layout); add(new JLabel("Login")); add(usernameField); add(passwordField); add(loginButton); componentSetup(); } private void componentSetup() { loginButton.setSize(20, 10); usernameField.setSize(100, 50); passwordField.setSize(100, 50); loginButton.setMinimumSize(new Dimension(20, 10)); usernameField.setMinimumSize(new Dimension(100, 50)); passwordField.setMinimumSize(new Dimension(100, 50)); loginButton.setPreferredSize(new Dimension(20, 10)); usernameField.setPreferredSize(new Dimension(100, 50)); passwordField.setPreferredSize(new Dimension(100, 50)); } }
I read that setting min, preferred size would be enough, but it looks like it's not.
解决方案I put everything into a single class. Explanations after the code.
import java.awt.Component; import java.awt.EventQueue; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.WindowConstants; public class SuperOne implements Runnable { private JButton loginButton; private JFrame frame; private JPasswordField passwordField; private JTextField usernameField; @Override public void run() { showGui(); } private JPanel createLoginPanel() { JPanel loginPanel = new JPanel(); BoxLayout layout = new BoxLayout(loginPanel, BoxLayout.PAGE_AXIS); loginPanel.setLayout(layout); loginPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); JLabel loginLabel = new JLabel("Login"); loginLabel.setAlignmentX(Component.CENTER_ALIGNMENT); usernameField = new JTextField(10); usernameField.setAlignmentX(Component.CENTER_ALIGNMENT); passwordField = new JPasswordField(10); passwordField.setAlignmentX(Component.CENTER_ALIGNMENT); loginButton = new JButton("login"); loginButton.setAlignmentX(Component.CENTER_ALIGNMENT); loginPanel.add(loginLabel); loginPanel.add(Box.createVerticalStrut(15)); loginPanel.add(usernameField); loginPanel.add(Box.createVerticalStrut(5)); loginPanel.add(passwordField); loginPanel.add(Box.createVerticalStrut(5)); loginPanel.add(loginButton); return loginPanel; } private void showGui() { frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(createLoginPanel()); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } /** * Start here. */ public static void main(String[] args) { EventQueue.invokeLater(new SuperOne()); } }
- All code that deals with the GUI components must run on the Event Dispatch Thread (EDT). Although not mandatory, I like to explicitly launch the EDT by calling
EventQueue.invokeLater()
. - Refer to the Web page with the tutorial on
BoxLayout
that appears in the other answer. JTextField
andJPasswordField
both have a columns property. I find that better for setting a desired width than usingsetPreferredSize()
Here is a screen capture of the running app.
这篇关于BoxLayout:无法设置子组件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- All code that deals with the GUI components must run on the Event Dispatch Thread (EDT). Although not mandatory, I like to explicitly launch the EDT by calling