自考JAVA语言程序设计(一)课后习题答案和源代码(第五章).doc
文本预览下载声明
第五章
5.6 编写已个小应用程序,小应用程序窗口有一个按钮,点击这个按钮时,点击按钮的次数会显示在按钮上。
程序运行结果:
源文件:Work5_6.java
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.JButton;
/**
* 5.6 编写已个小应用程序,小应用程序窗口有一个按钮,点击这个按钮时,点击按钮的次数会显示在按钮上。
* @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 创建一个有文本框和三个按钮的程序。当按下某个按钮时,使不同的文字显示在文本框中。
程序运行结果:
源文件: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()
{
super(第五章,第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)
{
System.exit(0);
}
else
{
text.setText(e.getActionCommand());
}
}
public static void main(String args[])
{
new Work5_7();
}
}
5.8 编写一个有两个文本框的小应用程序,在第一个文本框输入英语单词,在第二个文本框会自动显示汉语解
显示全部