JPanel formPanel = new JPanel(); formPanel.setLayout(new GridLayout(2, 2)); usernameLabel = new JLabel("Username:"); usernameField = new JTextField(); passwordLabel = new JLabel("Password:"); passwordField = new JPasswordField(); formPanel.add(usernameLabel); formPanel.add(usernameField); formPanel.add(passwordLabel); formPanel.add(passwordField); add(formPanel, BorderLayout.CENTER);怎么使其中文本框适应大小,居中对齐

时间: 2023-12-27 13:05:34 浏览: 23
你可以使用JTextField的setHorizontalAlignment方法将文本框中的文本居中对齐,通过设置preferredSize属性来适应大小。 例如: ``` usernameField.setPreferredSize(new Dimension(200, 30)); // 设置文本框大小 usernameField.setHorizontalAlignment(JTextField.CENTER); // 设置文本居中对齐 ``` 同样的,也可以用同样的方式对passwordField进行设置。
相关问题

创建一个注册窗口通过java

import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class RegisterWindow extends JFrame implements ActionListener { private JLabel nameLabel, emailLabel, passwordLabel, confirmPasswordLabel; private JTextField nameField, emailField; private JPasswordField passwordField, confirmPasswordField; private JButton registerButton; private JCheckBox agreeCheckBox; public RegisterWindow() { setTitle("注册"); setSize(400, 300); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); JPanel formPanel = new JPanel(); formPanel.setLayout(new GridLayout(5, 2, 5, 10)); nameLabel = new JLabel("用户名"); formPanel.add(nameLabel); nameField = new JTextField(); formPanel.add(nameField); emailLabel = new JLabel("电子邮箱"); formPanel.add(emailLabel); emailField = new JTextField(); formPanel.add(emailField); passwordLabel = new JLabel("密码"); formPanel.add(passwordLabel); passwordField = new JPasswordField(); formPanel.add(passwordField); confirmPasswordLabel = new JLabel("确认密码"); formPanel.add(confirmPasswordLabel); confirmPasswordField = new JPasswordField(); formPanel.add(confirmPasswordField); agreeCheckBox = new JCheckBox("同意协议"); formPanel.add(agreeCheckBox); registerButton = new JButton("注册"); registerButton.addActionListener(this); formPanel.add(registerButton); mainPanel.add(formPanel, BorderLayout.CENTER); setContentPane(mainPanel); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == registerButton) { String name = nameField.getText(); String email = emailField.getText(); String password = new String(passwordField.getPassword()); String confirmPassword = new String(confirmPasswordField.getPassword()); boolean agree = agreeCheckBox.isSelected(); if (name.trim().equals("") || email.trim().equals("") || password.trim().equals("")) { JOptionPane.showMessageDialog(this, "请填写完整信息"); return; } if (!password.equals(confirmPassword)) { JOptionPane.showMessageDialog(this, "两次密码不一致"); return; } if (!agree) { JOptionPane.showMessageDialog(this, "请同意协议"); return; } JOptionPane.showMessageDialog(this, "注册成功"); } } public static void main(String[] args) { new RegisterWindow(); } }

Java运用Swing控件完成以下窗体制作,最上面是借用物品作为标题,中间为需要输入的员工编号,借用器具,借用日期,归还日期,借用原因,最下面有确定,取消两个按钮

好的,我可以帮你完成这个窗体的制作。首先,我们需要创建一个 JFrame 对象,用于承载整个窗体。然后,我们可以使用 JPanel 对象来实现上、中、下三个部分的布局。 代码如下: ```java import javax.swing.*; import java.awt.*; public class BorrowForm extends JFrame { // 控件 private JLabel labelTitle, labelId, labelTool, labelBorrowDate, labelReturnDate, labelReason; private JTextField textFieldId, textFieldTool, textFieldBorrowDate, textFieldReturnDate, textFieldReason; private JButton buttonOk, buttonCancel; public BorrowForm() { // 设置窗体标题 setTitle("借用物品"); // 设置窗体大小 setSize(400, 300); // 设置窗体位置居中 setLocationRelativeTo(null); // 设置窗体关闭时退出程序 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建面板 JPanel panel = new JPanel(); // 设置面板布局 panel.setLayout(new BorderLayout()); // 创建标题标签 labelTitle = new JLabel("借用物品"); labelTitle.setFont(new Font("微软雅黑", Font.BOLD, 24)); labelTitle.setHorizontalAlignment(SwingConstants.CENTER); // 添加标题标签到面板上 panel.add(labelTitle, BorderLayout.NORTH); // 创建表单面板 JPanel formPanel = new JPanel(); formPanel.setLayout(new GridLayout(5, 2, 10, 10)); formPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 创建员工编号标签和文本框 labelId = new JLabel("员工编号:"); textFieldId = new JTextField(); formPanel.add(labelId); formPanel.add(textFieldId); // 创建借用器具标签和文本框 labelTool = new JLabel("借用器具:"); textFieldTool = new JTextField(); formPanel.add(labelTool); formPanel.add(textFieldTool); // 创建借用日期标签和文本框 labelBorrowDate = new JLabel("借用日期:"); textFieldBorrowDate = new JTextField(); formPanel.add(labelBorrowDate); formPanel.add(textFieldBorrowDate); // 创建归还日期标签和文本框 labelReturnDate = new JLabel("归还日期:"); textFieldReturnDate = new JTextField(); formPanel.add(labelReturnDate); formPanel.add(textFieldReturnDate); // 创建借用原因标签和文本框 labelReason = new JLabel("借用原因:"); textFieldReason = new JTextField(); formPanel.add(labelReason); formPanel.add(textFieldReason); // 添加表单面板到主面板上 panel.add(formPanel, BorderLayout.CENTER); // 创建按钮面板 JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); // 创建确定按钮 buttonOk = new JButton("确定"); buttonOk.setPreferredSize(new Dimension(80, 30)); buttonPanel.add(buttonOk); // 创建取消按钮 buttonCancel = new JButton("取消"); buttonCancel.setPreferredSize(new Dimension(80, 30)); buttonPanel.add(buttonCancel); // 添加按钮面板到主面板上 panel.add(buttonPanel, BorderLayout.SOUTH); // 添加主面板到窗体上 add(panel); // 显示窗体 setVisible(true); } } ``` 这段代码会创建一个 JFrame 对象,其中包含了一个标题为“借用物品”的 JLabel 对象,一个包含员工编号、借用器具、借用日期、归还日期、借用原因的表单,以及一个确定按钮和一个取消按钮。 你只需要在你的代码中调用 BorrowForm 的构造方法即可创建出这个窗体。

相关推荐

最新推荐

recommend-type

6-10.py

6-10
recommend-type

基于机器学习的入侵检测系统+源码+说明.zip

基于机器学习的入侵检测系统+源码+说明.zip
recommend-type

matlab基于潜在低秩表示的红外与可见光图像融合.zip

matlab基于潜在低秩表示的红外与可见光图像融合.zip
recommend-type

4-5.py

4-5
recommend-type

基于tensorflow使用简单线性回归实现波士顿房价预测源码.zip

基于tensorflow使用简单线性回归实现波士顿房价预测源码.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。