?? 第二十五章例子.txt
字號:
}
25-例子15(補充)
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
class DigitDocumnet extends PlainDocument
{ public void insertString(int offset ,String s,AttributeSet a)
{ char c=s.charAt(0);
if ((c<='9'&&c>='0')||(c=='.'))
{ try {super.insertString(offset,s,a);}
catch(BadLocationException e){}
}
}
}
public class DigitText extends JApplet
{ JTextField text=null;
DigitDocumnet document=new DigitDocumnet();
public void init()
{ text=new JTextField(30);
Container con= getContentPane();
con.setLayout(new FlowLayout());
text.setDocument(document);
con.add(text);
}
}
25-例子16
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class Example25_16 extends JApplet
{ JTextPane textpane;
public void init()
{ textpane=new JTextPane();//創建文本窗格。
getContentPane().add(textpane);
}
}
25-例子17
import javax.swing.*;import javax.swing.text.*;
import java.awt.*;
public class Example25_17 extends JApplet
{ JTextPane textpane;
MutableAttributeSet center_align,char_style;
public void init()
{ textpane=new JTextPane();//創建文本窗格。
JScrollPane scroll=
new JScrollPane(textpane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
center_align=new SimpleAttributeSet();
char_style=new SimpleAttributeSet(); //創建屬性對象。
StyleConstants.setAlignment(center_align,StyleConstants.ALIGN_CENTER);
StyleConstants.setFontFamily( char_style,"Serif");
StyleConstants.setFontSize(char_style,70);
StyleConstants.setForeground(char_style,Color.red);//為屬性對象指定值
textpane.setParagraphAttributes(center_align,true);//文本窗格設置文本的屬性
textpane.setCharacterAttributes(char_style,true);
getContentPane().add(scroll);
}
}
25-例子18
import javax.swing.*;import javax.swing.text.*;
import java.awt.*;
public class Example25_18 extends JApplet
{ JTextPane textpane;
MutableAttributeSet center_align,char_style_1,char_style_2;
public void init()
{ textpane=new JTextPane();//創建文本窗口
JScrollPane scroll=new
JScrollPane(textpane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Document mydocument=textpane.getDocument();//初始化一個文檔。
center_align=new SimpleAttributeSet();
char_style_1=new SimpleAttributeSet();
char_style_2=new SimpleAttributeSet();
StyleConstants.setAlignment(center_align,StyleConstants.ALIGN_CENTER);
StyleConstants.setFontFamily( char_style_1,"Courier");
StyleConstants.setFontSize(char_style_1,20);
StyleConstants.setForeground(char_style_1,Color.red);
StyleConstants.setFontFamily( char_style_2,"Serif");
StyleConstants.setFontSize(char_style_2,14);
StyleConstants.setForeground(char_style_2,Color.blue);
textpane.setParagraphAttributes(center_align,true);
textpane.setCharacterAttributes(char_style_1,true);
try{textpane.insertIcon(new ImageIcon("a.jpg"));
mydocument.insertString(mydocument.getLength(),
"Lovely Apple\n",char_style_1);
}
catch(BadLocationException e)
{}
textpane.setParagraphAttributes(center_align,true);
textpane.setCharacterAttributes(char_style_2,true);
try{mydocument.insertString(mydocument.getLength(),
"I Want It\n",char_style_2);
}
catch(BadLocationException e)
{}
getContentPane().add(scroll);
}
}
25-例子19
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;import java.io.*;
public class Example25_19 extends JApplet
{ JTextPane textpane;FileInputStream readfile;
public void init()
{ textpane=new JTextPane();//創建文本窗口
JScrollPane scroll=new JScrollPane(textpane);
try{ readfile=new FileInputStream("Example25_19.java");
}
catch(IOException ee){}
try{textpane.read(readfile,this);
}
catch(Exception e)
{}
getContentPane().add(scroll);
}
}
25-例子20
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyFileWin extends JFrame
{ JFileChooser chooser=new JFileChooser();
MyFileWin()
{ super("有文件選擇器的窗口");
chooser.showDialog(null,"打開");
setSize(200,200);
setVisible(true);
getContentPane().add(new Label("ok"));
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ System.exit(0);}} );
}
}
public class Example25_20
{public static void main(String args[])
{MyFileWin Win=new MyFileWin(); win.pack();
}
}
25-例子21
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FileWin extends JFrame implements ActionListener
{ JButton button; JTextArea text;JTextPane textpane;FileInputStream readfile;
JScrollPane scroll;Container con;
JFileChooser chooser=new JFileChooser();
FileWin()
{ super("有文件選擇器的窗口");
button=new JButton("打開文件選取器");
button.addActionListener(this);
textpane=new JTextPane();
scroll=new JScrollPane(textpane);
setSize(200,200);
setVisible(true);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ System.exit(0);}} );
con=getContentPane();con.add(button,BorderLayout.NORTH);
con.add(scroll,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button)
{String s;
int state=chooser.showOpenDialog(null);
File file=chooser.getSelectedFile();
if(file!=null&&state==JFileChooser.APPROVE_OPTION)
{ try{
readfile=new FileInputStream(file); //建立到文件的輸入流。
}
catch(IOException ee){}
try{ textpane.read(readfile,this);//從流中讀取數據。
}
catch(IOException e1){}
}
}
}
}
public class Example25_21
{public static void main(String args[])
{FileWin Win=new FileWin(); Win.pack();
}
}
25-例子21(補充)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FileWin extends JFrame
implements ActionListener
{ JButton button;
JScrollPane scroll;Container con;
JFileChooser chooser=new JFileChooser();
FileWin()
{ super("有文件選擇器的窗口");
button=new JButton("打開文件選取器");
button.addActionListener(this);
setSize(200,200);
setVisible(true);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ System.exit(0);}} );
con=getContentPane();con.add(button,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button)
{String s;
int state=chooser.showOpenDialog(null);
File file=chooser.getSelectedFile();
if(file!=null&&state==JFileChooser.APPROVE_OPTION)
{try{ Runtime ce=Runtime.getRuntime();
ce.exec(file.toString()); //把file用字符串表示
} //所有對象都能使用toString()。
catch(IOException ee){}
}
}
}
}
public class mytest
{public static void main(String args[])
{FileWin Win=new FileWin(); Win.pack();
}
}
25-例子22
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TimeWin extends JFrame implements ActionListener
{ static JTextArea text1,text2; Boy boy=new Boy();
JScrollPane scroll_1,scroll_2;Container con;
Timer time_1,time_2 ; //聲明2個計時器對象。
JSplitPane splitpane;
TimeWin()
{super("有計時器窗口");
time_1=new Timer(1000,this);//TimeWin對象做計時器的監視器。
time_2=new Timer(2000,boy);//Boy對象做計時器的監視器。
text1=new JTextArea(); text2=new JTextArea();
scroll_1=new JScrollPane(text1);
scroll_2=new JScrollPane(text2);
splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scroll_1,
scroll_2);
setSize(200,200);
setVisible(true);
con=getContentPane();con.add(splitpane);
time_1.start();time_2.start();//啟動計時器。
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ System.exit(0);}} );
}
public void actionPerformed(ActionEvent e)
{text1.append("歡迎光臨!"+"\n"); }
}
class Boy implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ TimeWin.text2.append("再見!"+"\n"); }
}
public class Example25_22
{public static void main(String args[])
{TimeWin Win=new TimeWin(); Win.pack();
}
}
25-例子23
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
class BarWin extends JFrame implements ActionListener
{ Timer time_1; int sum=0,i=1;
JProgressBar p_bar;Container con;
BarWin()
{super("窗口");
time_1=new Timer(1000,this);//TimeWin對象做計時器的監視器,每
//1000毫秒震鈴一次。
p_bar=new JProgressBar(0,55);
p_bar.setBackground(Color.white);
p_bar.setStringPainted(true);
setSize(200,200);
setVisible(true);
con=getContentPane();con.add(p_bar,BorderLayout.NORTH);
time_1.start();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ System.exit(0);}} );
}
public void actionPerformed(ActionEvent e)
{ sum=sum+i;
p_bar.setValue(sum);//吃掉sum/55
i=i+1;
if(sum>=55)
time_1.stop();
}
}
public class Example25_23
{public static void main(String args[])
{BarWin Win=new BarWin(); Win.pack();
}
}
25-例子24
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
public class Example25_24 extends JApplet implements ActionListener
{ static Timer time_1; static JButton button1,button2;int i=1;
JProgressBar p_bar;Container con;JLabel label;
public void init()
{time_1=new Timer(1000,this);
p_bar=new JProgressBar(0,33);
p_bar.setBackground(Color.white);
con=getContentPane();con.add(p_bar,BorderLayout.NORTH);
button1=new JButton("開始播放");button2=new JButton("停止播放");
p_bar.setStringPainted(true); p_bar.setString("正在播放第0張");
button1.addActionListener(new Boy()); button2.addActionListener(new Boy());
JPanel panel=new JPanel();
panel.add(button1); panel.add(button2); label=new JLabel();
con.add(label,BorderLayout.CENTER); con.add(p_bar,BorderLayout.NORTH);
con.add(panel,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{ label.setIcon(new ImageIcon("tom"+i+".jpg"));
p_bar.setValue(i);p_bar.setString("正在播放第"+i+"張,共33張");
i++;
if(i>33) {time_1.stop();i=1;}
}
}
class Boy implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ if(e.getSource()==Example25_24.button1)
Example25_24.time_1.start();
else if(e.getSource()==Example25_24.button2)
Example25_24.time_1.stop();
}
}
25-例子25
import javax.swing.*;
public class Example25_25 extends JApplet
{ JTable table;
public void init()
{ table=new JTable(4,6);
getContentPane().add(new JScrollPane(table));
}
}
25-例子26
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
public class Example25_26 extends JApplet implements ActionListener
{ JTable table;Object a[][];JPanel p1,p2;
Object name[]={"第1列","第2列"};
JButton button;JTextField text;
public void init()
{ a=new Object[2][2];
button=new JButton("確定");text=new JTextField(8);
p1=new JPanel();p2=new JPanel();
p1.setLayout(new GridLayout(2,1));
p1.add(new Label("輸入2階行列式的元素"));
p1.add(new Label("輸入或修改數據后,用鼠標點擊每個格,使數據生效"));
p2.add(button);p2.add(new JLabel("結果:"));p2.add(text);
table=new JTable(a,name);
button.addActionListener(this);
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
getContentPane().add(p1,BorderLayout.NORTH);
getContentPane().add(p2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button)
{ double d[][]=new double[2][2];double result;
for(int i=0;i<2;i++)
{for(int j=0;j<2;j++)
{d[i][j]=Double.valueOf(a[i][j].toString()).doubleValue();
}//將對象a[i][j]表示為字符串后,,再轉化為double型數值
}
result=d[1][1]*d[0][0]-d[0][1]*d[1][0];
text.setText(String.valueOf(result));
}
}
}
25-例子27
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.awt.*;
class ResultWin extends JFrame implements ActionListener
{ Object a[][];
Object columnName[]={"學號","姓名","出生日期","數學","物理","英語"};
JTable table;JButton button;
Container container;
String name,xuehao;Date date; int math,physics,english;
Connection con;Statement sql; ResultSet rs;
JProgressBar p_bar;
ResultWin()
{ super("數據查詢");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -