for(int i=星期几,n=1;i<星期几+day;i++) {
a[i]=String.valueOf(n) ; n++; } return a; } }
//CalendarFrame.java
import java.util.*; import java.awt.*; import java.awt.event.*; import java.applet.*;
public class CalendarFrame extends Frame implements ActionListener { Label labelDay[]=new Label[42]; Button titleName[]=new Button[7];
String name[]={\日\一\二\三\四\五\六\ Button nextMonth,previousMonth; int year=2006,month=10; CalendarBean calendar;
Label showMessage=new Label(\ public CalendarFrame() { Panel pCenter=new Panel();
【代码1】 //将pCenter的布局设置为7行7列的GridLayout 布局。 for(int i=0;i<7;i++)
{ titleName[i]=new Button(name[i]);
【代码2】//pCenter添加组件titleName[i]。 }
for(int i=0;i<42;i++) {
labelDay[i]=new Label(\ 【代码3】//pCenter添加组件labelDay[i]。 }
calendar=new CalendarBean(); calendar.setYear(year); calendar.setMonth(month); String day[]=calendar.getCalendar(); for(int i=0;i<42;i++)
{ labelDay[i].setText(day[i]); }
nextMonth=new Button(\下月\ previousMonth=new Button(\上月\ nextMonth.addActionListener(this); previousMonth.addActionListener(this);
Panel pNorth=new Panel(), pSouth=new Panel(); pNorth.add(previousMonth); pNorth.add(nextMonth); pSouth.add(showMessage);
showMessage.setText(\日历:\年\月\ ScrollPane scrollPane=new ScrollPane(); scrollPane.add(pCenter);
【代码4】// 窗口添加scrollPane在中心区域 【代码5】// 窗口添加pNorth 在北面区域 【代码6】// 窗口添加pSouth 在南区域。 }
public void actionPerformed(ActionEvent e) { if(e.getSource()==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]); } }
showMessage.setText(\日历:\年\月\ } }
//CalendarMainClass.java
public class CalendarMainClass { public static void main(String args[])
{ CalendarFrame frame=new CalendarFrame(); frame.setBounds(100,100,360,300); frame.setVisible(true);
frame.validate();
frame.addWindowListener(new java.awt.event.WindowAdapter()
{ public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } } ); } }
4、方程求根:请读懂下面模板程序,将其补充完整并调试运行。 //SquareEquation.java
public class SquareEquation { double a,b,c; double root1,root2; public void setA(double a) { this.a=a; }
public void setB(double b) { this.b=b; }
public void setC(double c) { this.c=c; }
public double getRootOne() throws NoRealRootException,NoSquareEquationException { if(a!=0)
{ double disk=b*b-4*a*c; if(disk>=0)
{ root1=(-b+Math.sqrt(disk))/(2*a); } else
{ throw new NoRealRootException(\没有实根\ } } else {
throw new NoRealRootException(\不是二次方程\ }
return root1; }
public double getRootTwo() throws NoRealRootException,NoSquareEquationException { if(a!=0)
{ double disk=b*b-4*a*c; if(disk>=0)
{ root2=(-b-Math.sqrt(disk))/(2*a);
} else
{ throw new NoRealRootException(\没有实根\ } } else {
throw new NoRealRootException(\不是二次方程\ }
return root2; } }
class NoRealRootException extends Exception { String message;
NoRealRootException(String s) { message=s; }
public String getMessage() { return message; } }
class NoSquareEquationException extends Exception { String message;
NoSquareEquationException(String s) { message=s; }
public String getMessage() { return message; } }
//EquationFrame.java
import java.awt.*; import java.awt.event.*;
public class EquationFrame extends Frame implements ActionListener { SquareEquation equation; TextField textA,textB,textC; TextArea showRoots; Button controlButton; public EquationFrame()
{ equation=new SquareEquation(); textA=new TextField(8); textB=new TextField(8); textC=new TextField(8); showRoots=new TextArea();
controlButton=new Button(\确定\ Panel pNorth=new Panel();
pNorth.add(new Label(\二次项系数:\ pNorth.add(textA);
pNorth.add(new Label(\一次项系数:\ pNorth.add(textB);
pNorth.add(new Label(\常数项系数:\ pNorth.add(textC);
pNorth.add(controlButton);
【代码1】 //当前窗口作为controlButton的ActionEvent事件的监视器 add(pNorth,BorderLayout.NORTH); add(showRoots,BorderLayout.CENTER); setBounds(100,100,630,160); setVisible(true); validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e) { System.exit(0); } } ); }
public void actionPerformed(ActionEvent e) { try{
double a=Double.parseDouble(【代码2】); //textA调用方法获取其中的文本
double b=Double.parseDouble(【代码3】); //textB调用方法获取其中的文本
double c=Double.parseDouble(【代码4】); // textC调用方法获取其中的文本
equation.setA(a); equation.setB(b); equation.setC(c); textA.setText(\ textB.setText(\ textC.setText(\
showRoots.append(\根:\ showRoots.append(\根:\ }
catch(Exception ex)
{ showRoots.append(\ } } }
//EquationMainClass.java
public class EquationMainClass
{ public static void main(String args[])
{ EquationFrame frame=new EquationFrame(); } }
思考:将下面题目改成运用Swing组件实现,观察有什么变化。