- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Calendar;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
- /**
- * 从键盘输入一个年份,将这一年中所有周日是几号显示出来。
- 周日是几号如何显示求学长分析。谢谢!
- */
- class WeekOfYear extends JFrame {
- //创建成员变量
- private JTextField filed = new JTextField(25);
- private JTextArea txt = new JTextArea(5,25);
- private JButton button = new JButton("显示");
- private JButton clear = new JButton("清除");
- private Calendar calendar=Calendar.getInstance();
- public WeekOfYear(){
- init();
- }
- //初始化窗体和组件
- private void init() {
- setLayout(new FlowLayout());
- setResizable(false);
- setLocation(300,100);
- add(filed);
- add(new JScrollPane(txt));
- add(button);
- add(clear);
- event();
- }
- public void event(){
- button.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- String str=filed.getText().trim();
- if((str.matches("\\D+"))){
- JOptionPane.showMessageDialog(null, "请输入中文年份!");
- filed.setText("");
- return ;
- }
- int count=1;
- int month,day;
- for(int i=0;i<7;i++){
- calendar.set(Integer.parseInt(str), 0,i);
- if(calendar.get(Calendar.DAY_OF_WEEK)==1)
- break;
- }
- txt.append("月份:"+(calendar.get(Calendar.MONTH)+1)+" 本月第:"
- +calendar.get(Calendar.DAY_OF_MONTH)+"天\n");
- while(calendar.get(Calendar.WEEK_OF_YEAR)!=1){
- calendar.add(Calendar.DAY_OF_YEAR, +7);
- month=calendar.get(Calendar.MONTH)+1;
- day=calendar.get(Calendar.DAY_OF_MONTH);
- txt.append("月份:"+month+" 本月第:"+day+"日\n");
- count++;
- }
- txt.append("本月度一共:"+count+"周!");
- }
- });
- clear.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- txt.setText("");
- }
- });
- }
- public static void main(String[] args) {
- SwingConsole.run(new WeekOfYear(), 300, 200);
- }
- }
- public class SwingConsole extends JFrame {
- public static void run(final JFrame frame,final int width,final int height){
- SwingUtilities.invokeLater(new Runnable(){
- public void run(){
- frame.setVisible(true);
- frame.setSize(width,height);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- });
- }
- }
复制代码 |