问答题

本题的功能是监听对于颜色的复制和粘贴。程序窗l:1中,有一个颜色设置框和两个按钮,名为“复制”和“粘贴”,在颜色设置框中设置颜色后,下面的预览面板将显示选中的颜色,单击“复制”按钮后,将设置的颜色复制到系统的剪贴板上,然后继续选择其他颜色,当单击按钮“粘贴”后预览面板的颜色将设置为刚才复制的颜色。
importjava.io.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class java3
{
public static void main(String[]args)
{
JFrame frame=new SerialTransferFrame():
frame.setDefaultCloseoperation(JFrame.EXIT
ON_CLOSE);
frame.show();
}
}
class SerialTransferFrame extends JFrame
{
public SerialTransferFrame()
{
setTitle("java3"):
Container contentPane = getContentPane();
chooser=new JColorChooser();
contentPane.add(chooser,BorderLayout.CEN-
TER);
JPanel panel=new JPanel();
JButton copyButton=new JButton("复制");
panel.add(copyButton);
copyButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
copy();
}
});
JButton pasteButton=new JButton("粘贴");
panel.add(pasteButton);
pasteButton.addAetionListener(new
ActionListener()
{
public void aetionPerformed(AetionEvent event)
{
paste();
}
}); contentPane.add(panel,BorderLayout.SOUTH);
pack();
}
private void copy()
{
Clipboard clipboard
=Toolkit.getDefaultToolkit().getSystemClip-
board();
int color=chooser.getColor();
SerialSeleetion selection=new SerialSelection
(color);
clipboard.setContents(selection,null);
}
private void paste()
{
Clipboard clipboard
=Toolkit.getDefaultToolkit().getSystemClip-
board();
Transferable contents=clipboard.getContents
(null);
if(contents= =null)return;
try
{
DataFlavor flavor=new DataFlavor(
"application/x-java-serialized-object;class=
java.awt.Color");
if(contents.isDataFlavorSupported(flavor))
{
Color color
=(Color)contents.getTransferData(flavor);
  ;
}
} catch(ClassNotFoundException exception)
{
JOptionPane.showMessageDialog(this,excep-
tion);
}
catch(UnsupportedFlavorException exception)
{
JOptionPane.showMessageDialog(this,excep-
tion);
}
catch(IOException exception)
{
JOptionPane.showMessageDialog(this,excep-
tion);
}
}
private JColorChooser chooser;
}
class SerialSelection implements Transferable
{
SerialSelection(Serializable o)
{
obj=o:
}
public DataFlavor[]getTransferDataFlavors()
{
DataFlavor[]flavors=new DataFlavor[2];
Class type=obj.getClass();
String mimeType
="application/x-java-serialized-object;class
="
+type.getName();
try
{
flavors[0]=new DataFlavor(mimeType);
flavors[1]=DataFlavor.stringFlavor;
return flavors;
}
catch(ClassNotFoundExeeption exception)
{
return new DataFlavor[O];
}
}
public boolean isDataFlavorSupported(DataFlavor
flavor)
{
Return
DataFlavor.stringFlavor.equals(flavor)‖"appli-
cation".equals(flavor.getPrimaryType())
&&"x-iava-serialized-object".equals(flavor.
getSubType())
&&flavor.getRepresentationClass().isAssign-
ableFrom(obj.getClass());
}
public void getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException
{
if(! isDataFlavorSupported(flavor))
throw new UnsupportedFlavorException(flaVor);
if(DataFlavor.stringFlavor.equals(flavor))
return obj.toString(); return obj;
}
private Serializable obj;
}

【参考答案】

