最近在学习java中的swing,做了一个小小的程序记录学习成果。
涉及知识点:swing中的gridBagLayout,事件处理等,以及正则表达式。
实现效果图:
实现代码如下:
/**
* swing小程序
* Data : 2018-6-18
* @author magentaLi
*/
public class ScheduleRecords extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel panelLeft;
private JPanel panelRight;
private JList<String> list;
private DefaultListModel<String> listModel;
private JPanel bottomLeft;
private JButton btnAdd;
private JButton btnDel;
private JButton btnClear;
private JLabel dateLabel;
private JLabel timeLabel;
private JLabel eventLabel;
private JLabel palceLabel;
private JLabel participantLabel;
private JLabel remarksLabel;
private JTextField dateTF;
private JTextField timeTF;
private JTextField eventTF;
private JTextField placeTF;
private JTextField participantTF;
private JTextArea remarksTA;
private JPanel bottomRight;
private JButton btnEdit;
private JButton btnCancel;
private JButton btnConfirm;
private Boolean btnEditClicked = false;
private Boolean btnAddClicked = false;
public ScheduleRecords() {
super("日程记录");
init();
}
private void init() {
// 设置左右面板
panelLeft = new JPanel();
panelRight = new JPanel();
// 设置左右面板的边框
panelLeft.setBorder(BorderFactory.createLoweredBevelBorder());
panelRight.setBorder(BorderFactory.createLoweredBevelBorder());
// 采用GridBagLayout布局设置左右面板的位置及大小
GridBagLayout gbLayout = new GridBagLayout();
setLayout(gbLayout);
GridBagConstraints gbCons = new GridBagConstraints();
// 左面板的布局属性
gbCons.gridx = 0;
gbCons.gridy = 0;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
gbCons.fill = GridBagConstraints.BOTH;
gbCons.insets = new Insets(20, 30, 20, 10);
add(panelLeft, gbCons);
// 右面板的布局属性
gbCons.gridx = 1;
gbCons.gridy = 0;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
gbCons.fill = GridBagConstraints.BOTH;
gbCons.insets = new Insets(20, 10, 20, 30);
add(panelRight, gbCons);
// 创建一个 JList 实例
listModel = new DefaultListModel<String>();
listModel.addElement("2018-4-20 14:20 Java考试");
listModel.addElement("2018-5-20 15:50 Oracle考试 ");
listModel.addElement("2018-6-20 19:30 Linux考试 ");
list = new JList<String>(listModel);
list.setFixedCellHeight(50);
// 只允许单选
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// 设置左面板内各个组件的布局
GridBagLayout gbLayoutLeft = new GridBagLayout();
panelLeft.setLayout(gbLayoutLeft);
GridBagConstraints gbConLeft = new GridBagConstraints();
// List的属性
gbConLeft.gridx = 0;
gbConLeft.gridy = 0;
gbConLeft.gridwidth = 3;
gbConLeft.gridheight = 4;
gbConLeft.weightx = 1.0;
gbConLeft.weighty = 1.0;
gbConLeft.fill = GridBagConstraints.BOTH;
gbConLeft.insets = new Insets(10, 10, 10, 10);
panelLeft.add(new JScrollPane(list), gbConLeft);
// 列表区的三个按钮
bottomLeft = new JPanel();
btnAdd = new JButton("添加");
btnDel = new JButton("删除");
btnClear = new JButton("清空");
//设置左侧面板底部的按钮的布局
GridBagLayout bLayout = new GridBagLayout();
bottomLeft.setLayout(bLayout);
GridBagConstraints bCons = new GridBagConstraints();
bCons.gridx = 0;
bCons.gridy = 0;
bCons.gridwidth = 1;
bCons.gridheight = 1;
bCons.weightx = 1.0;
bCons.weighty = 0.0;
bCons.fill = GridBagConstraints.NONE;
bCons.anchor = GridBagConstraints.LINE_START;
bottomLeft.add(btnAdd, bCons);
bCons.gridx = 1;
bCons.anchor = GridBagConstraints.CENTER;
bottomLeft.add(btnDel, bCons);
bCons.gridx = 2;
bCons.anchor = GridBagConstraints.LINE_END;
bottomLeft.add(btnClear, bCons);
//bottomLeft的位置
gbConLeft.gridx = 0;
gbConLeft.gridy = 4;
gbConLeft.gridwidth = 3;
gbConLeft.gridheight = 1;
gbConLeft.weightx = 1.0;
gbConLeft.weighty = 0.0;
gbConLeft.fill = GridBagConstraints.BOTH;
gbConLeft.insets = new Insets(10, 10, 10, 20);
panelLeft.add(bottomLeft, gbConLeft);
// 设置右边面板的布局
GridBagLayout gblayoutRight = new GridBagLayout();
panelRight.setLayout(gblayoutRight);
GridBagConstraints gbConsRight = new GridBagConstraints();
dateLabel = new JLabel("*日期:");
gbConsRight.gridx = 0;
gbConsRight.gridy = 0;
gbConsRight.gridwidth = 1;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.anchor = GridBagConstraints.EAST;
gbConsRight.fill = GridBagConstraints.NONE;
gbConsRight.insets = new Insets(10, 0, 10, 0);
panelRight.add(dateLabel, gbConsRight);
dateTF = new JTextField();
dateTF.setEditable(false);
gbConsRight.gridx = 1;
gbConsRight.gridy = 0;
gbConsRight.gridwidth = 1;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.fill = GridBagConstraints.BOTH;
gbConsRight.anchor = GridBagConstraints.WEST;
panelRight.add(dateTF, gbConsRight);
timeLabel = new JLabel("*时间:");
gbConsRight.gridx = 2;
gbConsRight.gridy = 0;
gbConsRight.gridwidth = 1;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.fill = GridBagConstraints.NONE;
gbConsRight.anchor = GridBagConstraints.EAST;
panelRight.add(timeLabel, gbConsRight);
timeTF = new JTextField();
timeTF.setEditable(false);
gbConsRight.gridx = 3;
gbConsRight.gridy = 0;
gbConsRight.gridwidth = 1;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.insets = new Insets(10, 0, 10, 50);
gbConsRight.fill = GridBagConstraints.BOTH;
panelRight.add(timeTF, gbConsRight);
eventLabel = new JLabel("*事件:");
gbConsRight.gridx = 0;
gbConsRight.gridy = 1;
gbConsRight.gridwidth = 1;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.insets = new Insets(10, 0, 10, 0);
gbConsRight.fill = GridBagConstraints.NONE;
gbConsRight.anchor = GridBagConstraints.EAST;
panelRight.add(eventLabel, gbConsRight);
eventTF = new JTextField();
eventTF.setEditable(false);
gbConsRight.gridx = 1;
gbConsRight.gridy = 1;
gbConsRight.gridwidth = 3;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.fill = GridBagConstraints.HORIZONTAL;
gbConsRight.anchor = GridBagConstraints.WEST;
gbConsRight.insets = new Insets(10, 0, 10, 50);
panelRight.add(eventTF, gbConsRight);
palceLabel = new JLabel("地点:");
gbConsRight.gridx = 0;
gbConsRight.gridy = 2;
gbConsRight.gridwidth = 1;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.ipadx = 0;
gbConsRight.fill = GridBagConstraints.NONE;
gbConsRight.anchor = GridBagConstraints.EAST;
gbConsRight.insets = new Insets(10, 0, 10, 0);
panelRight.add(palceLabel, gbConsRight);
placeTF = new JTextField();
placeTF.setEditable(false);
gbConsRight.gridx = 1;
gbConsRight.gridy = 2;
gbConsRight.gridwidth = 3;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.fill = GridBagConstraints.BOTH;
gbConsRight.insets = new Insets(10, 0, 10, 50);
panelRight.add(placeTF, gbConsRight);
participantLabel = new JLabel("参与者:");
gbConsRight.gridx = 0;
gbConsRight.gridy = 3;
gbConsRight.gridwidth = 1;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.fill = GridBagConstraints.NONE;
gbConsRight.anchor = GridBagConstraints.EAST;
gbConsRight.insets = new Insets(10, 0, 10, 0);
panelRight.add(participantLabel, gbConsRight);
participantTF = new JTextField();
participantTF.setEditable(false);
gbConsRight.gridx = 1;
gbConsRight.gridy = 3;
gbConsRight.gridwidth = 3;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.fill = GridBagConstraints.BOTH;
gbConsRight.insets = new Insets(10, 0, 10, 50);
panelRight.add(participantTF, gbConsRight);
remarksLabel = new JLabel("备注:");
gbConsRight.gridx = 0;
gbConsRight.gridy = 4;
gbConsRight.gridwidth = 1;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.fill = GridBagConstraints.NONE;
gbConsRight.anchor = GridBagConstraints.EAST;
gbConsRight.insets = new Insets(10, 0, 10, 0);
panelRight.add(remarksLabel, gbConsRight);
remarksTA = new JTextArea();
remarksTA.setEditable(false);
gbConsRight.gridx = 1;
gbConsRight.gridy = 4;
gbConsRight.gridwidth = 3;
gbConsRight.gridheight = 4;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 1.0;
gbConsRight.ipadx = 300;
gbConsRight.ipady = 60;
gbConsRight.fill = GridBagConstraints.BOTH;
gbConsRight.insets = new Insets(10, 0, 10, 50);
panelRight.add(remarksTA, gbConsRight);
bottomRight = new JPanel(bLayout);
btnEdit = new JButton("编辑");
btnCancel = new JButton("取消");
btnConfirm = new JButton("确定");
bCons.gridx = 0;
bCons.gridy = 0;
bCons.gridwidth = 1;
bCons.gridheight = 1;
bCons.weightx = 1.0;
bCons.weighty = 0.0;
bCons.fill = GridBagConstraints.NONE;
bCons.anchor = GridBagConstraints.LINE_START;
bottomRight.add(btnEdit, bCons);
bCons.gridx = 1;
bCons.anchor = GridBagConstraints.EAST;
bottomRight.add(btnCancel, bCons);
bCons.gridx = 2;
bCons.anchor = GridBagConstraints.LINE_END;
bottomRight.add(btnConfirm, bCons);
gbConsRight.gridx = 1;
gbConsRight.gridy = 9;
gbConsRight.gridwidth = 3;
gbConsRight.gridheight = 1;
gbConsRight.weightx = 1.0;
gbConsRight.weighty = 0.0;
gbConsRight.ipadx = 0;
gbConsRight.ipady = 0;
gbConsRight.fill = GridBagConstraints.BOTH;
panelRight.add(bottomRight, gbConsRight);
// 主面板的大小
setSize(1000, 600);
// 设置主面板点击可关闭
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// 设置主面板的可见性
setVisible(true);
// 事件处理
//list默认选中第一项
list.setSelectedIndex(0);
//默认各个文本框中的内容为list的第一项的内容
setTFText();
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
//设置只有释放鼠标时才触发
if (!list.getValueIsAdjusting()) {
//设置右侧面板中的文本框的内容为选中项的内容
setTFText();
}
}
});
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btnAddClicked = true;
//清空右侧的文本框
clearAllTF();
}
});
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//设置右侧各个文本框的为不可编辑的
setTFEditable(false);
setTFText();
}
});
btnConfirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Boolean dateInputCorrect = true;
Boolean timeInputCorrect = true;
Boolean eventInputCorrect = true;
String date = dateTF.getText();
String time = timeTF.getText();
String event = eventTF.getText();
if (btnEditClicked) {
if (!isDate(date)) {
JOptionPane.showMessageDialog(panelRight, "日期错误或日期格式错误!", "发生错误", JOptionPane.WARNING_MESSAGE);
dateInputCorrect = false;
}
if (!isTime(time)) {
JOptionPane.showMessageDialog(panelRight, "时间格式错误!", "发生错误", JOptionPane.WARNING_MESSAGE);
timeInputCorrect = false;
}
if (!eventExits()) {
JOptionPane.showMessageDialog(panelRight, "事件未填写!", "发生错误", JOptionPane.WARNING_MESSAGE);
eventInputCorrect = false;
}
if (dateInputCorrect && timeInputCorrect && eventInputCorrect) {
String items = date + " " + time + " " + event;
listModel.setElementAt(items, list.getSelectedIndex());
}
btnEditClicked = false;
}
if (btnAddClicked) {
if (!isDate(date)) {
JOptionPane.showMessageDialog(panelRight, "日期错误或日期格式错误!", "发生错误", JOptionPane.WARNING_MESSAGE);
dateInputCorrect = false;
}
if (!isTime(time)) {
JOptionPane.showMessageDialog(panelRight, "时间格式错误!", "发生错误", JOptionPane.WARNING_MESSAGE);
timeInputCorrect = false;
}
if (!eventExits()) {
JOptionPane.showMessageDialog(panelRight, "事件未填写!", "发生错误", JOptionPane.WARNING_MESSAGE);
eventInputCorrect = false;
}
if (dateInputCorrect && timeInputCorrect && eventInputCorrect) {
String items = date + " " + time + " " + event;
listModel.addElement(items);
}
btnAddClicked = false;
}
setTFEditable(false);
}
});
btnDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 如果用户已经选中的一项
if (list.getSelectedIndex() >= 0) {
Object[] options = { "删除", "取消 " };
int response = JOptionPane.showOptionDialog(panelLeft, "是否删除选中项?", "请确认 ", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (response == 0) {
clearAllTF();
listModel.removeElementAt(list.getSelectedIndex());
}
} else if (listModel.size() == 0) {
JOptionPane.showMessageDialog(panelLeft, "列表为空", "警告", JOptionPane.WARNING_MESSAGE);
} else if (list.getSelectedIndex() <= 0) {
JOptionPane.showMessageDialog(panelLeft, "没有选中任何项目", "警告", JOptionPane.WARNING_MESSAGE);
}
}
});
btnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listModel.size() == 0) {
JOptionPane.showMessageDialog(panelLeft, "列表为空", "警告", JOptionPane.WARNING_MESSAGE);
} else {
Object[] options = { "清空", "取消 " };
int response = JOptionPane.showOptionDialog(panelLeft, "确定清空列表?", "请确认 ", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (response == 0) {
clearAllTF();
listModel.removeAllElements();
}
}
}
});
btnEdit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btnEditClicked = true;
if (list.getSelectedIndex() >= 0) {
setTFEditable(true);
} else {
JOptionPane.showMessageDialog(panelRight, "未选中列表中的任何一项", "警告", JOptionPane.WARNING_MESSAGE);
}
}
});
}
public static void main(String[] args) {
ScheduleRecords sr = new ScheduleRecords();
}
// 设置各个文本框中的内容
public void setTFText() {
if (list.getSelectedIndex() >= 0) {
String str = list.getSelectedValue();
String[] items = str.split(" ");
dateTF.setText(items[0]);
timeTF.setText(items[1]);
eventTF.setText(items[2]);
placeTF.setText("实验楼西303");
participantTF.setText("全体同学老师!");
remarksTA.setText("可以带书和API文档 \r\n考试内容是Java Swing\r\n考试时间两个小时\r\n");
setTFEditable(false);
}
}
//清空所有的文本框
public void clearAllTF() {
int i = panelRight.getComponentCount();
for (int j = 0; j < i; j++) {
Component comp = panelRight.getComponent(j);
if (comp instanceof JTextField) {
JTextField tf = (JTextField) comp;
tf.setText("");
tf.setEditable(true);
}
if (comp instanceof JTextArea) {
JTextArea ta = (JTextArea) comp;
ta.setText("");
ta.setEditable(true);
}
}
}
//判断日期是否合法
public boolean isDate(String date) {
Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?"
+ "((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|"
+ "(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|"
+ "(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|"
+ "([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|"
+ "([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$");
return p.matcher(date).matches();
}
//判断时间是否合法
public boolean isTime(String time) {
Pattern p = Pattern.compile("((((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])))?$");
return p.matcher(time).matches();
}
//判断是否填写了事件
public boolean eventExits() {
if ("".equals(eventTF.getText())) {
return false;
} else {
return true;
}
}
//设置文本框是否可编辑
public void setTFEditable(Boolean CanOrCanot) {
int i = panelRight.getComponentCount();
for (int j = 0; j < i; j++) {
Component comp = panelRight.getComponent(j);
if (comp instanceof JTextField) {
JTextField tf = (JTextField) comp;
tf.setEditable(CanOrCanot);
}
if (comp instanceof JTextArea) {
JTextArea ta = (JTextArea) comp;
ta.setEditable(CanOrCanot);
}
}
}
}
总结:Swing有些笨重,用的也不太熟练,还需加油!