新普京网站-澳门新普京 > 计算机 > 日历显示读出输入的年月的java代码,上机实践六

日历显示读出输入的年月的java代码,上机实践六

2020/01/05 13:36

/*
*Teacher.java
**/
public class Teacher{
    int numberOne,numberTwo;
    String operator="";
    boolean right;
    public int giveNumberOne(int n){
        numberOne = (int)(Math.random()*n)+1;
        return numberOne;
    }    
    public int giveNumberTwo(int n){
        numberTwo = (int)(Math.random()*n)+1;
        return numberTwo;
    }
    public String giveOperator(){
        double d = Math.random();
        if(d>=0.5){
            operator="+";
        }else{
            operator="-";
        }
        return operator;
    }
    
    public boolean getRight(int answer){
        if(operator.equals("+")){
            if(answer==numberOne+numberTwo)
                right=true;
            else
                right=false;
        }else if(operator.equals("-")){
            if(answer==numberOne-numberTwo)
                right=true;
            else
                right=false;
        }
        return right;
    }
}

日历呈现读出输入的时刻的java代码

 那篇文章首要介绍了日历展现读出输入的年华的java代码,有要求的爱侣能够仿效一下

 

代码如下:

import java.awt.*;

import java.awt.event.*;

import java.util.Calendar;

 

import javax.swing.JOptionPane;

 

class CalendarBean {  //定义一个日历类

 String day[]; //天数数组

 int year = 二零一三, month = 9; //给定三个上马年月

 

 public void setYear(int year) { 

  this.year = year;

 }

 

 public int getYear() {

  return year;

 }

 

 public void setMonth(int month) {

  this.month = month;

 }

 

 public int getMonth() {

  return month;

 }

 