第1处:Color color=chooser.getColor()
第2处:chooser.setColor(......

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

相关考题

问答题 本题提取字符串“China is a great country.”中的前5个字符生成一个新的字符串,并将剩余字符组成另外一个新的字符串,最后将两个新的字符串连接输出。 public class javal{ public static void main(String[]args){ Stling str="China is a great country."; ; headstr=str.substring( ); trailstr=str.substring( ); System.out.println(headstr+trailstr); } }

判断题 房地产司法拍卖估价,评估的是市场价值,而房地产抵押估价,评估的应是谨慎价值。( )

问答题 本题是一个Applet,功能是用鼠标画不同颜色的图形。页面中有5个按钮“画红色图形”、“画绿色图形”、“画蓝色图形”、“橡皮”和“清除”,单击前三个按钮中的一个,按住鼠标左键或右键在面板中拖动,就能两出对应颜色的线条,单击“橡皮”按钮,按住鼠标左键或右键在面板中拖动就能将面板中的图形擦除掉,单击“清除”按钮,就能将面板中所有的图形清除掉。 import java.applet.*; import java.awt.*; import java.awt.event.*; public class java3 extends Applet implements ActionListener {int x=-1,y=-1,rubberNote=0,clearNote=0; Color C=new Color(255,0,O); int con=3; Button b_red,b_blue,b_green,b_clear,b_quit; public void init() { addMouseMotionListener(this); b_red=new Button("画红色图形"); b_blue=new Button("画蓝色图形"); b_green=new Button("画绿色图形"); b_quit=new Button("橡皮"); b_clear=new Button("清除"); add(b_red); add(b_green); add(b_blue); add(b_quit); add(b_clear); b_red.addActionListener(this); b_green.addActionListener(this); b_blue.addActionListener(this); b_quit.addActionListener(this); b_dear.addActionListener(this); } public void paint() (if(x!=-l&&y!=-l&rubberNote= = 0R&clearNote= =0) {g.setColor(c); g.filloval(X,Y,con,con); } else if(rubberNote= =1&&clearNote= =O) {g.clearRect(x,Y,10,10); } else if(clearNote= =1&&rubberNote= =O) {g.clearRect(0,0,getSize().width,getSize(). height); } } public void mouseDragged(MouseEvent e) {x=(int)e.getX();y=(int)e.getY();repaint(); } public void mouseMoved(MouseEvent e){) public void update(Graphics g) {paint(g); } public void actionPerformed(Event e) {if(e.getSource()= =b-red) {rubberNote=0;clearNote=0;c=new Color (255,0,0); } else if(e.getSource()= =b_green) {rubberNore=0;clearNote=0;C=new Color(0, 255,0); } else if(e.getSource()= =b_blue) {rubberNote=0;clearNote=0;C=new Color(0, 0,255); } if(e.getSource()= =b—quit) {rubberNote=1;clearNote=0; } if(e.getSource()= =b—clear) {clearNote=1;rubberNote=0;repaint(); } } }

问答题 本题的功能是通过鼠标确定两个点,然后画两点间的直线。窗口中有一个按钮“Draw line”,单击该按钮后,它就处于按下状态,然后用鼠标在窗口中单击一下,在单击的地方就会出现一个坐标圆点,用鼠标在另外一个地方单击一下又会出现另外一个圆点,并且此时在两个坐标圆点间画出一条直线,且“Draw line”处于可用状态,再单击这个按钮就可以画另外一条直线。 Import java.awt.* ; import java.awt.event.*; import javax.swing.*; class EventQueuePanel extends JPanel implements Ac- tionListener {EventQueuePanel() {JButton button=new JButton("Draw line"); add(button); button.addActionListener(this); } public void actionPerformed(ActionEvent evt) (Graphics g=getGraphics(); P=getClick(); g.drawOval(P.x-2,P.Y-2,4,4); Point q=getClick(); g.drawOval(q.x-2,q.y-2,4,4); g.drawLine(P.X,P.Y,q.x,q.y); g.dispose(); } public Point getClick() {EventQueue eq=Toolkit.getDefaultToolkit(). getSystemEventQueue(); while(true) {try {AWTEvent evt=eq.getNextEvent(); if(evt.getID()= =MouseEvent.MOUSE. PRESSED) {MouseEvent mevt=(MouseEvent)evt; Point P= (); Point top=getRootPane().getLocation(); P.X-=top.x; P.Y-=top.Y; return P; } } catch(InterruptedException e) {} } } private int Y=60; } class EventQueueFrame extends JFrame {public EventQueueFrame() {setTitle("java2"); setSize(300,200); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0); } }); Container contentPane=getContentPane(); contentPane.add(new EventQueuePanel()); } } public class java2 {public static void main(String[]args) {Frame frame=new EventQueueFrame(); frame.show(); } }

判断题 合作游戏是指由教师建议或开始一个游戏,教师事先定好某些角色,以部分控制游戏的进行。()

