《Java语言程序设计(一)》课后习题答案全集 下载本文

源文件:Work5_6.java

import java.applet.Applet; import java.awt.event.*; import javax.swing.JButton;

/** *

* @author 段智敏 */

public class Work5_6 extends Applet implements ActionListener { private static final long serialVersionUID = 1L; private int count = 0; private JButton button; public void init() { button = new JButton(\ + count + \); button.addActionListener(this); this.add(button); this.setSize(300, 200); } public void actionPerformed(ActionEvent arg0) { count++; button.setText(\ + count); } }

5.7 创建一个有文本框和三个按钮的程序。当按下某个按钮时,使不同的文字显示在文本框中。

程序运行结果:

41

源文件:Work5_7.java

import java.awt.event.*; import java.awt.*; import javax.swing.*; /** * 5.7创建一个有文本框和三个按钮的程序。当按下某个按钮时,使不同的文字显示在文本框中。

* @author 段智敏 */

public class Work5_7 extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JTextField text; private JButton button1, button2, button3, exit_button; private JPanel panel; public Work5_7() { text = new JTextField(10); button1 = new JButton(\刘德华\); button2 = new JButton(\张学友\); button3 = new JButton(\蔡依林\); exit_button = new JButton(\退 出\); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); exit_button.addActionListener(this); panel = new JPanel(); panel.add(button1); panel.add(button2); panel.add(button3); this.setLayout(new FlowLayout()); this.add(text); this.add(panel); this.add(exit_button); this.setSize(300, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if (e.getSource() == exit_button) {

42

}

System.exit(0); } else { text.setText(e.getActionCommand()); } }

public static void main(String args[]) { new Work5_7(); }

5.8 编写一个有两个文本框的小应用程序,在第一个文本框输入英语单词,在第二个文本框会自动显示汉语解释;在第一个文本框输入汉语单词,在第二个文本框中显示英语解释。设英语单词表只有少许几个。

程序运行结果:

源文件:Work5_8.java

import javax.swing.*; import java.awt.event.*; import java.awt.*; /**

* @author 段智敏 */

public class Work5_8 extends JFrame implements ActionListener ,KeyListener { private static final long serialVersionUID = 1L; private JTextField input_English, input_Chinese; private JLabel label1, label2; private JPanel panel1, panel2; private String[] word_English = { \, \, \ };// 可以随时增加词汇 private String[] word_Chinese = { \月亮\, \星星\, \太阳\ }; private JButton clear_button; public Work5_8() { panel1 = new JPanel(); panel2 = new JPanel(); input_English = new JTextField(15);

43

input_Chinese = new JTextField(15); label1 = new JLabel(\输入英语:\); label2 = new JLabel(\输入汉语:\); clear_button = new JButton(\清空文本框\); panel1.add(label1); panel1.add(input_English); panel2.add(label2); panel2.add(input_Chinese); input_English.addKeyListener(this); input_Chinese.addKeyListener(this); clear_button.addActionListener(this); this.setLayout(new FlowLayout()); this.add(panel1); this.add(panel2); this.add(clear_button); this.setBounds(300, 200, 350, 200); this.validate(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

public void actionPerformed(ActionEvent e) { if (e.getSource() == clear_button) { input_English.setText(\); input_Chinese.setText(\); } }

@Override

public void keyTyped(KeyEvent e) { } @Override

public void keyPressed(KeyEvent e) { } @Override

public void keyReleased(KeyEvent e) {

if (e.getKeyCode()>=65 && e.getKeyCode()<=91)//在英文框中输入英文 {

String word = input_English.getText();

for (int i = 0; i < word_English.length; i++) { }

if (word.equals(word_English[i])) { }

input_Chinese.setText(word_Chinese[i]); return;

input_Chinese.setText(\没有该Chinese翻译\); } else{

String word = input_Chinese.getText();

44

}

for (int i = 0; i < word_Chinese.length; i++) { }

if (word.equals(word_Chinese[i])) { }

input_English.setText(word_English[i]); return;

input_English.setText(\没有该English翻译\); } }

public static void main(String[] args) { new Work5_8(); }

5.9 一个标签、一个文本框、一个文本区、两个按钮。当在文本区中输入若干数后,点击求和按钮,在文本框显示输入数的和,标签显示“输入数的和”;点击求平均值按钮,在文本框显示输入数的平均值,标签显示“输入数的平均值”。要求文本区设有滚动条。

程序运行结果:

源文件Work5_9.java

import java.util.*; import java.awt.*;

import java.awt.event.*; import javax.swing.*; /** *

* @author 段智敏 */

public class Work5_9 extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JLabel result_label; private JTextArea input_textArea; private JTextField showResult_textField;

45