Java习题集3 - 图文 联系客服

发布时间 : 星期六 文章Java习题集3 - 图文更新完毕开始阅读

{

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()); }

- 45 -

}

public void actionPerformed(ActionEvent e) { 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;

- 46 -

Mypanel2() {

box1=new Checkbox(\读书\ box2=new Checkbox(\电脑\ box3=new Checkbox(\电影\

add(box1);add(box2);add(box3); } }

class F5 extends Frame {

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、如下图所示,当单击?开南窗?按钮时,出现名字为?阳光窗口?;当单击?开北窗?按钮时,出现名字为?冰雪之窗?的窗口;当单击?关南窗?或?关北窗?按钮时,相应窗口就关闭。

- 47 -

答:程序如下 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 {

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);

- 48 -