问答题 本题中定义了一个树型的通信录,窗El左侧是一个树, 右侧是一个文本域,单击树的结点,则在右侧文本域中显示 相关信息,如果单击的是树结点,则显示对应名字的电话 信息。 import javax.swing.*; import javax.swing.tree.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; class Mytree2 extends JFrame {JTree tree=null;JTextArea text=new JTextArea (20,20); Mytree2() {Container con=getContentPane(); DefauhMutableTreeNode root=new Default- MutableTreeNode("同学通信录"); DefaultMutableTreeNode tl=new Default- MutableTreeNode("大学同学"); DefaultMutableTreeNode t2=new Default- MutableTreeNode("研究生同学"); DefaultMutableTreeNode tl l=new Default- MutatleTreeNode("陈艳"); DefaultMutableTreeNode tl 2=new Default- MutableTreeNode("李小永"); DefaultMutableTreeNode t2 1=new Defauh- MutableTreeNode("王小小"); DefauhMutableTreeNode t2 2=new Defauh- MutableTreeNode("董小"); setTitle("java2"); root.add(t1);root.add(t2); tl.add(t1_1);tl.add(t1_2);t2.add(t2_1);t2. add(t2_2); tree=new JTree(root); JSerollPane scrollpane=new JScrollPane(text); JSplitPane splitpane=new JSplitPane(JSplitPane. HORIZONTAL SPLIT, true,tree,scrollpane); tree.addTreeSeleetionListener(this); con.add(splitpane); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);}}); setVisible(true);setBounds(70,80,200,300); } public void valueChanged(TreeSelectionEvent e) {if(e.getSouree()= =tree) {DefauhMutableTreeNode node= (DefaultMutableTreeNode)tree.getLastSelected- PathComponent(); if(node.isLeaf()) (String str= ; if(str.equals("陈艳")) (text.setText(str+":联系电话:0411- 4209876");} else if(str.equals("李小永")) {text.setText(str+":联系电话:010- 62789876");} else if(str.equals("王小小")) {text.setText(str+":联系电话:0430- 63596677");) else if(str.equals("董小")) {text.setText(str+":联系电话:020- 85192789");} } else {text.setText(node.getUserObject().toString ()); } } } } public class java2 {public static void main(String args[]) {Mytree2 win=new Mytree2();win.pack();} }

判断题 住房市场存在政府干预且受社会高度关注,除政府政策调整、社会舆论导向等的影响外,示范效应、从众心理等也会影响购买者决策,出现羊群效应等非理性购买行为。( )

问答题 本题中定义了长度为20的一维整型数组a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。 public class javal{ public static void main(String[]args){ int a[]= ; int i; for( ;i++) a[i]=i; for(i=0;i<20;i++){ if( ) System.OUt.print("a[It+i+"]="+a[i]+","); } } }

问答题 本题的功能是监听键盘键的敲击,并显示在窗口中。 import javax,.swing.*; importjava.awt.*; , import java.awt.event.*; public class java3 extends JFrame extends KeyListener { private String linel=""line2="" private String line3="" private JTextArea textArea; public java3() { super("java3"); textArea=new JTextArea(10,15); textArea.setText("Press any key on the key- board…"); textArea.setEnabled(false); addKeyListener(this); getContentPane().add(textArea); setSize(350,100); show(); } public void keyPressed(KeyEvent e) { linel="Key pressed:"+e.getKeyText(e. getKeyCode()); setLines2and3(e); } public void keyReleased(KeyEvent e) { linel="Key released:"+e.getKeyText(e. getKeyCode()); setLines2and3(e): } public void keyTyped(KeyEvent e) { Linel="Key typed:"+e.getKeychar(); setLines2and3(e); } private void setLines2and3(KeyEvent e) { line2="This key is"+(e.isActionKey()?"" :"not")+"an action key"; String temp=e.getKeyModifiersText(e.get- Modifiers()); hne3="Modifier keys pressed:"+(temp.e- quals("")?"none":temp); textArea.setText(linel+"\n"+line2+"\n" +line3+"\n"); } public static void main(String args[]) { java3 app=new java3(); addWindowListener(new Windowadapted() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } }

问答题 本题是一个Applet,页面中有10个按钮,名称从“0~ 9”,用鼠标任意单击其中一个按钮后,通过键盘上的上下左右键可以控制按钮在窗口中移动。 import java.applet.*; import java.awt.*; import java.awt.event.*; public class java2 extends Applet {Button b[]=new Button[10]; int x,Y; public void init() {for(int i=0;i<=9;i++) {b[i]=new Button(""+i); b[i].addKeyListener(this); addCb[i]); } } public void {Button button=(Button)e.getSource(); x=button.getBounds().x; y—button.getBounds().y; if(e,getKeyCode()= =KeyEvent.VK_UP) {y=y-2; if(y<=O)y=0; button.setLocation(x,y); } else if(e.getKeyCode()= =KeyEvent.VK_ DOWN) {y=y+2; if(y>=300)y=300; button,.setLocation(X,y); } else if(e.getKeyCode()= =KeyEvent.VK_ LEFT) {x=x-2; if(x<=0)x=0; button.setLocation(x,y); } else if(e.getKeyCode()= =KeyEvent.VK_ RIGHT) (x=X+2; if(x>=300)x=300; button.setLoeation(X,y); } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} }