}
else if(e.getSource()==button2) {
n=n1-n2;
text3.setText(String.valueOf(n)); }
else if(e.getSource()==button3) {
n=n1*n2;
text3.setText(String.valueOf(n)); } else {
n=n1/n2;
text3.setText(String.valueOf(n)); } } }
public class Class1 {
public static void main (String[] args) {
new F3(); } }
8、设计两个文本区和一个“确定”按钮,当在第一个文本区输入文本时,第二个文本区也会显示相同的文本;当点击按钮时,在第二个文本区内会显示“我按了确定按钮” import java.awt.*;
import java.awt.event.*;
class F4 extends Frame implements TextListener,ActionListener { TextArea text1,text2;Button button; F4()
{ text1=new TextArea(\ text2=new TextArea(\ button=new Button(\确定\
add(text1);add(text2);add(button); text1.addTextListener(this); button.addActionListener(this); setBounds(100,100,400,200); setVisible(true); }
public void textValueChanged(TextEvent e) { if(e.getSource()==text1)
{ text2.setText(text1.getText()); } }
public void actionPerformed(ActionEvent e)
- 36 -
{ if (e.getSource()==button)
text2.setText(\我按了确定按钮\ } }
public class Class1 {
public static void main (String[] args) {
new F4(); } }
9、下图是一程序的运行结果,请编程序完成。
import java.awt.*;
class Mypanel1 extends Panel {
Checkbox box1,box2,box3; Mypanel1() {
box1=new Checkbox(\音乐\ box2=new Checkbox(\体育\ box3=new Checkbox(\吹牛\ add(box1);add(box2);add(box3); } }
class Mypanel2 extends Panel {
Checkbox box1,box2,box3; Mypanel2() {
box1=new Checkbox(\读书\ box2=new Checkbox(\电脑\ box3=new Checkbox(\电影\ add(box1);add(box2);add(box3); } }
class F5 extends Frame {
- 37 -
Mypanel1 panel1; Mypanel2 panel2; F5() {
setLayout(new GridLayout(2,2)); panel1=new Mypanel1(); panel2=new Mypanel2();
add(panel1);add(new Label());add(new Label());add(panel2); setBounds(100,100,400,200); setVisible(true); } }
public class Class1 {
public static void main (String[] args) {
new F5(); } }
10、如下图所示,当单击“开南窗”按钮时,出现名字为“阳光窗口”;当单击“开北窗”按钮时,出现名字为“冰雪之窗”的窗口;当单击“关南窗”或“关北窗”按钮时,相应窗口就关闭。
答:程序如下 import java.awt.*;
import java.awt.event.*;
class Yourwindow extends Frame {
Yourwindow(String s,int a,int b) {
super(s);
setLayout(new GridLayout(1,1)); setSize(a,b);
setBackground(Color.white); setVisible(false); validate(); } }
class F6 extends Frame implements ActionListener {
- 38 -
Yourwindow window1,window2;
Button button1,button2,button3,button4; F6() {
button1=new Button(\开南窗\ button2=new Button(\开北窗\ button3=new Button(\关南窗\ button4=new Button(\关北窗\
window1=new Yourwindow(\阳光之窗\ window2=new Yourwindow(\冰雪之窗\ button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this);
add(button1);add(button2);add(button3);add(button4); setBounds(100,100,400,200); setVisible(true); }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1) {
window1.setVisible(true); }
else if(e.getSource()==button3) {
window1.setVisible(false); }
else if(e.getSource()==button2) {
window2.setVisible(true); }
else if(e.getSource()==button4) {
window2.setVisible(false); } } }
public class Class1 {
public static void main (String[] args) {
new F6(); } }
11、在显示区域上有一个文本域、一个面板和由三个单选按钮组成的按钮组,每个单选按钮对应的标识为“中国队”,“韩国队”,“日本队”并放入面板中。当选择相应的单选按钮时。在文本域中会显示出相应的“选中
- 39 -
了XX队”。编写程序代码完成上述要求,布局由自己定。 答:程序如下 import java.awt.*;
import java.awt.event.*;
class F7 extends Frame implements ItemListener {
Label label1;
TextField text1; Panel p;
CheckboxGroup checkboxgroup1; Checkbox radio1; Checkbox radio2; Checkbox radio3; F7() {
Label label1= new Label(\请选择参赛队:\ text1=new TextField(40); p=new Panel();
checkboxgroup1=new CheckboxGroup();
radio1=new Checkbox(\中国队\ radio2=new Checkbox(\韩国队\ radio3=new Checkbox(\日本队\ radio1.addItemListener(this);
radio2.addItemListener(this); radio3.addItemListener(this); p.add(radio1); p.add(radio2); p.add(radio3); add(label1); add(p); add(text1);
setBounds(100,100,400,200); setVisible(true); }
public void itemStateChanged(ItemEvent e) {
if (e.getItemSelectable()==radio1) {
text1.setText(\选中了中国队\ }
else if (e.getItemSelectable()==radio2) {
text1.setText(\选中了韩国队\ }
else if (e.getItemSelectable()==radio3) {
- 40 -