java中gui的三阶段_Java 由浅入深GUI编程实战练习(三)

import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.table.*;public class Demo6 extendsJApplet {private static final long serialVersionUID = 1L;public static final String WEEK_SUN = "SUN";public static final String WEEK_MON = "MON";public static final String WEEK_TUE = "TUE";public static final String WEEK_WED = "WED";public static final String WEEK_THU = "THU";public static final String WEEK_FRI = "FRI";public static final String WEEK_SAT = "SAT";public static final Color background =Color.white;public static final Color foreground =Color.black;public static final Color headerBackground =Color.blue;public static final Color headerForeground =Color.white;public static final Color selectedBackground =Color.blue;public static final Color selectedForeground =Color.white;privateJPanel cPane;privateJLabel yearsLabel;privateJSpinner yearsSpinner;privateJLabel monthsLabel;private JComboBoxmonthsComboBox;privateJTable daysTable;privateAbstractTableModel daysModel;privateCalendar calendar;publicDemo6() {

cPane=(JPanel) getContentPane();

}public voidinit() {

cPane.setLayout(newBorderLayout());

calendar=Calendar.getInstance();

yearsLabel= new JLabel("Year: ");

yearsSpinner= newJSpinner();//利用编辑器

yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));

yearsSpinner.setValue(newInteger(calendar.get(Calendar.YEAR)));

yearsSpinner.addChangeListener(newChangeListener() {

@Overridepublic voidstateChanged(ChangeEvent e) {//TODO Auto-generated method stub

int day =calendar.get(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH,1);

calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue()).intValue());int maxDay =calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, day> maxDay ?maxDay : day);

updateView();

}

});

JPanel yearMonthPanel= newJPanel();

cPane.add(yearMonthPanel, BorderLayout.NORTH);

yearMonthPanel.setLayout(newBorderLayout());

yearMonthPanel.add(newJPanel(), BorderLayout.CENTER);

JPanel yearPanel= newJPanel();

yearMonthPanel.add(yearPanel, BorderLayout.WEST);

yearPanel.setLayout(newBorderLayout());

yearPanel.add(yearsLabel, BorderLayout.WEST);

yearPanel.add(yearsSpinner, BorderLayout.CENTER);

monthsLabel= new JLabel("Month: ");

monthsComboBox= new JComboBox();for (int i = 1; i <= 12; i++) {

monthsComboBox.addItem(newInteger(i));

}

monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));

monthsComboBox.addActionListener(newActionListener() {

@Overridepublic voidactionPerformed(ActionEvent actionEvent) {int day =calendar.get(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH,1);

calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());int maxDay =calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, day> maxDay ?maxDay : day);

updateView();

}

});

JPanel monthPanel= newJPanel();

yearMonthPanel.add(monthPanel, BorderLayout.EAST);

monthPanel.setLayout(newBorderLayout());

monthPanel.add(monthsLabel, BorderLayout.WEST);

monthPanel.add(monthsComboBox, BorderLayout.CENTER);

daysModel= newAbstractTableModel() {/****/

private static final long serialVersionUID = -3517455337953024330L;public intgetRowCount() {return 7;

}public intgetColumnCount() {return 7;

}public Object getValueAt(int row, intcolumn) {if (row == 0) {returngetHeader(column);

}

row--;

Calendar calendar= (Calendar) Demo6.this.calendar.clone();

calendar.set(Calendar.DAY_OF_MONTH,1);int dayCount =calendar.getActualMaximum(Calendar.DAY_OF_MONTH);int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;int index = row * 7 +column;int dayIndex = index - moreDayCount + 1;if (index < moreDayCount || dayIndex >dayCount) {return null;

}else{return newInteger(dayIndex);

}

}

};

daysTable= newCalendarTable(daysModel, calendar);

daysTable.setCellSelectionEnabled(true);

daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

daysTable.setDefaultRenderer(daysTable.getColumnClass(0), newTableCellRenderer() {public Component getTableCellRendererComponent(JTable table, Object value, booleanisSelected,boolean hasFocus, int row, intcolumn) {

String text= (value == null) ? "": value.toString();

JLabel cell= newJLabel(text);

cell.setOpaque(true);if (row == 0) {

cell.setForeground(headerForeground);

cell.setBackground(headerBackground);

}else{if(isSelected) {

cell.setForeground(selectedForeground);

cell.setBackground(selectedBackground);

}else{

cell.setForeground(foreground);

cell.setBackground(background);

}

}returncell;

}

});

updateView();

cPane.add(daysTable, BorderLayout.CENTER);

}public static String getHeader(intindex) {switch(index) {case 0:returnWEEK_SUN;case 1:returnWEEK_MON;case 2:returnWEEK_TUE;case 3:returnWEEK_WED;case 4:returnWEEK_THU;case 5:returnWEEK_FRI;case 6:returnWEEK_SAT;default:return null;

}

}public voidupdateView() {

daysModel.fireTableDataChanged();

daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),

calendar.get(Calendar.WEEK_OF_MONTH));

daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK)- 1,

calendar.get(Calendar.DAY_OF_WEEK)- 1);

}public static class CalendarTable extendsJTable {private static final long serialVersionUID = 1L;privateCalendar calendar;publicCalendarTable(TableModel model, Calendar calendar) {super(model);this.calendar =calendar;

}public void changeSelection(int row, int column, boolean toggle, booleanextend) {super.changeSelection(row, column, toggle, extend);if (row == 0) {return;

}

Object obj=getValueAt(row, column);if (obj != null) {

calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());

}

}

}public static voidmain(String[] args) {

JFrame frame= new JFrame("Calendar Application");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Demo6 myCalendar= newDemo6();

myCalendar.init();

frame.getContentPane().add(myCalendar);

frame.setSize(240, 172);

}

}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值