重要提示: 请勿将账号共享给其他人使用,违者账号将被封禁!
查看《购买须知》>>>
找答案首页 > 全部分类 > 计算机类考试
搜题
网友您好, 请在下方输入框内输入要搜索的题目:
搜题
题目内容 (请给出正确答案)
[主观题]

本题的功能是获取鼠标在窗口中的位置。当鼠标移进窗口中,就会实时显示鼠标在窗口中的相对位置,比

如显示为"鼠标的当前位置:X:Y"(其中,X为横坐标,Y为纵坐标)。

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

public class java2

{

public static void main(String[]args)

{

MouseFrame. frame=new MouseFrame();

frame.setDefaultCloseoperation(JFrame.EXIT_

0N CLOSE);

frame.show();

}

}

class MouseFrame. extends JFrame

{

public MouseFrame()

{

setTitle("java2");

setSize(WIDTH,HEIGHT);

MousePanel panel=new MousePanel();

Container contentPane=getContentPane();

contentPane.add(panel);

}

public static final int WIDTH = 300;

public static final int HEIGHT=200;

}

class MousePanel extends JPanel

{

public MousePanel()

{

addMouseListener(new MouseHandler());

addMouseMotionListener(new MouseMotionHan-

dler());

}

public void paintComponent(Graphics g)

super.paintComponent(g);

String text="鼠标指针位置:"+mousex+":"

+mousey;

g.drawString(text,10,10);

}

private int mousex,mousey;

private class MouseMotionHandler {

public void mouseMoved(MouseEvent event)

{

mousex=event.getX();

mousey=event.getY();

repaint();

}

public void mouseDragged(MouseEvent event)

{

mousex=event.getX();

mousey=event.getY();

repaint();

}

}

private class MouseHandler

{

public void mousePressed(MouseEvent eveat)

{mousex=event.getX();

mousey=event.getY();

}

}

}

查看答案
更多“本题的功能是获取鼠标在窗口中的位置。当鼠标移进窗口中,就会实时显示鼠标在窗口中的相对位置,比”相关的问题

第1题

本题的功能是计算l~10之间除了5以外的各个自然数的和。 public class javal{public static void m

本题的功能是计算l~10之间除了5以外的各个自然数的和。

public class javal{

public static void main(String[]args){

int i=1

int sum=0;

while(i<;=10){

if(i= =5){

}

i++;

}

System.out.println("sum="+sum);

}

}

点击查看答案

第2题

本题将一个整形变量opl的二进制的低3位全部变成1。 public class javal{public static void main(

本题将一个整形变量opl的二进制的低3位全部变成1。

public class javal{

public static void main(String[]args){

pl=21;

int op2=7:

res;

System.out.println(res);

}

}

点击查看答案

第3题

本题定义了一个方法add(),用于求两个整形数的和。方法中有两个整形参数a和b,方法体中计算a和b的和

sum,并将结果返回。程序中调用add()方法求整数24和34的和,并将结果打印输出。

public class javal{

public static void main(String[]args){

int a=24,b=34;

System.out.println(add(a,b));

}

public static int add( ){

sum—a+b;

}

}

点击查看答案

第4题

本题将数组中arr[]={5,6,3,7,9,1}的各个元素按下标的逆序输出。 public class javal{public stati

本题将数组中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]+"");

}

}

}

点击查看答案

第5题

本题的功能是计算二维数组arr[][]={{34,21,45,67,20},{23,10,3,45,76},{22,3,79,56,50}}中的最小

值,并输出。

public class javal{

public static void main(String[]args){

int arr[][]={{34,21,45,67,20),{23,10,3,45,

76},{22,3,79,56,50}};

int i=0:

intj=0;

int min=arr[0][0];

while(i<;3){

while( ){

if(arr[i][j]<;min)

min=arr[i][j];

j++;

}

}

System.out.println("The min:"+min);

}

}

点击查看答案

第6题

本题的功能是展示4种不同的对话框。窗口中有4个按钮:“消息”、“输入”、“确定”和“选择”,单击任意一个

按钮,就能弹出一个对应的对话框。其中,消息对话框只有一个提示信息和一个“确定’’按钮,输入对话框有一个供输入的文本框及“确定”和“撤销”两个按钮;确定对话框中有一个提示信息和三个按钮“是”、“否”和“撤销”;而选择对话框中有一个提示信息和两个按钮“确定,,和“取消”。

