?? 第二十六章例子.txt
字號:
}
}
class Win extends Frame implements ActionListener
{ TreeSet 節目清單=null;
TextField 名稱文本框=new TextField(10),
時間文本框=new TextField(5),
刪除文本框=new TextField(5);
Button b_add=new Button("添加節目"),
b_del=new Button("刪除節目"),
b_show =new Button("顯示節目清單");
TextArea 顯示區=new TextArea();
Win()
{ 節目清單=new TreeSet(new Comparator()
{public int compare(Object a,Object b)
{節目 item_1=(節目)a;
節目 item_2=(節目)b;
return item_1.compareTo(item_2);
}
});
Panel 節目單輸入區=new Panel();
節目單輸入區.add(new Label("節目名稱:"));
節目單輸入區.add(名稱文本框);
節目單輸入區.add(new Label("演出時間:"));
節目單輸入區.add(時間文本框);
節目單輸入區.add(new Label("點擊添加:"));
節目單輸入區.add(b_add);
節目單輸入區.add(b_show);
Panel 節目單刪除區=new Panel();
節目單刪除區.add(new Label("輸入演出的時間:"));
節目單刪除區.add(刪除文本框);
節目單刪除區.add(new Label("點擊刪除:"));
節目單刪除區.add(b_del);
Panel 節目單顯示區=new Panel();
節目單顯示區.add(顯示區);
顯示區.setBackground(Color.pink);
b_add.addActionListener(this);b_del.addActionListener(this);
b_show.addActionListener(this);
add(節目單輸入區,"North");add(節目單顯示區,"Center");
add(節目單刪除區,"South");
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==b_add)
{String 名稱=null;double 時間=0.0;
名稱=名稱文本框.getText();
try{時間=Double.valueOf(時間文本框.getText()).doubleValue();
}
catch(NumberFormatException ee)
{時間文本框.setText("請輸入代表時間的實數");
}
節目 programme=new 節目(名稱,時間);
節目清單.add(programme);
showing();
}
else if(e.getSource()==b_del)
{節目 待刪除節目=null;
double time=Double.valueOf(刪除文本框.getText()).doubleValue();
Iterator te=節目清單.iterator();
while(te.hasNext())
{節目 item=(節目)te.next();
if(Math.abs(item.time-time)<=0.000001d)
{待刪除節目=item; }
}
if(待刪除節目!=null) 節目清單.remove(待刪除節目);
showing();
}
else if(e.getSource()==b_show)
{ showing();
}
}
void showing()
{ 顯示區.setText(null);
Iterator iter=節目清單.iterator();
while(iter.hasNext())
{節目 item=(節目)iter.next();
顯示區.append("節目名稱:"+item.name+"演出時間: "+item.time);
顯示區.append("\n");
}
}
}
public class Tree_3
{public static void main(String args[])
{ Win win=new Win();
win.setSize(500,250);win.setVisible(true);
win.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
}
}
26-例子10
import java.util.*;
class Student
{ int english=0; String name,number;
Student(String na,String nu,int e)
{english=e;name=na;number =nu;}
}
public class HT
{ public static void main(String args[])
{ Hashtable hashtable=new Hashtable();
hashtable.put("199901",new Student("199901","王小林",98));
hashtable.put("199902",new Student("199902","能林茂",70));
hashtable.put("199903",new Student("199903","多種林",93));
hashtable.put("199904",new Student("199904","圍林蛤",46));
hashtable.put("199905",new Student("199905","夾貿林",77));
hashtable.put("199906",new Student("199906","噔林可",55));
hashtable.put("199907",new Student("199907","降王林",68));
hashtable.put("199908",new Student("199908","糾林咯",76));
Student stu=(Student)hashtable.get("199902");//檢索一個元素。
System.out.println(stu.number+" "+stu.name+" "+stu.english);
hashtable.remove("199906"); //刪除一個元素
System.out.println("散列表中現在含有:"+hashtable.size()+"個元素");
Enumeration enum=hashtable.elements();
while(enum.hasMoreElements()) //遍歷當前散列表。
{Student s=(Student)enum.nextElement();
System.out.println(s.number+" "+s.name+" "+s.english);
}
}
}
26-例子11
import java.util.*;import java.awt.event.*;import java.awt.*;
import javax.swing.*;import java.io.*;
class 學生 extends JPanel
{String 學號,姓名;float 分數;
學生(String 學號,String 姓名,float 分數)
{this.學號=學號;this.姓名=姓名;this.分數=分數;
}
}
class ShowWin extends JFrame implements ActionListener
{ Hashtable hashtable=new Hashtable();
JTextField 學號文本框=new JTextField(),
姓名文本框=new JTextField(),
分數文本框=new JTextField(),
查詢文本框=new JTextField();
JButton b_add=new JButton("添加成績"),
b_show =new JButton("顯示成績");
JTextField 成績顯示條=new JTextField();
ShowWin()
{Container con=getContentPane();
JPanel 成績輸入區=new JPanel();
成績輸入區.setLayout(new GridLayout(5,2));
成績輸入區.add(new Label("成績輸入區:"));
成績輸入區.add(new Label());
成績輸入區.add(new Label("考生學號:"));
成績輸入區.add(學號文本框);
成績輸入區.add(new JLabel("考生姓名:"));
成績輸入區.add(姓名文本框);
成績輸入區.add(new Label("考生成績:"));
成績輸入區.add(分數文本框);
成績輸入區.add(new Label("點擊添加:"));
成績輸入區.add(b_add);
JPanel 查詢顯示區=new JPanel();
查詢顯示區.setLayout(new GridLayout(3,2));
查詢顯示區.add(new Label("成績查詢區:"));
查詢顯示區.add(new Label());
查詢顯示區.add(new Label("輸入考生的學號:"));
查詢顯示區.add(查詢文本框);
查詢顯示區.add(b_show);
查詢顯示區.add(成績顯示條);
JSplitPane split;
split=new JSplitPane(JSplitPane.VERTICAL_SPLIT,成績輸入區,查詢顯示區);
con.add(split,BorderLayout.CENTER);
con.add(new Label("成績輸入和查詢系統"),BorderLayout.NORTH);
b_add.addActionListener(this);b_show.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==b_add)
{String 學號=null,姓名=null;float 分數=0.0f;
try {學號=學號文本框.getText();
姓名=姓名文本框.getText();
}
catch(NullPointerException ee)
{ 學號文本框.setText("請輸入學號");
姓名文本框.setText("請輸入姓名");
}
try{分數=Float.valueOf(分數文本框.getText()).floatValue();}
catch(NumberFormatException ee)
{分數文本框.setText("請輸入數字字符");}
學生 stu=new 學生(學號,姓名,分數);
hashtable.put(學號,stu);
try {FileOutputStream file=new FileOutputStream("score.txt");
ObjectOutputStream out=new ObjectOutputStream(file);
out.writeObject(hashtable); out.close();
}
catch(IOException event){}
}
else if(e.getSource()==b_show)
{ String temp=null;
temp=查詢文本框.getText();
成績顯示條.setText(null);
try {FileInputStream come_in=new FileInputStream("score.txt");
ObjectInputStream in=new ObjectInputStream(come_in);
hashtable=(Hashtable)in.readObject();in.close();
}
catch(ClassNotFoundException event){}
catch(IOException event){System.out.println("文件無法讀出");}
學生 s=(學生)hashtable.get(temp);
成績顯示條.setText("姓名:"+s.姓名+"學號:"+s.學號+"成績:"+s.分數);
}
}
}
public class HT_2
{public static void main(String args[])
{ ShowWin win=new ShowWin();
win.setSize(100,100); win.setVisible(true);
win.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
}
}
26-例子12
import java.util.*;
class Example26_12
{public static void main(String args[])
{ Vector vector=new Vector(); Date date=new Date();
vector.add(new Integer(1));vector.add(new Float(3.45f));
vector.add(new Double(7.75));vector.add(new Boolean(true));
vector.add(date);
System.out.println(vector.size());
Integer number1=(Integer)vector.get(0);
System.out.println("向量的第1個元素: "+number1.intValue());
Float number2=(Float)vector.get(1);
System.out.println("向量的第2個元素: "+number2.floatValue());
Double number3=(Double)vector.get(2);
System.out.println("向量的第3個元素: "+number3.doubleValue());
Boolean number4=(Boolean)vector.get(3);
System.out.println("向量的第4個元素: "+number4.booleanValue());
date=(Date)vector.lastElement();
System.out.println("向量的第5個元素: "+date.toString());
if(vector.contains(date))
System.out.println("ok");
}
}
26-例子13
import java.applet.*;
import java.awt.*;import java.util.*;
import java.awt.event.*;
class Point
{int x,y;
Point(int x,int y)
{this.x=x;this.y=y;
}
}
public class Example26_13 extends Applet
implements MouseMotionListener,MouseListener
{ int x=-1,y=-1;
Vector v=null;int n=1;
public void init()
{ setBackground(Color.green);
addMouseMotionListener(this); addMouseListener(this);
v=new Vector();
}
public void paint(Graphics g)
{if(x!=-1&&y!=-1)
{ n=v.size();
for(int i=0;i<n-1;i++)
{Point p1=(Point)v.elementAt(i);
Point p2=(Point)v.elementAt(i+1);
g.drawLine(p1.x,p1.y,p2.x,p2.y);
}
}
}
public void mouseDragged(MouseEvent e)
{ x=(int)e.getX();y=(int)e.getY();
Point p=new Point(x,y);
v.addElement(p);
repaint();
}
public void mouseMoved(MouseEvent e)
{}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e)
{v.removeAllElements();}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void update(Graphics g)
{ paint(g);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -