内容发布更新时间 : 2024/12/23 22:17:01星期一 下面是文章的全部内容请认真阅读。
boxV1.add(Box.createVerticalStrut(8)); boxV1.add(new Label(\职业\boxV2=Box.createVerticalBox(); boxV2.add(text1);
boxV2.add(Box.createVerticalStrut(8)); boxV2.add(text2);
boxV2.add(Box.createVerticalStrut(8)); boxV2.add(text3);
baseBox=Box.createHorizontalBox(); baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10)); baseBox.add(boxV2);
setLayout(new FlowLayout()); add(baseBox); add(button); add(textarea);
addWindowListener(new WindowAdapter() { {
System.exit(0); } } );
textarea.setEditable(false); button.addActionListener(this); setBounds(100,100,210,250); setVisible(true); validate(); }
public void actionPerformed(ActionEvent e) {
boolean a; int b; String s;
if(e.getSource()==button) {
s=text2.getText(); a=s.endsWith(\b=s.indexOf(\if(a&&b>0) {
String str1=text1.getText()+\String str2=textarea.getText();
public void windowClosing(WindowEvent e)
textarea.setText(str2+str1); } else {
text2.setText(\输入了非法格式的E-mail地址\} } } }
public class Test {
public static void main(String args[]) {
new WindowBox(); } }
8. 写一个应用程序,要求编写一个Panel的子类MyPanel,MyPanel中有一个文本框和一个按钮,要求MyPanel的实例作为其按钮的ActionEvent事件的监视器,当单击按钮时,程序获取文本框中的文本,
并将该文本作为按钮的名称。然后在编写一个Frame的子类,即窗口。窗口的布局为BorderLayout布局。窗口中添加两个MyPanel面板,分别添加到窗口的东部区域和西部区域。 答: import java.awt.*; import java.awt.event.*;
class MyPanel extends Panel implements ActionListener {
String name; TextField text; Button button; MyPanel() {
text=new TextField(10); button=new Button(\确定\add(text); add(button);
button.addActionListener(this); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); } }
); }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button) {
name=text.getText(); button.setLabel(name); } } }
class MyFrame extends Frame {
MyPanel panel1,panel2; MyFrame() {
panel1=new MyPanel(); panel2=new MyPanel();
add(panel1,BorderLayout.EAST); add(panel2,BorderLayout.WEST); setBounds(100,100,400,100);
setVisible(true); validate(); } }
public class Test {
public static void main(String args[]) {
MyFrame win=new MyFrame(); } }
9. 参照例子7.18编写一个应用程序,要求有一个画布,在画布上绘制一个矩形,用户通过文本框输入矩形的宽和高以及矩形左上角的位置坐标。 答: import java.awt.*; import java.awt.event.*; class Mycanvas extends Canvas {
int x,y,w,h; Mycanvas() {
setBackground(Color.cyan); }
public void setX(int x) { this.x=x; }
public void setY(int y) { this.y=y; }
public void setW(int w) { this.w=w; }
public void setH(int h) { this.h=h; }
public void paint(Graphics g) {
g.drawRect(x,y,w,h); } }
class WindowCanvas extends Frame implements ActionListener
{
Mycanvas canvas;
TextField text1,text2,text3,text4; Button button; WindowCanvas() {
canvas=new Mycanvas(); text1=new TextField(4); text2=new TextField(4); text3=new TextField(5); text4=new TextField(5);
Panel pNorth=new Panel(),pSouth=new Panel(); button=new Button(\确定\button.addActionListener(this); pNorth.add(new Label(\矩形的宽: \pNorth.add(text3);
pNorth.add(new Label(\矩形的高: \
pNorth.add(text4);
pSouth.add(new Label(\左上角位置坐标:\pSouth.add(text1); pSouth.add(text2); pSouth.add(button);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); } } );
add(canvas,BorderLayout.CENTER); add(pNorth,BorderLayout.NORTH); add(pSouth,BorderLayout.SOUTH); setBounds(100,100,500,500); setVisible(true); validate(); }
public void actionPerformed(ActionEvent e) {
int x,y,w,h; try {
x=Integer.parseInt(text1.getText()); y=Integer.parseInt(text2.getText());
w=Integer.parseInt(text3.getText()); h=Integer.parseInt(text4.getText()); canvas.setX(x); canvas.setY(y); canvas.setW(w); canvas.setH(h); canvas.repaint(); }
catch(NumberFormatException ee) {
x=0;y=0;w=0;h=0; } } }