Java入门代码练习与记录-2

2023-03-20 10:54:37 浏览数 (2)

这次主要练习的是图形用户界面设计,当然也有一些乱起八糟的东西,但主要是Applet的内容。

代码语言:javascript复制
// java 序列化与反序列化demo
// 导入反序列化所需要的包
/*
import javax.naming.event.ObjectChangeListener;
import java.io.*;

class user implements java.io.Serializable{
    public String name;
    public String id;
    public void OutInfo(){
        System.out.println(this.name this.id);
    }
}

public class test {
    public static void main(String[] args){
        user ldd = new user();
        ldd.name = "tommonkey";
        ldd.id = "10086";
        System.out.println(ldd.id);

        try {
            // 进行序列化,会在当前目录下生成一个序列化文件
            FileOutputStream fileOut = new FileOutputStream("./serial.txt");    // 打开文件流
            ObjectOutputStream objOut = new ObjectOutputStream(fileOut);    // 创建对象流
            objOut.writeObject(ldd);    // 输出序列化对象
            objOut.close();     // 关闭
            fileOut.close();
            System.out.println("serial is over!");

            // 进行反序列化
            user n = null;
            FileInputStream fileIn = new FileInputStream("./serial.txt");
            ObjectInput objIn = new ObjectInputStream(fileIn);
            n = (user) objIn.readObject();
            objIn.close();
            fileIn.close();
            System.out.println(n.name n.id);
        }catch (IOException i){
            i.printStackTrace();
            return;
        }catch (ClassNotFoundException a){
            System.out.println("Employee class not found");
            a.printStackTrace();
            return;
        }
    }
}
*/





// Runtime.getRuntime().exec():系统命令执行方法
/*
public class test {
    public static void main(String[] args) throws Exception{
        Runtime.getRuntime().exec("notepad.exe");   // 打开记事本
    }
}
*/





// applet,awt-GUI界面响应事件流程demo
// 启动方法,再html页面中设置此class文件的路径名,再cmd启动:AppletViewer ****.html
// html中添加路径的方法:<APPLET CODE="test.class" width=500 height=500></APPLET>
/*
import sun.security.mscapi.CPublicKey;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class test extends Applet implements ActionListener{     // 继承appelt 且实现接口:动作监听
    private Button but1,but2;
    int button = 0;
    public void init(){
        but1 = new Button("按钮1");
        but2 = new Button("按钮2");
        add(but1);      // 注册事件监听
        add(but2);
        but1.addActionListener(this);   // 事件监听本对象:but1
        but2.addActionListener(this);
    }
    public void paint(Graphics g){
        if(button==1)
            g.drawString("事件监听到你选择了按钮1!",10,20);
        else if(button==2)
            g.drawString("事件监听到你选择了按钮2!",10,20);
    }
    public void actionPerformed(ActionEvent e){     // 实现动作监听必须要有实现该方法
        if(e.getActionCommand().equals("按钮1"))
            button = 1;
        else if(e.getActionCommand().equals("按钮2"))
            button =2;
        repaint();
    }
}
*/




// applet的文本框textField与文本区textArea演示
/*
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class test extends Applet implements ActionListener,TextListener{
    TextArea ta1,ta2;     // 创建两个文本区对象
    TextField text1;      // 创建一个文本框对象
    public void init() {
        text1 = new TextField("this is textField!", 30);
        text1.addActionListener(this);
        text1.addTextListener(this);
        add(text1);

        ta1 = new TextArea("this is textArea!", 100, 100);
        ta1.addTextListener(this);
        add(ta1);
        ta2 = new TextArea("this is orther textArea!", 100, 100);
        ta2.addTextListener(this);
        add(ta2);
    }
    public void actionPerformed(ActionEvent e){
        ta1.append("we have capture you action event and output here!n");
        repaint();    //刷新
    }
    public void textValueChanged(TextEvent e){
        ta2.append("we have capture you action event and output here!n");
        repaint();
    }
}
 */
 
 
 
