今日は、Java Swingを使用して簡単な小さなカレンダーを開発する方法を紹介します。以下のコードを見てみましょう。
基本的な日付計算のために最初にカレンダービーンクラスを作成します。
パッケージother1; java.util.calendarをインポートします。 public class calendarbean {string day []; int year = 2005、月= 0; public void setyear(int year){this.year = year; } public intyear(){return year; } public void setmonth(int month){this.month = month; } public int getMonth(){return month; } public string [] getCalendar(){string a [] = new String [42];カレンダーdate = calendar.getInstance(); date.set(year、month-1,1); int week = date.get(calendar.day_of_week)-1; int day = 0; //大規模な月を裁判官if(月== 1 ||月== 3 ||月== 5 ||月== 7 ||月== 8 ||月== 10 ||月== 12){day = 31; } //小規模な月を裁判官if(月== 4 ||月== 6 ||月== 9 ||月== 11){day = 30; } //通常の年と跳躍年を裁判官if(month == 2){if(((year%4 == 0)&&(year%100!= 0))||(year%400 == 0)){day = 29; } else {day = 28; }} for(int i = week、n = 1; i <week+day; i ++){a [i] = string.valueof(n); n ++; } return a; }}次に、インターフェイス実装のメインインターフェイスクラスを作成します。
パッケージother1; java.awt。*; java.awt.event。*; javax.swing。*;パブリッククラスのカレンダーフレームは、JFRAMEを拡張します。ActionListener{jlabel labelday [] = new Jlabel [42]; jtextfield text = new jtextfield(10); jbutton titlename [] = new jbutton [7]; jbutton button = new jbutton(); string name [] = {"day"、 "one"、 "2"、 "3"、 "4"、 "5"、 "6"}; jbutton nextmonth、前月; int year = 1996、月= 1; //スタートアッププログラムCalendarBeanカレンダーによって表示される日付情報。 jlabel showmessage = new Jlabel( ""、jlabel.center); jlabel lbl1 = new Jlabel( "年を入力してください:"); jlabel lbl2 = new Jlabel( ""); public CalendarFrame(){setbackground(new Color(0、128、128)); jpanel pcenter = new JPanel(); pcenter.setbackground(新しい色(0、139、139)); // PCENTERのレイアウトを、7行と7列を持つグリッドレイアウトレイアウトに設定します。 pcenter.setlayout(new gridlayout(7,7)); // pcenterは、(int i = 0; i <7; i ++){titlename [i] = new jbutton(name [i]); pcenter.add(titlename [i]); } // pCenterはコンポーネントラベルデイ[i] for(int i = 0; i <42; i ++){labelday [i] = new jlabel( ""、jlabel.center); pcenter.add(labelday [i]); } text.addActionListener(this); Calendar = new CalendarBean(); calendar.setyear(year); calendar.setmonth(月); string day [] = calendar.getCalendar(); for(int i = 0; i <42; i ++){labelday [i] .settext(day [i]); } nextmonth = new jbutton( "来月");前方= new jbutton( "先月");ボタン= new JButton( "もちろん"); //リスナーNextMonth.AddActionListener(this)を登録します。前month.addactionlistener(this); button.addActionListener(this); jpanel pnorth = new jpanel()、psouth = new jpanel(); pnorth.add(showmessage); pnorth.add(lbl2); pnorth.add(前月); pnorth.add(nextmonth); psouth.add(lbl1); psouth.add(テキスト); psouth.add(ボタン); showmessage.settext( "calendar:"+calendar.getyear()+"year"+calendar.getmonth()+"month"); scrollpane scrollpane = new Scrollpane(); scrollpane.add(pcenter); getContentPane()。add(Scrollpane、Borderlayout.center); //中央エリアにScrollpaneを追加しますgetContentPane()。追加(pnorth、borderlayout.north); //北部のpnorth getcontentpane()。 } public void actionperformed(actionevent e){if(e.getSource()== nextmonth){月=月+1; if(月> 12)月= 1; calendar.setmonth(月); string day [] = calendar.getCalendar(); for(int i = 0; i <42; i ++){labelday [i] .settext(day [i]); }} else if(e.getSource()== previousmonth){月=月1; if(月<1)月= 12; calendar.setmonth(月); string day [] = calendar.getCalendar(); for(int i = 0; i <42; i ++){labelday [i] .settext(day [i]); }} else if(e.getSource()== button){月=月+1; if(月> 12)月= 1; calendar.setyear(integer.parseint(text.getText())); string day [] = calendar.getCalendar(); for(int i = 0; i <42; i ++){labelday [i] .settext(day [i]); }} showmessage.settext( "calendary:"+calendar.getyear()+"year"+calendar.getmonth()+"month"); }}最後に、クラスを使用してそれを呼び出します。
パッケージother1; javax.swing.jframeをインポートします。 javax.swing.uimanagerをインポートします。 public class calendarmainclass {public static void main(string args []){try {uimanager.setlookandfeel( "com.sun.java.swing.plaf.windows.windowslookandfeel"); // Windows Interface Style} catch(Exception e){e.printstacktrace(); } calendaryframe frame = new CalendarFrame(); frame.setbounds(100,100,360,300); frame.settitle( "カレンダーアプレット"); frame.setLocationRelativeto(null); //フォームは、frame.setVisible(true)を表示するように中心にあります。 frame.setDefaultCloseoperation(jframe.exit_on_close); }}操作結果は次のとおりです。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。