import javax.swin9.*:

import java.awt.event.*;

import java.awt.*;

Public class java3 extends JFrame. implements ButtonSelecte ActionListener

{

JButton btnMessage=new JButton("消息");

JButton btnlnput=new JButton("输入");

JButton btnConfirm=new JButton("确认");

JButton btnOption=new JButton("选择");

public java3()

{

super("java3");

btnMessage.addActionListener(this);

btnlnput.addActionListener(this);

btnConfirm.addActionListener(this);

btnOption.addActionListener(this);

getContentPane().setLayout( new FIowLavout

()):

getContentPane().add(btnMessage);

getContentPane().add(btnlnput):

getContentPane().add(btnConfirm):

getContentPane().add(btnOption);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}

public static void main(String args[])

{

java3 fr=new java3();

fr.pack();

fr.setVisible(true);

}

Public void actionperformed(ActionEvent e)

{

Object[]opt={"确认","取消");

JButton instance:(JButton)e.getObject();

if(instance= =btnMessage)

JOptionPane.showMessageDialog(this,"消息对话框");

else if(instance= =btnInput、

JOptionPane.showInputDialog(this,"输入对话框");

else if(instance= =btnConfirm、

JOptionPane.showConfirmDialog(this,"确认对话框");

else

JOptionPane.showOptionDialog(this,"选择对话框","选择",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE,null,opt,opt[1]);

}

}

点击查看答案

第7题

本题的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带

有数字的面板,该面板上的数字指示着文本域中的行数。

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

public class java3 extends JFrame

{

public static JTextPane textPane;

public static JScrollPane scrollPane;

JPanel panel;

public java3()

{

super("java3()");

panel=new JPanel();

panel.setLayout(new BorderLayout());

panel.setBorder(BorderFactory.createEmptyBor-

der(20,20,20,20));

textPane=new JTextPane();

textPane.setFont(new Font("monospaeed",

Font.PLAIN,12));

scrollPane=new JScrollPane(textPane);

panel.add(scrollPane);

scrollPane.setPreferredsize(new Dimension(300,

250));

setContentPane(panel);

setCloseOperation(JFrame.EXIT_ON_CLOSE);

LineNumber lineNumber=new LineNumber();

scrollPane.setRowHeaderView(lineNumber);

}

public static void main(String[]args)

{

java3 ttp=new java3();

ttp.pack();

ttp.setVisible(true);

}

}

class LineNumber extends JTextPane

{

private final static Color DEFAULT_BACK-

GROUND=Color.gray;

private final static Color DEFAULT_FORE-

GROUND=Color.black;

private final static Font DEFAUl。T—FONT=new

Font("monospaced",Font.PLAIN,12);

private final static int HEIGHT=Integer.MAX_

VALUE-1000000;

private final static int MARGIN=5;

private FontMetrics fontMetrics;

private int lineHeight;

private int currentRowWidth;

private JComponent component;

private int componentFontHeight;

private int componentFontAscent;

public LineNumber(JComponent component)

{

if(component= =null)

{

setBackground(DEFAULT_BACKGROUND);

setForegroun"DEFAULT_FOREGROUND);

setFont(DEFAULT FONT);

this.component=this;

}

else

{

setBaekground(DEFAULT_BACKGROUND);

setForeground(component.getForeground());

setFont(component.getFont());

this.component=component;

}

componentFontHeight=component.getFontMet-

rics(component.getFont()).getHeight();

componentFontAscent=component.getFontMet-

ries(component.getFont()).getAscent();

setPreferredWidth(9999);

}

public void setPreferredWidth(int row)

{

int width=fontMetrics.stringWidth(String.val-

ueOf(row));

if(currentRowWidth<;width)

{

currentRowWidth=width;

setPreferredSize(new Dfimension(2 * MARGIN

+width,HEIGHT));

}

}

public void setFont(Font font)

{

super.setFont(font);

fontMetrics=getFontMetrics(getFont());

}

public int getLineHeight()

{

if(hneHeight= =0)

return componentFontHeight;

else

return lineHeight;

}

public void setLineHeight(int lineHeight)

{

if(hneHeight>;0)

this.lineHeight=lineHeight;

}

public int getStartOffset()

{

return component.getlnsets().top+component-

FontAscent;

}

public void paintComponent(Graphics g)

{

int lineHeight=getLineHeight();

int startOffset=getStartOffset();

Rectangle drawHere=g.getClipBounds();

g.setColor(getBackground());

g.fillRect(drawHere.x,drawHere.Y,drawHere.

width,drawHere.height);

g.setColor(getForeground());

int startLineNumber=(drawHere.y/line-

Height)+1;

int endLineNUmber = startLineNumber+

(drawHere.height/lineHeight);

int start=(drawHere.Y/hneHeight)*line-

Height+startOffset;

for(int i=startLineNumber;i<;=endLineN-

umber;i++)

{

String lineNumber=String.valueOf(i);

int width=fontMetrics.stringWidth(lineNumber

);

g.drawstring(lineNumber,MARGIN+current-

RowWidth-width,start);

start+=lineHeight:

}

setPreferredWidth(endLineNumber);

}

}

点击查看答案

第8题

本题是一个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();

}

}

}

