习题答案.docx
- 文档编号:16859621
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:31
- 大小:22.66KB
习题答案.docx
《习题答案.docx》由会员分享,可在线阅读,更多相关《习题答案.docx(31页珍藏版)》请在冰点文库上搜索。
习题答案
一、填空题
1.定义Bean的类称为JavaBean组件或Bean组件,简称为组件。
2.JavaBean必须实现接口
3.类Component是所有UI组件和容器的根类。
4.方法repaint定义在类Component中,调用repaint方法会引起paintComponent方法的调用。
5.对字符串进行操作时经常使用trim方法,该方法的作用是删除字符串两端的空格。
6.Java提供了五个实现菜单的类:
JMenuBar、JMenu、JMenuItem、JCheckBoxMenuItem和JRadioButtonMenuItem。
7.使用方法addSeparator()可以在菜单中添加一条分割线。
8.JCheckBoxMenuItm是JMenuItem的子类,它在JMenuItem上添加一个布尔状态,该状态为真时,该项前显示对号。
9.设置按钮上文本的方法名是____setText_________,获取按钮上文本的方法名是_getText_。
9.设置文本域上文本的方法名是setText__,获取文本域上文本的方法名是_getText___,设置文本域可编辑属性的方法名是__setEditable____。
二、单项选择题
1.JLabel继承了Jcomponent的所有属性,并具有Jbutton类的许多属性,下列不属于JLabel的属性的是(A)。
ArowsBtextCiconDhorizontalAlign
A抽象类B子类C父类D基类
三、判断题
1.TextField和TextArea是用来接受用户输入的组件,但是也可以由程序控制使用户不能在其中输入信息。
2.用hide()或setVisible(false)方法可以使组件隐藏不可见,但是一旦隐藏便不能恢复显示。
3.一个Button对象,可以调用方法getLabel()获取其上的标签,从而判断是哪个按钮;Label也使用相同的方法。
4.使用BorderLayout的容器最多只能放置5个组件,如果要放置更多的组件,则需要使用多层容器。
5.使用GridLayout布局策略的容器中,所有的组件都有相同大小。
答案:
1.对
2.错,可以恢复
3.后半句错
4.对
5..对
四、编程题
1.请编写一个Application,其功能为:
在窗口上摆放两个标签。
构造第一个标签时,令其上面的文本信息为“我将参加Java程序设计考试。
”,将第二个标签构造为空标签。
程序将第一个标签的信息复制到第二个标签上,并增加信息“希望自己考取好成绩。
”。
要求第一个标签以红色为背景,绿色为前景;第二个标签以绿色为背景,蓝色为前景。
(知识点考察:
定义标签,设置标签文本值和背景颜色)
@程序
importjava.awt.*;
importjavax.swing.*;
classMyFrameextendsJFrame
{
JLabelp1=newJLabel("我将参加Java程序设计考试。
");
JLabelp2=newJLabel("");
publicMyFrame()
{
this.getContentPane().setLayout(newFlowLayout());
this.getContentPane().add(p1);
this.getContentPane().add(p2);
p2.setText(p1.getText()+"希望自己考取好成绩。
");
p1.setBackground(Color.red);
p1.setForeground(Color.green);
p2.setBackground(Color.green);
p2.setForeground(Color.blue);
}
publicstaticvoidmain(String[]args)
{
MyFramemyFrame=newMyFrame();
myFrame.setTitle("Show");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(250,250);
myFrame.setVisible(true);
}
}
2.请编写一个Application实现如下功能:
定义一个用于给出提示信息的标签和两个文本框,其中,一个文本框用于获取用户给出的一个整数,求该数的平方后将计算结果置在另一个文本框中输出。
(知识点考察:
定义标签和文本框,数值型数据与字符串西相互转换)
@程序
importjava.awt.*;
importjavax.swing.*;
import;
classMyFrameextendsJFrameimplementsActionListener
{
JLabelp;
JTextFieldin,out;
intx;
Stringstr="";
publicMyFrame()
{
p=newJLabel("请输入一个整数:
");
in=newJTextField(18);
out=newJTextField(18);
this.getContentPane().setLayout(newFlowLayout());
this.getContentPane().add(p);
this.getContentPane().add(in);
this.getContentPane().add(out);
in.addActionListener(this);
}
publicvoidactionPerformed(ActionEventevt)
{
x=Integer.parseInt(in.getText());
str=x+"的平方为:
"+(long)(x*x);
out.setText(str);
}
publicstaticvoidmain(String[]args)
{
MyFramemyFrame=newMyFrame();
myFrame.setTitle("Show");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(250,250);
myFrame.setVisible(true);
}
}
3.请编写一个Application实现如下功能:
定义三个文本框。
其中,第一个文本框上面的文本信息为“请输入口令:
”;第二个文本框为口令输入域;第三个文本框上的信息由程序设置:
若口令(假设口令为字符串”MyKey”)正确,则设置为“通过!
”,否则设置为“口令错!
”;。
(知识点考察:
定义文本框,设置和获取文本框的文本值)
@程序
importjava.awt.*;
importjavax.swing.*;
import;
classMyFrameextendsJFrameimplementsActionListener
{
JTextFieldp;
JTextFieldin;
JTextFieldout;
Strings="";
publicMyFrame()
{
p=newJTextField("请输入口令:
");
in=newJTextField(18);
out=newJTextField(18);
this.getContentPane().setLayout(newFlowLayout());
this.getContentPane().add(p);
this.getContentPane().add(in);
this.getContentPane().add(out);
in.addActionListener(this);
}
publicvoidactionPerformed(ActionEventevt)
{
s=in.getText();
if(s.equals("MyKey"))
out.setText("通过!
");
else
out.setText("口令错!
");
}
publicstaticvoidmain(String[]args)
{
MyFramemyFrame=newMyFrame();
myFrame.setTitle("Show");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(250,250);
myFrame.setVisible(true);
}
}
4.编写Application,其中包含两个按钮b1、b2,初始时b1的前景为兰色,b2的前景为红色,它们的标签分别为”兰按钮”、”红按钮”。
无论哪个按钮被点击,都将该按钮上的标记改为“已按过”,并使该按钮变灰。
(知识点考察:
定义并设置按钮的前景色和背景色,点击按钮触发事件处理过程)
@程序
importjava.awt.*;
importjavax.swing.*;
import;
classMyFrameextendsJFrameimplementsActionListener
{
inti;
JButtonb1,b2;
publicMyFrame()
{
b1=newJButton("兰按钮");
b2=newJButton("红按钮");
this.getContentPane().setLayout(newFlowLayout());
this.getContentPane().add(b1);
b1.setForeground(Color.blue);
this.getContentPane().add(b2);
b2.setForeground(Color.red);
b1.addActionListener(this);
b2.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==b1)
{
b1.setText("已按过");
b1.setForeground(Color.gray);
}
if(e.getSource()==b2)
{
b2.setText("已按过");
b2.setForeground(Color.gray);
}
}
publicstaticvoidmain(String[]args)
{
MyFramemyFrame=newMyFrame();
myFrame.setTitle("Show");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(250,250);
myFrame.setVisible(true);
}
}
5.请编写一个Applicaion,其功能为:
在其窗口中摆放三个单选按钮,令它们的标签分别为“选项1”、“选项2”、“选项3”,初始时,所有按钮均可见;以后,如果某个单选按钮被选中了,就通过消息对话框显示它被选中的信息(如,若点击了第二个单选按钮,则显示“你选择了”选项2””),并使该单选按钮自身不可见,而使其它单选按钮变为可见的。
(知识点考察:
定义单选按钮和消息提示框,点击按钮触发事件处理过程,修改提示框的visible属性)
@程序
importjava.awt.*;
importjavax.swing.*;
import;
classMyFrameextendsJFrameimplementsActionListener
{
ButtonGroupoptGroup;
JRadioButtonopt1,opt2,opt3;
Strings="";
booleanb=false;
publicMyFrame()
{
optGroup=newButtonGroup();
opt1=newJRadioButton("选项1");
opt2=newJRadioButton("选项2");
opt3=newJRadioButton("选项3");
optGroup.add(opt1);
optGroup.add(opt2);
optGroup.add(opt3);
this.getContentPane().setLayout(newFlowLayout());
this.getContentPane().add(opt1);
this.getContentPane().add(opt2);
this.getContentPane().add(opt3);
opt1.addActionListener(this);
opt2.addActionListener(this);
opt3.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==opt1)
{
JOptionPane.showMessageDialog(this,"你选择了选项1");
opt1.setVisible(false);
opt2.setVisible(true);
opt3.setVisible(true);
}
if(e.getSource()==opt2)
{
JOptionPane.showMessageDialog(this,"你选择了选项2");
opt1.setVisible(true);
opt2.setVisible(false);
opt3.setVisible(true);
}
if(e.getSource()==opt3)
{
JOptionPane.showMessageDialog(this,"你选择了选项3");
opt1.setVisible(true);
opt2.setVisible(true);
opt3.setVisible(false);
}
}
publicstaticvoidmain(String[]args)
{
MyFramemyFrame=newMyFrame();
myFrame.setTitle("Show");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(250,250);
myFrame.setVisible(true);
}
}
7.请编写一个Applet,在其窗口中摆放两复选按钮框,通过一个文本域显示它们被选中(那个被选中、或两个均被选中、或两个均未选中)的信息。
(知识点考察:
定义复选按钮,点击按钮触发事件处理过程)
@程序
importjava.awt.*;
importjavax.swing.*;
import;
classMyFrameextendsJFrameimplementsItemListener
{
privateJTextFieldt;
privateJCheckBoxopt1,opt2;
publicMyFrame()
{
t=newJTextField(20);
this.getContentPane().setLayout(newFlowLayout());
this.getContentPane().add(t);
opt1=newJCheckBox("选项1");
this.getContentPane().add(opt1);
opt1.addItemListener(this);
opt2=newJCheckBox("选项2");
this.getContentPane().add(opt2);
opt2.addItemListener(this);
}
publicvoiditemStateChanged(ItemEvente)
{
Strings="";
if(opt1.isSelected())
s="选择了选项1";
if(opt2.isSelected())
s=s+"选择了选项2";
t.setText(s);
}
publicstaticvoidmain(String[]args)
{
MyFramemyFrame=newMyFrame();
myFrame.setTitle("Show");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(250,250);
myFrame.setVisible(true);
}
}
8.程序在画板中显示一条信息,并利用两个按钮up和down上下移动该信息。
程序输出结果如下图所示。
(知识点考察:
点击按钮触发事件处理过程,注册监听器)
答案:
importjava.awt.*;
import;
import;
importjavax.swing.*;
publicclassButtonDemoextendsJFrame
implementsActionListener
{
//Declareapanelfordisplayingmessage
privateMessagePanelmessagePanel;
//Declaretwobuttonstomovethemessageleftandright
privateJButtonjbtUp,jbtDown;
//Mainmethod
publicstaticvoidmain(String[]args)
{
ButtonDemoframe=newButtonDemo();
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
publicButtonDemo()
{
setTitle("ButtonDemo");
//CreateaMessagePanelinstanceandsetcolors
messagePanel=newMessagePanel("WelcometoJava");
messagePanel.setBackground(Color.yellow);
//CreatePaneljpButtonstoholdtwoButtons"<="and"right=>"
JPaneljpButtons=newJPanel();
jpButtons.setLayout(newFlowLayout());
jpButtons.add(jbtUp=newJButton());
jpButtons.add(jbtDown=newJButton());
//Setbuttontext
jbtUp.setText("Up");
jbtDown.setText("Down");
//Setkeyboardmnemonics
jbtUp.setMnemonic('U');
jbtDown.setMnemonic('D');
//Seticons
//jbtUp.setIcon(newImageIcon("images/left.gif"));
//jbtDown.setIcon(newImageIcon("images/right.gif"));
//SettoolTipTextonthe"Up"and"Down"buttons
jbtUp.setToolTipText("MovemessagetoUp");
jbtDown.setToolTipText("MovemessagetoDown");
//Placepanelsintheframe
getContentPane().setLayout(newBorderLayout());
getContentPane().add(messagePanel,BorderLayout.CENTER);
getContentPane().add(jpButtons,BorderLayout.SOUTH);
//Registerlistenerswiththebuttons
jbtUp.addActionListener(this);
jbtDown.addActionListener(this);
}
//Handlebuttonevents
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==jbtUp)
{
up();
}
elseif(e.getSource()==jbtDown)
{
down();
}
}
//Movethemessageinthepanelleft
privatevoidup()
{
inty=messagePanel.getYCoordinate();
if(y>10)
{
//Shiftthemessagetotheleft
messagePanel.setYCoordinate(y-10);
messagePanel.repaint();
}
}
//Movethemessageinthepanelright
pr
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 习题 答案