第九章习题答案Word格式文档下载.docx
- 文档编号:20417843
- 上传时间:2023-01-22
- 格式:DOCX
- 页数:28
- 大小:63.55KB
第九章习题答案Word格式文档下载.docx
《第九章习题答案Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《第九章习题答案Word格式文档下载.docx(28页珍藏版)》请在冰豆网上搜索。
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.event.*;
classMyFrameextendsJFrameimplementsActionListener
JLabelp;
JTextFieldin,out;
intx;
Stringstr="
;
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);
}
3.请编写一个Application实现如下功能:
定义三个文本框。
其中,第一个文本框上面的文本信息为“请输入口令:
”;
第二个文本框为口令输入域;
第三个文本框上的信息由程序设置:
若口令(假设口令为字符串”MyKey”)正确,则设置为“通过!
”,否则设置为“口令错!
。
定义文本框,设置和获取文本框的文本值)
JTextFieldp;
JTextFieldin;
JTextFieldout;
Strings="
p=newJTextField("
请输入口令:
s=in.getText();
if(s.equals("
MyKey"
))
out.setText("
通过!
else
口令错!
4.编写Application,其中包含两个按钮b1、b2,初始时b1的前景为兰色,b2的前景为红色,它们的标签分别为”兰按钮”、”红按钮”。
无论哪个按钮被点击,都将该按钮上的标记改为“已按过”,并使该按钮变灰。
定义并设置按钮的前景色和背景色,点击按钮触发事件处理过程)
inti;
JButtonb1,b2;
b1=newJButton("
兰按钮"
b2=newJButton("
红按钮"
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);
5.请编写一个Applicaion,其功能为:
在其窗口中摆放三个单选按钮,令它们的标签分别为“选项1”、“选项2”、“选项3”,初始时,所有按钮均可见;
以后,如果某个单选按钮被选中了,就通过消息对话框显示它被选中的信息(如,若点击了第二个单选按钮,则显示“你选择了”选项2””),并使该单选按钮自身不可见,而使其它单选按钮变为可见的。
定义单选按钮和消息提示框,点击按钮触发事件处理过程,修改提示框的visible属性)
classMyFrameextendsJFrameimplementsActionListener
ButtonGroupoptGroup;
JRadioButtonopt1,opt2,opt3;
booleanb=false;
optGroup=newButtonGroup();
opt1=newJRadioButton("
选项1"
opt2=newJRadioButton("
选项2"
opt3=newJRadioButton("
选项3"
optGroup.add(opt1);
optGroup.add(opt2);
optGroup.add(opt3);
this.getContentPane().add(opt1);
this.getContentPane().add(opt2);
this.getContentPane().add(opt3);
opt1.addActionListener(this);
opt2.addActionListener(this);
opt3.addActionListener(this);
if(e.getSource()==opt1)
{
JOptionPane.showMessageDialog(this,"
你选择了选项1"
opt1.setVisible(false);
opt2.setVisible(true);
opt3.setVisible(true);
}
if(e.getSource()==opt2)
你选择了选项2"
opt1.setVisible(true);
opt2.setVisible(false);
if(e.getSource()==opt3)
你选择了选项3"
opt3.setVisible(false);
}
7.请编写一个Applet,在其窗口中摆放两复选按钮框,通过一个文本域显示它们被选中(那个被选中、或两个均被选中、或两个均未选中)的信息。
定义复选按钮,点击按钮触发事件处理过程)
classMyFrameextendsJFrameimplementsItemListener
privateJTextFieldt;
privateJCheckBoxopt1,opt2;
t=newJTextField(20);
this.getContentPane().add(t);
opt1=newJCheckBox("
opt1.addItemListener(this);
opt2=newJCheckBox("
this.getContentPane().add(opt2);
opt2.addItemListener(this);
publicvoiditemStateChanged(ItemEvente)
Strings="
if(opt1.isSelected())
s="
选择了选项1"
if(opt2.isSelected())
s=s+"
选择了选项2"
t.setText(s);
8.程序在画板中显示一条信息,并利用两个按钮up和down上下移动该信息。
程序输出结果如下图所示。
点击按钮触发事件处理过程,注册监听器)
importjava.awt.event.ActionListener;
importjava.awt.event.ActionEvent;
publicclassButtonDemoextendsJFrame
implementsActionListener
//Declareapanelfordisplayingmessage
privateMessagePanelmessagePanel;
//Declaretwobuttonstomovethemessageleftandright
privateJButtonjbtUp,jbtDown;
//Mainmethod
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"
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();
//Movethemessageinthepane
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 第九 习题 答案
