2016年全国计算机等级考试成绩查询-2016年全国计算机等级考试《二级JAVA》综合应用试题

副标题:2016年全国计算机等级考试《二级JAVA》综合应用试题

时间:2023-05-06 21:24:24 阅读: 最新文章 文档下载
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

11[简答题]
本题的功能是监听对于菜单项和工具条的操作。窗口中有一个菜单“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);
}
}
}


12[简答题]
本题中,通过菜单“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;
}


13[简答题]
本题是一个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();
}
}
}


14[简答题]
本题的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带有数字的面板,该面板上的数字指示着文本域中的行数。
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{
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);
}
}


15[简答题]
本题的功能是展示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]);
}
}

2016年全国计算机等级考试《二级JAVA》综合应用试题.doc

本文来源:https://www.wddqw.com/grhx.html