设计住户面板的java代码
时间: 2023-06-12 17:03:10 浏览: 85
基于Java的小区住户管理系统的设计与实现【附源码】
以下是一个简单的设计住户面板的Java代码示例:
```
import java.awt.*;
import javax.swing.*;
public class ResidentPanel extends JPanel {
private JLabel nameLabel;
private JTextField nameField;
private JLabel ageLabel;
private JTextField ageField;
private JCheckBox smokingCheckBox;
private JButton saveButton;
private JButton cancelButton;
public ResidentPanel() {
setLayout(new GridLayout(4, 2));
nameLabel = new JLabel("Name:");
add(nameLabel);
nameField = new JTextField(20);
add(nameField);
ageLabel = new JLabel("Age:");
add(ageLabel);
ageField = new JTextField(3);
add(ageField);
smokingCheckBox = new JCheckBox("Smoking?");
add(smokingCheckBox);
saveButton = new JButton("Save");
add(saveButton);
cancelButton = new JButton("Cancel");
add(cancelButton);
}
// getters and setters for fields
public String getName() {
return nameField.getText();
}
public void setName(String name) {
nameField.setText(name);
}
public int getAge() {
return Integer.parseInt(ageField.getText());
}
public void setAge(int age) {
ageField.setText(Integer.toString(age));
}
public boolean isSmoking() {
return smokingCheckBox.isSelected();
}
public void setSmoking(boolean smoking) {
smokingCheckBox.setSelected(smoking);
}
// main method for testing
public static void main(String[] args) {
JFrame frame = new JFrame("Resident Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ResidentPanel panel = new ResidentPanel();
panel.setName("John Smith");
panel.setAge(35);
panel.setSmoking(true);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
这个住户面板包含一个姓名文本框,一个年龄文本框,一个吸烟复选框和两个按钮(保存和取消)。它使用网格布局将这些组件排列在一起,并提供了一些getter和setter方法来访问和修改这些组件的值。在main方法中,我们创建了一个新的JFrame窗口,将该面板添加到其中,并设置了一些默认值。
阅读全文