 public String[] getCalendar(卡塔尔(قطر‎ { //得到日历

  String a[] = new String[42]; //定义多少个以字符串数组

  Calendar 日历 = Calendar.getInstance(); 

  日历.set(year, month - 1, 1);

  int 星期几 = 日历.get(Calendar.DAY_OF_WEEK) - 1;

  int day = 0;

  if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {

   day = 31;

  }

  if (month == 4 || month == 6 || month == 9 || month == 11) {

   day = 30;

  }

  if (month == 2) {

   if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {

    day = 29;

   } else {

    day = 28;

   }

  }

  for (int i = 星期几, n = 1; i < 星期几 + day; i++) {

   a[i] = String.valueOf(n);

   n++;

  }

  return a;

 }

}

 

class CalendarFrame extends Frame implements ActionListener {

 Label labelDay[] = new Label[42]; //用来输出日历的天的数组

 Button titleName[] = new Button[7]; //周生机勃勃到星期六的开关

 String name[] = { "日", "一", "二", "三", "四", "五", "六" };

 TextField text1, text2; //定义输入的年份和月份

 Button nextMonth, previousMonth, Enter; //下个月,上个月,确定

 Label lab1, lab2, lab3; //多少个文本类型的字符串

 int year = 2012, month = 9;

 CalendarBean calendar; //顶贰个量

 Label showMessage = new Label("", Label.CENTERAV4State of Qatar; //   定义二个用来体现当前年月的label 评释,标签上应为焦点。

 

 public CalendarFrame() {    //窗体类

  Panel pCenter = new Panel();  //镶嵌

  pCenter.setLayout(new GridLayout(7, 7));

  for (int i = 0; i < 7; i++卡塔尔国 {      //给周六到周日的button增添突显文本

   titleName[i] = new Button(name[i]);

   pCenter.add(titleName[i]);

  }

  for (int i = 0; i < 42; i++) {      

   labelDay[i] = new Label("", Label.CENTER);

   pCenter.add(labelDay[i]);

  }

  calendar = new CalendarBean();

  calendar.setYear(year);     //设定年

  calendar.setMonth(month);

  String day[] = calendar.getCalendar();  //设定天

  for (int i = 0; i < 42; i++State of Qatar {     //给日历地点循环加多展现日历天

   labelDay[i].setText(day[i]);

  }

  lab1 = new Label("请输入日期"State of Qatar;  //调用二个方式    new多少个目的 

  lab2 = new Label("年份");

  lab3 = new Label("月份");

  Enter = new Button("确定");

  text1 = new TextField(10);

  text2 = new TextField(5);

  nextMonth = new Button("下月");

  previousMonth = new Button("上月");

  Enter.addActionListener(this卡塔尔(قطر‎;  //增多内定的动作侦听器

  nextMonth.addActionListener(this);

  previousMonth.addActionListener(this);

  Panel pNorth = new Panel(), pSouth = new Panel();

  pNorth.add(lab1);

  pNorth.add(lab2);

  pNorth.add(text1);

  pNorth.add(lab3);

  pNorth.add(text2);

  pNorth.add(Enter);

  pNorth.add(previousMonth);

  pNorth.add(nextMonth);

  pSouth.add(showMessage);

  showMessage.setText("日历:" + calendar.getYear() + "年" + calendar.getMonth() + "月");

  ScrollPane scrollPane = new ScrollPane();  //达成单个子组件的全自动水平和/或垂直滚动的容器类

  scrollPane.add(pCenter); 

  add(scrollPane, BorderLayout.CENTER);

  add(pNorth, BorderLayout.NORTH);

  add(pSouth, BorderLayout.SOUTH);

 }

 

 public void actionPerformed(ActionEvent e) {

  if (e.getSource(State of Qatar == nextMonth卡塔尔(قطر‎ { //假设获得的操作时前些时间的

   month = month + 1;

   if (month > 12)

    month = 1;

   calendar.setMonth(month);

   String day[] = calendar.getCalendar();

   for (int i = 0; i < 42; i++) {

    labelDay[i].setText(day[i]);

   }

  } else if (e.getSource() == previousMonth) {

   month = month - 1;

   if (month < 1)

    month = 12;

   calendar.setMonth(month);

   String day[] = calendar.getCalendar();

   for (int i = 0; i < 42; i++) {

    labelDay[i].setText(day[i]);

   }

  } else {

   String yea = text1.getText();

   String mon = text2.getText();

   try {

    year = Integer.parseInt(yea); //把string转成int类型 

    month = Integer.parseInt(mon);

    if (month > 12 || month < 1 || year < 1卡塔尔(قطر‎ {  //错误输入的拍卖

     JOptionPane.showMessageDialog(null, "请输入正确月份或月份"卡塔尔国; 

     return;

    } else {

     calendar.setYear(year);

     calendar.setMonth(month);

    }

    String day[] = calendar.getCalendar();

    for (int i = 0; i < 42; i++) {

     labelDay[i].setText(day[i]);

    }

   } catch (NumberFormatException ee) {

    JOptionPane.showMessageDialog(null, "请输入正确的年度及月份"卡塔尔国;

   }

  }

  showMessage.setText("日历:" + calendar.getYear() + "年" + calendar.getMonth() + "月");

 }

 

}

 

public class CalendarMainClass {

 public static void main(String args[]) {

  CalendarFrame frame = new CalendarFrame();

  frame.setTitle("日历");

  frame.setBounds(300, 200, 500, 300);

  frame.setVisible(trueState of Qatar;  //显示窗口

  frame.validate(); //使生效

  frame.addWindowListener(new java.awt.event.WindowAdapter(卡塔尔(قطر‎ {  //点击叉号关闭frame程序

   public void windowClosing(java.awt.event.WindowEvent e) {

    System.exit(0); 

   }

  });

 }

}

 

那篇文章主要介绍了日历呈现读出输入的时日的java代码,有需求的对象能够参谋一下 代码如下: import...

 /*

*ComputerFrame.java
**/
import java.awt.*;
import java.awt.event.*;
public class ComputerFrame extends Frame implements ActionListener{
    TextField textOne,textTwo,textResult;
    Button getProblem,giveAnwser;
    Label operatorLabel,message;
    
    Teacher teacher;
    ComputerFrame(String s){
        super(s);
        teacher = new Teacher();
        setLayout(new FlowLayout());
        textOne=new TextField("",10);
        textTwo=new TextField("",10);
        textResult=new TextField("",10);
        operatorLabel=new Label("+");
        message=new Label("你还未答复难题吗"卡塔尔国;
        getProblem=new Button("获取标题"State of Qatar;
        giveAnwser=new Button("确认答案"卡塔尔(قطر‎;
        
        add(getProblem);
        add(textOne);
        add(operatorLabel);
        add(textTwo);
        add(new Label("="));
        add(textResult);
        add(giveAnwser);
        add(message);
        
        textResult.requestFocus();
        textOne.setEditable(false);
        textTwo.setEditable(false);
        
        //注册事件监听器
        getProblem.addActionListener(this);
        giveAnwser.addActionListener(this);
        textResult.addActionListener(this);
        
        setBounds(100,100,450,100);
        setVisible(true);
        validate();
        this.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource(卡塔尔==getProblem卡塔尔(قطر‎{//判定事件源是或不是是getProblem
            int number1=teacher.giveNumberOne(100);
            int number2=teacher.giveNumberTwo(100);
            String operator=teacher.giveOperator();
            textOne.setText(""+number1);
            textTwo.setText(""+number2);
            operatorLabel.setText(operator);
            message.setText("请回答");
            textResult.setText(null);
        }
        if(e.getSource(卡塔尔(قطر‎==giveAnwserState of Qatar{//决断事件源是不是是giveAnwser
            String answer=textResult.getText();
            try{
                int result=Integer.parseInt(answer);
                if(teacher.getRight(result)==true){
                    message.setText("回答正确"卡塔尔;
                }else{
                    message.setText("回答错误"State of Qatar;
                }
            }catch(NumberFormatException ex){
                message.setText("请输入数字字符串"卡塔尔;
            }    
        }
        textResult.requestFocus();
        validate();
    }
}

上一篇:【澳门新普京】客户管理,MySQL分界面工具Sqlyog用法 下一篇:没有了