点击查看答案

第9题

本题中,通过菜单“Connect”显示一个对话框,单击“ok”按钮后,所填写的内容就会传回到主窗口并显示出

来。

import java.awt.*

import java.awt.event.*;

import javax.swin9.*;

public class java3 extends JFrame. implements ActionL-

istener{

public java3(){

setTitle("java3");

setSize(300,300);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

JMenuBar mbar = new JMenuBar();

setJMenuBar(bar);

JMenu fileMenu=new JMenu("File");

mbar.add(fileMenu);

connectltem=new JMenuhem("Connect");

connecthem.addActionListener(this);

fileMenu.add(connecthem);

exithem=new JMenuhem("Exit");

exithem.addActionListener(this);

fileMenu.add(exithem);

}

public void actionPerformed(ActionEvent evt){

Object source=evt.getSource();

if(source= =connecthem){

Connectlnfo transfer=new ConnectInfo ("your-

name","pw");

if(dialog= =null)

dialog=new ConnectDialog(this);

if(dialog.showDialog(transfer)){

String uname=transfer.username;

String pwd=transfer.password;

Container contentPane=getContentPane();

contentPane.add(new JLabel("username="+

uname+",password="+pwd),"South");

validate();

}

}

else if(source= =exitltem)

System.exit(0);

}

public static void main(String[]args){

JFrame. f=new java3();

f.show();

}

private ConnectDialog dialog=null;

private JMenuhem connecthem;

private JMenuhem exithem;

}

class Connectlnfo{

public String username;

public String password;

public Connectlnfo(String U,String p){

username=u;password=P;

}

}

class ConnectDialog extends JDialog implements Ac-

tionListener{

public ConnectDialog(){

super(parent,"Connect",true);

Container contentPane=getContentPane();

JPanel pl=new JPanel();

pl.setLayout(new GridLayout(2,2));

pl.add(newJLabel("User name:"));

pl.add(username=new JTextField(""));

pl.add(new JLabel("Password:"));

pl.add(password=new JPasswordField(""));

contentPane.add("Center",pl);

Panel p2=new Panel();

okButton=addButton(p2,"ok");

cancelButton=addButton(p2。"Cancel");

contentPane.add("South",p2);

setSize(240,120);

}

JButton addButton(Container C,String name){

JButton button=new JButton(name);

button.addActionListener(this);

C.add(button);

return button;

}

public void actionPerformed(ActionEvent evt){

object source=evt.getSource();

if(source= =okButton){

ok=true:

setVisible(false);

}

else if(source= =cancelButton)

setVisible(false);

}

public void showDialog(Connectlnfo transfer){

username.setText(transfer.username);

password.setText(transfer.password);

ok=false;

show();

if(ok){

transfer.username=username.getText();

transfer.password=new String(password.get-

Password());

}

return ok;

}

private JTextField username

private JPasswordField password;

private boolean ok;

private JButton okButton;

private JButton cancelButton;

}

点击查看答案

第10题

本题的功能是监听对于菜单项和工具条的操作。窗口中有一个菜单“Color”和一个工具体,菜单“Color”中

有菜单项“Yellow”、“Blue”、“Red”和“Exit”,每个菜单项都有对应的图形,单击前三个颜色菜单项,主窗口就变成对应的颜色,单击“Exit”则退出程序。工具条上有4个按钮,分别为三个颜色按钮和一个退出程序的按钮,单击任意一个颜色按钮,主窗口将变成按钮对应的颜色,单击退出程序按钮,则退出程序。

import java.awt.*;

import java.awt.event.*;

import java.beans.*;

import javax.swin9.*;

public class java3

{

public static void main(String[]args)

{

ToolBarFrame. frame=new ToolBarFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_

ON_CLOSE);

frame.show();

}

}

