调整登录屏幕按钮的大小 [英] Resize the button of login screen
问题描述
我想调整按钮的大小.我怎样才能做到这一点?
我想将我的登录按钮和文本输入调整为小.我希望我的登录屏幕在中间显示得很好.
首先尝试不同的布局管理器,而不是 GridLayout
,例如 GridBagLayout
:
backPanel = new JPanel(new BorderLayout());midPanel = new JPanel(new GridBagLayout());bottomPanel = new JPanel(new GridBagLayout());//bottomPanel.setLayout(new BorderLayout());//final FrameTestBase = new FrameTestBase();loginButton = new JButton("登录");bottomPanel.add(loginButton);usernameLabel = new JLabel(" 用户名:");passwordLabel = new JLabel(" 密码:");usernameText = new JTextField(20);passwordField = new JPasswordField(20);GridBagConstraints gbc = new GridBagConstraints();gbc.gridx = 0;gbc.gridy = 0;gbc.insets = new Insets(2, 2, 2, 2);midPanel.add(usernameLabel, gbc);gbc.gridx++;midPanel.add(usernameText, gbc);gbc.gridx = 0;gbc.gridy++;midPanel.add(passwordLabel, gbc);gbc.gridx++;midPanel.add(passwordField, gbc);bottomPanel.add(loginButton);backPanel.add(midPanel);backPanel.add(bottomPanel, BorderLayout.SOUTH);
查看在容器内布置组件 和 如何使用 GridBagLayout 了解更多详情.>
例如,您可以使用frame.setLocationRelativeTo(null);
轻松地在屏幕中间显示一个窗口
import java.awt.BorderLayout;导入 java.awt.EventQueue;导入 java.awt.GridBagConstraints;导入 java.awt.GridBagLayout;导入 java.awt.Insets;导入 javax.swing.JButton;导入 javax.swing.JFrame;导入 javax.swing.JLabel;导入 javax.swing.JPanel;导入 javax.swing.JPasswordField;导入 javax.swing.JTextField;导入 javax.swing.UIManager;导入 javax.swing.UnsupportedLookAndFeelException;公共类登录示例{公共静态无效主(字符串 [] args){新的登录示例();}公共登录示例(){EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){尝试 {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {ex.printStackTrace();}JFrame frame = new JFrame("测试");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.add(new LoginPane());框架.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}});}公共类 LoginPane 扩展 JPanel {私人最终JPanel backPanel;私人最终 JPanel midPanel;私人最终 JPanel 底部面板;私有最终 JButton 登录按钮;私有最终 JLabel 用户名标签;私人最终 JLabel 密码标签;private final JTextField usernameText;私人最终 JPasswordField passwordField;公共登录窗格(){backPanel = new JPanel(new BorderLayout());midPanel = new JPanel(new GridBagLayout());bottomPanel = new JPanel(new GridBagLayout());loginButton = new JButton("登录");bottomPanel.add(loginButton);usernameLabel = new JLabel(" 用户名:");passwordLabel = new JLabel(" 密码:");usernameText = new JTextField(20);passwordField = new JPasswordField(20);GridBagConstraints gbc = new GridBagConstraints();gbc.gridx = 0;gbc.gridy = 0;gbc.insets = new Insets(2, 2, 2, 2);midPanel.add(usernameLabel, gbc);gbc.gridx++;midPanel.add(usernameText, gbc);gbc.gridx = 0;gbc.gridy++;midPanel.add(passwordLabel, gbc);gbc.gridx++;midPanel.add(passwordField, gbc);bottomPanel.add(loginButton);backPanel.add(midPanel);backPanel.add(bottomPanel, BorderLayout.SOUTH);setLayout(new BorderLayout());添加(后面板);}}}
I want to resize the button. How can I do that?
I want to resize my login button to be small and my textinput. I want my log in screen to be display nice in middle.
Start by trying different layout managers, other than GridLayout
, like GridBagLayout
for example:
backPanel = new JPanel(new BorderLayout());
midPanel = new JPanel(new GridBagLayout());
bottomPanel = new JPanel(new GridBagLayout());
//bottomPanel.setLayout(new BorderLayout());
//final FrameTestBase = new FrameTestBase();
loginButton = new JButton(" Login ");
bottomPanel.add(loginButton);
usernameLabel = new JLabel(" Username : ");
passwordLabel = new JLabel(" Password : ");
usernameText = new JTextField(20);
passwordField = new JPasswordField(20);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
midPanel.add(usernameLabel, gbc);
gbc.gridx++;
midPanel.add(usernameText, gbc);
gbc.gridx = 0;
gbc.gridy++;
midPanel.add(passwordLabel, gbc);
gbc.gridx++;
midPanel.add(passwordField, gbc);
bottomPanel.add(loginButton);
backPanel.add(midPanel);
backPanel.add(bottomPanel, BorderLayout.SOUTH);
Take a look at Laying Out Components Within a Container and How to Use GridBagLayout for more details.
You can easily display a window in the middle of the screen by using frame.setLocationRelativeTo(null);
, for example
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LoginExample {
public static void main(String[] args) {
new LoginExample();
}
public LoginExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new LoginPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LoginPane extends JPanel {
private final JPanel backPanel;
private final JPanel midPanel;
private final JPanel bottomPanel;
private final JButton loginButton;
private final JLabel usernameLabel;
private final JLabel passwordLabel;
private final JTextField usernameText;
private final JPasswordField passwordField;
public LoginPane() {
backPanel = new JPanel(new BorderLayout());
midPanel = new JPanel(new GridBagLayout());
bottomPanel = new JPanel(new GridBagLayout());
loginButton = new JButton(" Login ");
bottomPanel.add(loginButton);
usernameLabel = new JLabel(" Username : ");
passwordLabel = new JLabel(" Password : ");
usernameText = new JTextField(20);
passwordField = new JPasswordField(20);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
midPanel.add(usernameLabel, gbc);
gbc.gridx++;
midPanel.add(usernameText, gbc);
gbc.gridx = 0;
gbc.gridy++;
midPanel.add(passwordLabel, gbc);
gbc.gridx++;
midPanel.add(passwordField, gbc);
bottomPanel.add(loginButton);
backPanel.add(midPanel);
backPanel.add(bottomPanel, BorderLayout.SOUTH);
setLayout(new BorderLayout());
add(backPanel);
}
}
}
这篇关于调整登录屏幕按钮的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!