简答题 Java
题号 #2245914

本题的功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。
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()
第2处:hourField.getDocument().addDocumentLis-tener(listener)
第3处:private class ClockFieldListener implements Docu-mentListener
【解析】第1处从后面程序可以看出ClockFieldListener类扩展了DocumentListener,此处应使用继承后的子类;第2处注册窗体的监听器,参数应为事件源。第3处实现的是接口,应使用implements。

你可能感兴趣的试题

3 本题的功能是获得系统剪贴板中的内容。窗口中有一个菜单“Edit”和一个文本域,“Edit”中有菜单项“Cut”、“Copy”和“Paste”,在文本域中输入内容,可以通过菜单进行剪切、复制和粘贴操作,如果系统剪贴板为空,又做粘贴操作的话,则设置文本域中背景颜色为红色,并显示错误信息。
import Java.awt.*;
importjava.io.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
class java3 extends Frame implements ActionListener,
ClipboardOwner{
TextArea textArea=new TextArea();
java3(){
super("java3");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}); 
MenuBar mb=new MenuBar();
Menu m=new Menu("Edit");
setLayout(new BorderLayout());
add("Center",textArea);
m.add("Cut");
m.add("Copy");
m.add("Paste");
mb.add(m);
setMenuBar(this);
for(int i=0;i<m.gethemCount();i++){
m.itern(i).addActionListener(this);
}
setSize(300,300);
show();
}
public void actionPerformed(ActionEvent evt){
if("Paste".equals(evt.getActionCommand())){
boolean error=true;
Transferable t=
getToolkit().getSystemClipboard().getContents
(this);
try{
if(t! =null&&t.isDataFlavorSupported(Dat-
aFlavor.stringFlavor)){
textArea.setBackground(Color.white);
textArea.setForeground(Color.black);
textArea.replaceRange(
(String)t.getTransferData(DataFlavor.stringFla-
vor),
textArea.getSelectionStart(),
textArea.getSelectionEnd());
error=false;
}
}catch(UnsupportedFlavorException e){
}catch(IOException e){
}
if(error){
textArea.setBackground(Color.red);
textArea.setForeground(Color.white);
textArea.repaint();
textArea.setText("ERROR:\nEither the clip-
board"+"is empty or the contents is not fl string.");
}
}else if("Copy".equals(evt.getActionCommand
())) {
setContents();
}else if("Cut".equals(evt.getActionCommand
())){
setContents();
textArea.replaceRange("",textArea.getSelec-
tionStart(),textArea.getSelectionEnd());
}
}
void setContents(){
S=textArea.getSelectedText();
St ringSelection contents = new StringSelection
(s);
getToolkit().getSystemClipboard().setContents
(contents,this);
}
public void lostOwnership(Clipboard clipboard,
Transferable contents){
System.out.println("lost ownership");
}
public static void main(String args[]){
new java3();
}
}
找试题 · 找答案 · 找资料 在线模考