class ToolBarFrame. extends JFrame

{

public ToolBarFrame()

{

setTitle("java3");

setSize(DEFAULT_WIDTH,DEFAUlT_

HElGHT);

Container contentPane=getContentPane();

panel=new JPanel();

contentPane.add(panel,BorderLayout.CEN-

TER);

Action blueAction=new ColorAction("Blue".

new ImageIcon("java3-blue-ball.gif"),Color.

BLUE);

Action yellowAction=new ColorAction("

Yellow",

new Imagelcon("java3-yellow-ball.gif"),Col-

or.YELLOW);

Action redAction=new ColorAction("Red".

new Imagelcon("java3-red-ball.gif"),Color.

RED);

Action exitAction=new

AbstractAction("Exit".new Imagelcon("java3-

exit.gif"))

{

public void actionPerformed(ActionEvent event)

{

System.exit(0);

}

};

exitAction.putValue(Action.SH()RT_DESCRIP-

TIoN,"Exit");

JToolBar bar=new JToolBar();

bar.add(blueAction);

bar.add(yellowAction);

bar.add(redAction);

bar.addSeparator();

bar.add(exitAction);

contentPane.addToolBar(bar,BorderLayout.

NoRTH);

JMenu menu=new JMenu("Color"):

menu.add(yellowAction);

menu.add(blueAction);

menu.add(redAction);

menu.add(exitAction);

JMenuBar menuBar=new JMenuBar():

menuBar.add(menu);

SetJ Menu(menuBar);

}

public static final int DEFAULT_WIDTH=300;

public static final int DEFAULT_HEIGHT

=200;

private JPanel panel;

class ColorAction extends AbstractAction

{

public ColorAction(String name,Icon icon,Color

c)

{

putValue(Action.NAME,name);

putValue(Action.SMALL_ICON,icon);

putValue(Action.SHORT_DESCRIPTION,

name+"background");

putValue("Color",c);

}

public void actionPerformed(ActionEvent evt)

{

Color C=(Color)getValue("Color");

panel.setBackcolor(c);

}

}

}

点击查看答案
下载上学吧APP
客服
TOP
重置密码
账号:
旧密码:
新密码:
确认密码:
确认修改
购买搜题卡查看答案
购买前请仔细阅读《购买须知》
请选择支付方式
微信支付
支付宝支付
选择优惠券
优惠券
请选择
点击支付即表示你同意并接受《服务协议》《购买须知》
立即支付
搜题卡使用说明

1. 搜题次数扣减规则:

功能 扣减规则
基础费
(查看答案)
加收费
(AI功能)
文字搜题、查看答案 1/每题 0/每次
语音搜题、查看答案 1/每题 2/每次
单题拍照识别、查看答案 1/每题 2/每次
整页拍照识别、查看答案 1/每题 5/每次

备注:网站、APP、小程序均支持文字搜题、查看答案;语音搜题、单题拍照识别、整页拍照识别仅APP、小程序支持。

2. 使用语音搜索、拍照搜索等AI功能需安装APP(或打开微信小程序)。

3. 搜题卡过期将作废,不支持退款,请在有效期内使用完毕。

请使用微信扫码支付(元)
订单号:
遇到问题请联系在线客服
请不要关闭本页面,支付完成后请点击【支付完成】按钮
遇到问题请联系在线客服
恭喜您,购买搜题卡成功 系统为您生成的账号密码如下:
重要提示: 请勿将账号共享给其他人使用,违者账号将被封禁。
发送账号到微信 保存账号查看答案
怕账号密码记不住?建议关注微信公众号绑定微信,开通微信扫码登录功能
警告:系统检测到您的账号存在安全风险

为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!

- 微信扫码关注上学吧 -
警告:系统检测到您的账号存在安全风险
抱歉,您的账号因涉嫌违反上学吧购买须知被冻结。您可在“上学吧”微信公众号中的“官网服务”-“账号解封申请”申请解封,或联系客服
- 微信扫码关注上学吧 -
请用微信扫码测试
选择优惠券
确认选择
谢谢您的反馈

您认为本题答案有误,我们将认真、仔细核查,如果您知道正确答案,欢迎您来纠错

上学吧找答案