java程序建立页面_Java学习笔记-13.创建窗口和程序片

1.init()方法:程序片第一次被创建,初次运行初始化程序片时调用。

start()方法:每当程序片进入web浏览器中,并且允许程序片启动他的常规操作时调用(特殊的程序片被stop()关闭);同样在init()后调用。

paint()方法:基础类Component的一部分(继承结构中上朔三级)。作为update()的一部分调用,以便对程序片的画布进行特殊的描绘。

stop()方法:每次程序片从web浏览器的视线中离开时调用,时程序片能关闭代价高昂的操作;同样在调用destroy()前调用。

destroy()方法:程序片不再需要,将它从页面中卸载时调用。

2.事件模型:(1)先在类中添加addXXXXXListener()方法。

(2)重写执行接口的方法。

packagethirteen;import java.awt.*;import java.awt.event.*;import java.applet.*;public class Button2New extendsApplet {

Button b1= new Button("button1"), b2 = new Button("button2");public voidinit() {

b1.addActionListener(newB1());

b2.addActionListener(newB2());

add(b1);

add(b2);

}class B1 implementsActionListener {public voidactionPerformed(ActionEvent e) {

getAppletContext().showStatus("BUTTon1");

}

}class B2 implementsActionListener {public voidactionPerformed(ActionEvent e) {

getAppletContext().showStatus("Button2");

}

}

}

3.制作窗口:(1)main()方法中新建一个Frame类,并将applet的衍生类给其初始化。

(2)继承WindowAdapter类,并重写windowClosing()方法。

(3)执行Frame的setVisible()方法。

packagethirteen;import java.applet.*;import java.applet.*;importjava.awt.BorderLayout;importjava.awt.Button;importjava.awt.TextField;importjava.awt.Desktop.Action;importjava.awt.Frame;import java.awt.event.*;importjava.time.temporal.TemporalQueries;importjavax.swing.table.TableRowSorter;importorg.omg.CORBA.FloatSeqHelper;public class TextNew extendsApplet {

Button b1= new Button("Get Text"), b2 = new Button("Set Text");

TextField t1= new TextField(30), t2 = new TextField(30), t3 = new TextField(30);

String s= newString();public voidinit() {

b1.addActionListener(newB1());

b2.addActionListener(newB2());

t1.addTextListener(newT1());

t1.addActionListener(newT1A());

t1.addKeyListener(newT1K());

add(b1);

add(b2);

add(t1);

add(t2);

add(t3);

}class T1 implementsTextListener {public voidtextValueChanged(TextEvent e) {

t2.setText(t1.getText());

}

}class T1A implementsActionListener {private int count = 0;

@Overridepublic voidactionPerformed(ActionEvent e) {//TODO 自动生成的方法存根

t3.setText("t1 Action Event " + count++);

}

}class T1K extendsKeyAdapter {public voidkeyTyped(KeyEvent e) {

String tString=t1.getText();if (e.getKeyChar() ==KeyEvent.VK_BACK_SPACE) {if (tString.length() > 0) {

tString= tString.substring(0, tString.length() - 1);

t1.setText(tString);

}

}elset1.setText(t1.getText()+Character.toUpperCase(e.getKeyChar()));

t1.setCaretPosition(t1.getText().length());

e.consume();

}

}class B1 implementsActionListener{public voidactionPerformed(ActionEvent e){

s=t1.getSelectedText();if(s.length()==0)s=t1.getText();

t1.setEditable(true);

}

}class B2 implementsActionListener{public voidactionPerformed(ActionEvent e){

t1.setText("Insert by Button2:"+s);

t1.setEditable(false);;

}

}public static voidmain(String[] args){

TextNew applet=newTextNew();

Frame aFrame=new Frame("TextNew");

aFrame.addWindowListener(newWindowAdapter() {public voidwindowClosing(WindowEvent e){

System.exit(0);

}

});

aFrame.add(applet, BorderLayout.CENTER);

aFrame.setSize(300,200);

applet.init();

applet.start();

aFrame.setVisible(true);

}

}

4.JavaBean要求:

(1)所有的类必须放在一个包中,在web中没有包是不存在的。

(2)所有的类必须声明为public class,这样才能够被外部访问。

(3)类中所有的属性都必须封装,使用private声明。

(4)封装的属性如果需要被外部所操作,则必须编写对应的setter,getter方法。

(5)一个JavaBean中至少存在一个无参构造方法。

5.Swing各种边框的一个例子:

packagethirteen;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Borders extendsJPanel {staticJPanel showBorder(Border b) {

JPanel jPanel= newJPanel();

jPanel.setLayout(newBorderLayout());

String nm=b.getClass().toString();

nm= nm.substring(nm.lastIndexOf('.') + 1);

jPanel.add(newJLabel(nm, JLabel.CENTER), BorderLayout.CENTER);

jPanel.setBorder(b);returnjPanel;

}publicBorders() {

setLayout(new GridLayout(2, 4));

add(showBorder(new TitledBorder("Title")));

add(showBorder(newEtchedBorder()));

add(showBorder(newLineBorder(Color.blue)));

add(showBorder(new MatteBorder(5, 5, 30, 30, Color.green)));

add(showBorder(newBevelBorder(BevelBorder.RAISED)));

add(showBorder(newSoftBevelBorder(BevelBorder.LOWERED)));

add(showBorder(new CompoundBorder(new EtchedBorder(), newLineBorder(Color.red))));

}public static voidmain(String[] args) {

Show.inFrame(new Borders(), 500, 300);

}static classShow {public static void inFrame(JPanel jPanel, int width, intheight) {

String title=jPanel.getClass().toString();if (title.indexOf("class") != -1)

title= title.substring(6);

JFrame frame= newJFrame(title);

frame.addWindowListener(newWindowAdapter() {public voidWindowClosing(WindowEvent e) {

System.exit(0);

}

});

frame.getContentPane().add(jPanel, BorderLayout.CENTER);

frame.setSize(width, height);

frame.setVisible(true);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值