// Appl的单选框与列表demo
/*
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class test extends Applet implements ItemListener{
    CheckboxGroup cbk;
    Checkbox cb1,cb2,cb3,cb4;
    Color showColor;
    List ls;
    String[] colorName={"red","yellow","blue","black"};
    String[] setColor={String.valueOf(Color.red), String.valueOf(Color.yellow), String.valueOf(Color.blue), String.valueOf(Color.black)};
    int num;

    public void init(){
        cbk = new CheckboxGroup();
        cb1 = new Checkbox("Red",cbk,true);
        cb2 = new Checkbox("Yellow",cbk,false);
        cb3 = new Checkbox("Blue",cbk,false);
        cb4 = new Checkbox("Black",cbk,false);
        showColor = Color.red;
        cb1.addItemListener(this);
        cb2.addItemListener(this);
        cb3.addItemListener(this);
        cb4.addItemListener(this);
        add(cb1);
        add(cb2);
        add(cb3);
        add(cb4);

        ls = new List(4,false);
        for(int i=0;i<colorName.length;i  ){
            ls.add(colorName[i]);
        }
        ls.addItemListener(this);
        add(ls);
    }

    public void itemStateChanged(ItemEvent e){
        if (e.getSource() == cb1){
            showColor = Color.red;      // getSource()方法:返回当前动作所指向的对象,包含对象的所有信息,而getActionCommond()是指当前动作指向的对象的名称
        }
        if (e.getSource() == cb2){
            showColor = Color.yellow;
        }
        if (e.getSource() == cb3){
            showColor = Color.blue;
        }
        if (e.getSource() == cb4){
            showColor = Color.black;
        }

        // showStatus()方法:打印一行状态信息
        showStatus(ls.getSelectedItem() ":序列号为 " ls.getSelectedIndex());
        num = ls.getSelectedIndex();
        repaint();
    }

    public void paint(Graphics g){
        g.setColor(showColor);
        g.fillRect(50,80,50,50);

        g.setColor(Color.decode(setColor[num]));    // 这里的类型转化出了问题,我没找解决办法
        g.fillOval(270,100,50,50);
    }
}
 */
 
 
 // 容器Panel(面板):无边框容器,没有边框,也不能移动,缩放,和关闭,通常作为一个容器组件被加到其他容器中
/*
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class test extends Applet implements ActionListener,ContainerListener{
    Button but1,but2;
    Label lab1,lab2,lab3,lab4,lab5;
    Panel pan1,pan2,pan3,pan4,pan5;         // 创建五个panel容器(面板)
    public void init(){
        pan1 = new Panel();
        pan2 = new Panel();
        pan3 = new Panel();
        pan4 = new Panel();
        pan5 = new Panel();
        pan1.setBackground(Color.red);      // 设置面板背景颜色
        pan2.setBackground(Color.yellow);
        pan3.setBackground(Color.black);
        pan4.setBackground(Color.blue);
        pan5.setBackground(Color.green);

        lab1 = new Label("this is red panel");
        lab2 = new Label("this is yellow panel");
        lab3 = new Label("this is black panel");
        lab4 = new Label("this is blue panel");
        lab5 = new Label("this is green panel");

        but1 = new Button("this is button1");
        but2 = new Button("this is button2");

        pan1.add(lab1);         // 将标签添加到panel上
        pan2.add(lab2);
        pan3.add(lab3);
        pan4.add(lab4);
        pan5.add(lab5);

        pan3.add(but1);         // 将按钮添加到panel上
        pan4.add(but2);

        pan1.add(pan3);
        pan2.add(pan4);

        add(pan1);          // 将panel添加到容器中
        add(pan2);
        add(pan5);

        but1.addActionListener(this);
        but2.addActionListener(this);

        pan1.addContainerListener(this);
        pan2.addContainerListener(this);
    }

    public void actionPerformed(ActionEvent e){
        if (e.getSource() == but1){
            pan1.remove(pan3);
            showStatus("remove panel3 in panel1");
        }else{
            pan2.remove(pan4);
            showStatus("remove panel4 in panel2");
        }
    }

    public void componentRemoved(ContainerEvent e){

    }

    public void componentAdded(ContainerEvent e){

    }
}
 */

0 人点赞