《第九章习题※答案.doc》由会员分享,可在线阅读,更多相关《第九章习题※答案.doc(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流一、二、三、四、五、六、七、八、九、 第九章习题答案【精品文档】第 15 页十、 填空题1. 定义Bean的类称为JavaBean组件或Bean组件,简称为组件。2. JavaBean必须实现接口java.io.Serializable或java.io.Externalizable。3. 类Component是所有UI组件和容器的根类。4. 方法repaint定义在类Component中,调用repaint方法会引起paintComponent方法的调用。5. 对字符串进行操作时经常使用trim方法,该方法的作用是删除字符串两端的空格。6. Java提供
2、了五个实现菜单的类:JMenuBar、JMenu、JMenuItem、JCheckBoxMenuItem和JRadioButtonMenuItem。7. 使用方法addSeparator()可以在菜单中添加一条分割线。8. JCheckBoxMenuItm是JMenuItem的子类,它在JMenuItem上添加一个布尔状态,该状态为真时,该项前显示对号。9.设置按钮上文本的方法名是_setText_,获取按钮上文本的方法名是_getText_。9. 设置文本域上文本的方法名是setText_,获取文本域上文本的方法名是_getText_,设置文本域可编辑属性的方法名是_setEditable_
3、。十一、 单项选择题1. JLabel继承了Jcomponent的所有属性,并具有Jbutton类的许多属性,下列不属于JLabel的属性的是( A )。A rows B text C icon D horizontalAlign2. javax.swing.ImageIcon是javax.swing.Icon的(B)。A 抽象类 B 子类 C 父类 D 基类十二、 判断题1. TextField和TextArea是用来接受用户输入的组件,但是也可以由程序控制使用户不能在其中输入信息。2. 用hide()或setVisible(false)方法可以使组件隐藏不可见,但是一旦隐藏便不能恢复显示。
4、3. 一个Button对象,可以调用方法getLabel()获取其上的标签,从而判断是哪个按钮;Label也使用相同的方法。4. 使用BorderLayout的容器最多只能放置5个组件,如果要放置更多的组件,则需要使用多层容器。5.使用GridLayout布局策略的容器中,所有的组件都有相同大小。答案:1. 对2. 错,可以恢复3. 后半句错4. 对5. .对十三、 编程题1. 请编写一个Application,其功能为:在窗口上摆放两个标签。构造第一个标签时,令其上面的文本信息为“我将参加Java程序设计考试。”,将第二个标签构造为空标签。程序将第一个标签的信息复制到第二个标签上,并增加信息
5、“希望自己考取好成绩。”。要求第一个标签以红色为背景,绿色为前景;第二个标签以绿色为背景,蓝色为前景。(知识点考察:定义标签,设置标签文本值和背景颜色)程序import java.awt.*;import javax.swing.*;class MyFrame extends JFrameJLabel p1=new JLabel(我将参加Java程序设计考试。);JLabel p2=new JLabel( );public MyFrame()this.getContentPane().setLayout(new FlowLayout();this.getContentPane().add(p1
6、); this.getContentPane().add(p2); p2.setText(p1.getText( )+ 希望自己考取好成绩。);p1.setBackground(Color. red);p1.setForeground(Color.green);p2.setBackground(Color. green);p2.setForeground(Color.blue);public static void main(String args)MyFrame myFrame = new MyFrame();myFrame.setTitle(Show);myFrame.setDefault
7、CloseOperation(JFrame.EXIT_ON_CLOSE);myFrame.setSize(250,250);myFrame.setVisible(true);2. 请编写一个Application实现如下功能:定义一个用于给出提示信息的标签和两个文本框,其中,一个文本框用于获取用户给出的一个整数,求该数的平方后将计算结果置在另一个文本框中输出。(知识点考察:定义标签和文本框,数值型数据与字符串西相互转换)程序import java.awt.*;import javax.swing.*;import java.awt.event.*;class MyFrame extends J
8、Frame implements ActionListener JLabel p; JTextField in,out; int x; String str= ;public MyFrame() p=new JLabel(请输入一个整数: ); in=new JTextField(18); out=new JTextField(18); this.getContentPane().setLayout(new FlowLayout(); this.getContentPane().add(p); this.getContentPane().add(in); this.getContentPane
9、().add(out); in.addActionListener(this); public void actionPerformed(ActionEvent evt) x=Integer.parseInt(in.getText(); str=x+ 的平方为: +(long)(x*x); out.setText(str); public static void main(String args)MyFrame myFrame = new MyFrame();myFrame.setTitle(Show);myFrame.setDefaultCloseOperation(JFrame.EXIT_
10、ON_CLOSE);myFrame.setSize(250,250);myFrame.setVisible(true);3. 请编写一个Application实现如下功能:定义三个文本框。其中,第一个文本框上面的文本信息为“请输入口令:”;第二个文本框为口令输入域;第三个文本框上的信息由程序设置:若口令(假设口令为字符串”MyKey”)正确,则设置为“通过!”,否则设置为“口令错!”;。(知识点考察:定义文本框,设置和获取文本框的文本值)程序import java.awt.*;import javax.swing.*;import java.awt.event.*;class MyFrame
11、extends JFrame implements ActionListener JTextField p; JTextField in; JTextField out; String s=;public MyFrame() p=new JTextField (请输入口令: ); in=new JTextField(18); out=new JTextField(18); this.getContentPane().setLayout(new FlowLayout(); this.getContentPane().add(p); this.getContentPane().add(in); t
12、his.getContentPane().add(out); in.addActionListener(this); public void actionPerformed(ActionEvent evt) s=in.getText(); if(s.equals(MyKey) out.setText(通过!); else out.setText(口令错!);public static void main(String args)MyFrame myFrame = new MyFrame();myFrame.setTitle(Show);myFrame.setDefaultCloseOperat
13、ion(JFrame.EXIT_ON_CLOSE);myFrame.setSize(250,250);myFrame.setVisible(true);4. 编写Application, 其中包含两个按钮b1、b2,初始时b1的前景为兰色,b2的前景为红色,它们的标签分别为”兰按钮”、”红按钮”。无论哪个按钮被点击,都将该按钮上的标记改为“已按过”,并使该按钮变灰。(知识点考察:定义并设置按钮的前景色和背景色,点击按钮触发事件处理过程)程序import java.awt.*;import javax.swing.*;import java.awt.event.*;class MyFrame e
14、xtends JFrame implements ActionListener int i;JButton b1,b2;public MyFrame()b1=new JButton(兰按钮);b2=new JButton(红按钮);this.getContentPane().setLayout(new FlowLayout();this.getContentPane().add(b1);b1.setForeground(Color.blue);this.getContentPane().add(b2); b2.setForeground(Color.red);b1.addActionListe
15、ner(this);b2.addActionListener(this); public void actionPerformed(ActionEvent e) if(e.getSource()=b1) b1.setText(已按过); b1.setForeground(Color.gray); if(e.getSource()=b2)b2.setText(已按过); b2.setForeground(Color.gray); public static void main(String args)MyFrame myFrame = new MyFrame();myFrame.setTitle
16、(Show);myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);myFrame.setSize(250,250);myFrame.setVisible(true);5. 请编写一个Applicaion,其功能为:在其窗口中摆放三个单选按钮,令它们的标签分别为“选项1”、“选项2”、“选项3”, 初始时,所有按钮均可见;以后,如果某个单选按钮被选中了,就通过消息对话框显示它被选中的信息(如,若点击了第二个单选按钮,则显示“你选择了”选项2”), 并使该单选按钮自身不可见,而使其它单选按钮变为可见的。(知识点考察:定义单选按钮和消息提示框
17、,点击按钮触发事件处理过程,修改提示框的visible属性)程序import java.awt.*;import javax.swing.*;import java.awt.event.*;class MyFrame extends JFrame implements ActionListener ButtonGroup optGroup;JRadioButton opt1,opt2,opt3;String s=;boolean b=false;public MyFrame()optGroup=new ButtonGroup( );opt1=new JRadioButton(选项1);opt2
18、=new JRadioButton(选项2);opt3=new JRadioButton(选项3);optGroup.add(opt1);optGroup.add(opt2);optGroup.add(opt3);this.getContentPane().setLayout(new FlowLayout();this.getContentPane().add(opt1);this.getContentPane().add(opt2);this.getContentPane().add(opt3);opt1.addActionListener(this);opt2.addActionListe
19、ner(this);opt3.addActionListener(this); public void actionPerformed(ActionEvent e)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(
20、true);opt2.setVisible(false); opt3.setVisible(true);if(e.getSource()=opt3) JOptionPane.showMessageDialog(this,你选择了选项3);opt1.setVisible(true);opt2.setVisible(true); opt3.setVisible(false);public static void main(String args)MyFrame myFrame = new MyFrame();myFrame.setTitle(Show);myFrame.setDefaultClos
21、eOperation(JFrame.EXIT_ON_CLOSE);myFrame.setSize(250,250);myFrame.setVisible(true);7. 请编写一个Applet,在其窗口中摆放两复选按钮框,通过一个文本域显示它们被选中(那个被选中、或两个均被选中、或两个均未选中)的信息。(知识点考察:定义复选按钮,点击按钮触发事件处理过程)程序import java.awt.*;import javax.swing.*;import java.awt.event.*;class MyFrame extends JFrame implements ItemListener pr
22、ivate JTextField t;private JCheckBox opt1,opt2;public MyFrame()t=new JTextField(20); this.getContentPane().setLayout(new FlowLayout();this.getContentPane().add(t);opt1=new JCheckBox(选项1); this.getContentPane().add(opt1); opt1.addItemListener(this); opt2=new JCheckBox(选项2); this.getContentPane().add(
23、opt2); opt2.addItemListener(this); public void itemStateChanged(ItemEvent e)String s=;if(opt1.isSelected() s=选择了选项1; if(opt2.isSelected() s=s+选择了选项2; t.setText(s); public static void main(String args)MyFrame myFrame = new MyFrame();myFrame.setTitle(Show);myFrame.setDefaultCloseOperation(JFrame.EXIT_
24、ON_CLOSE);myFrame.setSize(250,250);myFrame.setVisible(true);8. 程序在画板中显示一条信息,并利用两个按钮up和down上下移动该信息。程序输出结果如下图所示。(知识点考察:点击按钮触发事件处理过程,注册监听器)答案:import java.awt.*;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.*;public class ButtonDemo extends JFrame implements A
25、ctionListener / Declare a panel for displaying message private MessagePanel messagePanel; / Declare two buttons to move the message left and right private JButton jbtUp, jbtDown; / Main method public static void main(String args) ButtonDemo frame = new ButtonDemo(); / frame.setDefaultCloseOperation(
26、JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); public ButtonDemo() setTitle(Button Demo); / Create a MessagePanel instance and set colors messagePanel = new MessagePanel(Welcome to Java); messagePanel.setBackground(Color.yellow); / Create Panel jpButtons to hold two Buttons JPanel jpBu
27、ttons = new JPanel(); jpButtons.setLayout(new FlowLayout(); jpButtons.add(jbtUp = new JButton(); jpButtons.add(jbtDown = new JButton(); / Set button text jbtUp.setText(Up); jbtDown.setText(Down); / Set keyboard mnemonics jbtUp.setMnemonic(U); jbtDown.setMnemonic(D); / Set icons /jbtUp.setIcon(new Im
28、ageIcon(images/left.gif); /jbtDown.setIcon(new ImageIcon(images/right.gif); / Set toolTipText on the Up and Down buttons jbtUp.setToolTipText(Move message to Up); jbtDown.setToolTipText(Move message to Down); / Place panels in the frame getContentPane().setLayout(new BorderLayout(); getContentPane()
29、.add(messagePanel, BorderLayout.CENTER); getContentPane().add(jpButtons, BorderLayout.SOUTH); / Register listeners with the buttons jbtUp.addActionListener(this); jbtDown.addActionListener(this); / Handle button events public void actionPerformed(ActionEvent e) if (e.getSource() = jbtUp) up(); else
30、if (e.getSource() = jbtDown) down(); / Move the message in the panel left private void up() int y = messagePanel.getYCoordinate(); if (y 10) / Shift the message to the left messagePanel.setYCoordinate(y-10); messagePanel.repaint(); / Move the message in the panel right private void down() int y = me
31、ssagePanel.getYCoordinate(); if (y getSize().width - 120) / Shift the message to the right messagePanel.setYCoordinate(y+10); messagePanel.repaint();9 .使用文本区和滚动条技术相结合显示一段字符串,程序输出结果如下图所示。(知识点考察:定义文本区,设置滚动条)答案:import java.awt.*;import javax.swing.*;public class TextAreaDemo extends JFrame private Desc
32、riptionPanel descriptionPanel = new DescriptionPanel(); public static void main(String args) TextAreaDemo frame = new TextAreaDemo(); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle(Text Area Demo); frame.setVisible(true); public TextAreaDemo() String description =
33、 Task scheduling is of great significance to + shorten performing time and minimize the cost for computational + Grid. A grid task schedule algorithm is presented in this paper, + which is based on a constraint satisfaction neural network. The + constraint satisfaction means to remove the violations
34、 for sequence+ and resource constraints during scheduling subtasks for grid + environment. The data-transferring costs among subtasks are also+ considered in our task scheduling. The simulation in this paper+ has shown that the task schedule algorithm is efficient with respect+ to the quality of sol
35、utions and the solving speed. ; descriptionPanel.setTextDescription(description); getContentPane().setLayout(new BorderLayout(); getContentPane().add(descriptionPanel, BorderLayout.CENTER);class DescriptionPanel extends JPanel private JTextArea jtaTextDescription; public DescriptionPanel() JScrollPa
36、ne scrollPane = new JScrollPane (jtaTextDescription = new JTextArea(); jtaTextDescription.setFont(new Font(Serif, Font.PLAIN, 14); jtaTextDescription.setLineWrap(true); jtaTextDescription.setWrapStyleWord(true); scrollPane.setPreferredSize(new Dimension(200, 100); setLayout(new BorderLayout(); add(s
37、crollPane, BorderLayout.CENTER); public void setTextDescription(String text) jtaTextDescription.setText(text);10. 编写一个程序,包含4个button,各代表正方形,矩形,圆和椭圆,如下图所示。(知识点考察:按钮触发事件过程,绘制正方形、矩形、圆和椭圆)答案:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TextFieldDemo extends JFrame implement
38、s ActionListener private JButton jbtSquare,jbtRectangle,jbtCircle,jbtOvel; / Declare Add button FigurePanel p1 = new FigurePanel(1); public static void main(String args) TextFieldDemo frame = new TextFieldDemo(); frame.setSize(400,300); frame.setVisible(true); public TextFieldDemo() setTitle(TextFie
39、ldDemo); setBackground(Color.yellow); setForeground(Color.black); JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout(); p2.add(jbtSquare = new JButton(Square); p2.add(jbtRectangle = new JButton(Rectangle); p2.add(jbtCircle = new JButton(Circle); p2.add(jbtOvel = new JButton(Ovel); getContentPane(
40、).setLayout(new BorderLayout(); getContentPane().add(p1, BorderLayout.CENTER); getContentPane().add(p2, BorderLayout.SOUTH); jbtSquare.addActionListener(this); jbtRectangle.addActionListener(this); jbtCircle.addActionListener(this); jbtOvel.addActionListener(this); public void actionPerformed(Action
41、Event e) if (e.getSource() = jbtSquare) p1.setFigure(1); p1.repaint(); if (e.getSource() = jbtRectangle) p1.setFigure(2); p1.repaint(); if (e.getSource() = jbtCircle) p1.setFigure(3); p1.repaint(); if (e.getSource() = jbtOvel) p1.setFigure(4); p1.repaint();class FigurePanel extends JPanel final stat
42、ic int SQUARE = 1; final static int RECTANGLE = 2; final static int CIRCLE = 3; final static int OVAL = 4; private int figureType = 1; / Constructing a figure panel public FigurePanel(int figureType) this.figureType = figureType; public void setFigure(int figureType) this.figureType = figureType; / Drawing a figure on the panel public void paintComponent(Graphics g) super.paintComponent(g); / Get the appropriate size for the figure int width = getSize().width; int height = getSize().height; int side = (int)(0.80*Math.min(width, h