问答题

本题的功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。
import java.awt.*;
import java.awt.event*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
public class java3
{
public static void main(String[]args)
{
TextTestFrame frame=new TextTestFrame():
frame.setDefauhCloseOperation(JFrame.EXIT_
0N_CLOSE);
frame.show();
}
}
class TextTestFrame extends JFrame
{
public TextTestFrame()
{ setTitle("java3"):
setSize(DEFAULT_WIDTH,DEFAULT_
HElGHT);
Container contentPane=getContentPane();
DocumentListener listener=new DoeumentListen-
er();
JPanel panel=new JPanel();
hourField=new JTextField("12",3);
panel.add(hourField);
hourField.getDocument().addDocumentListener
(this);
minuteField=new JTextField("00",3):
panel.add(minuteField);
minuteField.getDocument().addDocumentListener
(listener);
contentPane.add(panel,BorderLayout.S()UTH);
clock=new ClockPanel();
contentPane.add(clock,BorderLayout.CEN-
TER);
}
public void setClock()
{
try
{
int hours
=Integer.parseInt(hourField.getText().trim
()):
int minutes
=Integer.parseInt(minuteField.getText().trim
());
clock.setTime(hours,minutes);
}
catch(NumberFormatExcepfion e){}
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT
=300;
private J TextField hourField;
private JTextField minuteField;
private ClockPanel clock;
private class clockFieldListener extends Docu-
mentListener
{
public void insertUpdate(DocumentEvent e){ set-
Clock();}
public void removeUpdate(DocumentEvent e){
setClock();}
public void changedUpdate(DocumentEvent e){}
}
}
class ClockPanel extends JPanel
{
pubhc void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphies2D g2=(Graphics2D)g;
Ellipse2D circle
=new Ellipse2D.Double(0,0,2* RADIUS,2
*RADIUS);
g2.draw(circle);
double hourAngle
=Math.toRadians(90-360*minutes/(12
*60));
drawHand(92,hourAngle,HOUR_HAND_
LENGTH);
double minuteAngle
=Math.toRadians(90-360*minutes/60);
drawHand(g2,minuteAngle,MINUTE_HAND_
LENGTH):
}
punic void drawHand(Graphics2D g2,
double angle,double handLength)
{
Point2D end=new Point2D.Double(
RADIUS+handLength*Math.cos(angle),
RADIUS-handLength*Math.sin(angle));
Point2D center=new Point2D.Double(RADIUS,
RADIUS):
g2.draw(new Line2D.Double(center,end));
}
public void setTime(int h,int m)
{
minutes=h*60+m;
repaint();
}
private double minutes=0;
private double RADIUS=100;
private double MINUTE_HAND_LENGTH=
0.8*RADIUS;
private double HOUR_HAND_LENGTH=0.6
*RADIUS:
}

【参考答案】

第1处:DocumentListener listener=new ClockField-Listener()
......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)

相关考题

问答题 本题利用递归方法求前n个自然数的和(n=lO)。 public class javal{ public static void main(String[]args){ int sum=add(10): System.out.println("1+2+…+9+10="+ sum); } public static int add( ){ if(n= =l){ ; } else ; } }

判断题 商业银行财务管理主要是指商业银行对各职能部门、分支机构的成本费用和利润进行控制考核,其目的降低整体经营成本、提高经济效益。

问答题 本题将数组中arr[]={5,6,3,7,9,1}的各个元素按下标的逆序输出。 public class javal{ public static void main(String[]args){ int arr[]={5,6,3,7,9,1}; ; n= ; while(n>=O){ System.OUt.print(arr[n]+""); ; } } }

问答题 本题的功能是用复选按钮来控制鼠标右键的弹出菜单是否弹出。窗口中有一个复选按钮“弹出菜单”,如果选中该复选按钮后,鼠标置于窗口上,单击鼠标右键会弹出一个菜单,单击菜单项中的选项后,后台会输出单击的菜单项,如果取消该复选按钮的选择,单击鼠标右键则不能弹出菜单。 import java.awt.*; import java.awt.event.*; class CanvasWithPopup extends Canvas{ Popupmenu popup; CanvasWithPopup(PopupMenu popup){ enableEvents(AWTEvent.MOUSE EVENT MASK): this.popup=popup; } void addPopup(){ add(popup); } void removePopup(){ remove(popup); } protected void processMouseEvent(MouseEvent evt){ if(popup.getParent()!=null&&evt.isPop- upTrigger()){ popup.show(evt.getComponent(),evt.getX(), evt.getY()); } super.processMouseEvent(evt); } } public class java3 extends Frame implements Item- Listener,ActionListener{ Checkbox cb=new Checkbox("弹出菜单", false); CanvasWithPopup canvas; java3(){ super("java3"); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); add(cb,BorderLayout.NORTH); cb.addhemListener(this); PopupMenu popup=new PopupMenu("Button Control"): popup.add("iteml"); popup.add("item2"); popup.addActionListener(this); canvas=new CanvasWithPopup(popup); add(canvas,BorderLayout.CENTER); setSize(100,200); show(); }

判断题 一般来说,确定“金牌”客户的基本方法是估算客户带来的利益。

问答题 本题将一个整形变量opl的二进制的低3位全部变成1。 public class javal{ public static void main(String[]args){ opl=21; int op2=7: res; ; System.out.println(res); } }

问答题 下面程序中,在主窗口单击鼠标后,就会生成一个新 窗口。 import java.awt.*; import java.awt.event.*; public class java3 extends Frame{ java3(){ super("java3"); addNotify(); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); Insets insets=getInsets(); setSize(insets.left+insets.right+150, insets.top+insets.bottom+150); this.addMouseListener(MouseEventHandler()); } class MouseEventHandler implements MouseAda- pter{ public void mousePresse(MouseEvent evt){ Rectangle bounds=getBounds(); int x=evt.getX()+bounds.x; int y=evt.getY()十bounds.y; java3 m=newjava3(); m.setLocation(x,y); m.show(); } } static public void main(String[]args){ (new java3()).show(); } }

判断题 银行可以通过建立客户档案的形式来实现对客户信息的收集与整理。

判断题 银行产品设计的技术主要是指银行产品的定价、利费率的设定。

问答题 本题是判断一个字符串“Tom”是否在另外一个字符串 “I am Tom,I am from China”中出现。 public class javal{ public static void main(String[]args){; strl="I am Tom,I am from China."; str2="Tom"; int i= ; if( ) System.out.println("\"+str2+"\"is in the string:\""+strl+"\""); else System.out.println("\""+str2+"\"is not in the string:\""+strl+"\""); } }