??xml version="1.0" encoding="utf-8" standalone="yes"?> ---- 我们知道Q在Java Applet中出于安全性考虑QApplet是不允许Ҏ件进行操作的Q不仅不允许写文Ӟ而且不允许读文g。尽我们在~制Applet时即使用了文g操作的语句Java不会报错Q在开发工P如CafeQ中调试时也能够正常q行Q但当我们在览器中q行q个Applet时浏览器׃报错。但有时我们的确要读取文件中的内容,比如要将服务?/a>中的.txt文g内容在Applet中显C出来,是不是就没有办法了呢Q? ---- 不!有办法。决H就是我们不要将q些服务器上的文件作为普通文件来处理Q而是它们作为网l资源来获取它们的内宏V在Java中可用于获取|络资源的类主要有两U,一是URLc,另一个是URLConnectioncR两个类都提供了以字节流的方式读取资源信息的ҎQ而且可以对资源信息的cd作出判断Q以便作相应的处理。不同之处是URLConnectioncd提供的信息比URLc要多得多,它除了可以获取资源数据外Q还可以提供资源长度、资源发送时间、资源最新更新时间、资源编码、资源的标题{许多信息? ---- 以下是两个类的常用方法? URLc: · URL(String, String, int, String) 构造方法,创徏一个包含协议类型、主机名?br />
端口号和路径的URL对象 · URL(String, String, String) 构造方法,创徏一个包含协议类型、主机名和\?br />
的URL对象Q其中端口号为缺省?br />
· URL(String) 构造方法,创徏一个URL对象Q参数将协议 、主机名、端口号和\径组合v?br />
· URL(URL,String) 构造方法,Ҏl定URL对象与相对\径创Z个新的URL对象 · Object getContent( ) 索URL内容信息Qƈq回l对?br />
· InputStream openStream( ) 从资源处q回一个输入流 · URLConnection openConnection( ) 生成一个URLConnection对象 URLConnectionc: · protected URLConnection(URL) 构造方法,创徏一个针Ҏ定URL对象的URLConnectionc?br />
· Object getContent( ) q回URL对象所对应的内?br />
· InputStream getInputStream( ) 获取从对象中d的字节流 · Protected static String guessContentTypeFromStream(InputStream is) Ҏ输入猜内容的cd ---- 下面以读取服务器上的.txt文g内容Z说明如何在Applet中读取文件。设服务器的IP地址?02.114.1.16Q?txt文g的\径ؓ/file/sample.txt。以下是dsample.txt内容的Applet的源代码? //getfile.html
//getFile.java 以上JAVAE序在两U系l中调试均通过Q两U系l的配置分别为: Q?Q?服务器:Digital Unix + Oracle Webserver3.0 览器:Netscape4.0.5或IE4.0 Q?Q?服务器:Windows98 + Pws 览器:Netscape4.0.5或IE4.0 urlc.setDoOutput(
private static ThreadLocal codeBase = new ThreadLocal();
codeBase.set(this.getCodeBase());
public static Object getUrl(){
return codeBase.get();
}
在其他的form中,可以用getUrlq个static method调用得到main frame的codebase
URL codeBase = (URL)MainFrame.getUrl();
]]>
< HTML >
< HEAD >
< TITLE >d文g的Applet< /TITLE >
< /HEAD >
< BODY >
q是服务器上TXT文g的内?/span>< BR >
< Applet code="getFile.class" width=200 height=100 >
< /Applet >
< /BODY >
< /HTML >
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
public class getFile extends Applet
{
String info;
public void init()
{
URL url;
URLConnection urlc;
resize(200,100);
setBackground(Color.white);
try{
url = new URL("http://202.114.1.16/file/sample.txt");
urlc = url.openConnection();
urlc.connect();
info = getInfo(urlc);
}catch(MalformedURLException mfe){
System.out.println("URL form error!");
}catch(IOException ioe){
System.out.println("IO Exception!");
}
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString(info,50,50);
}
public String getInfo(URLConnection urlc)
{
String txt = new String();
InputStream is;
int i;
try{
is = urlc.getInputStream();
i = is.read();
while(i != -1){
txt = txt + (char)i;
i = is.read();
}
is.close();
}catch(IOException ioe){
System.out.println("IO Exception!");
txt = new String("File read failed!");
}
return txt;
}
}
用bufferedreader的方?br />
//create url to the file on server
URL url = new URL(ma.getCodeBase(),"filename");
URLConnection urlc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String line=null;
if( (line = in.readLine()) != null )
{
System.out.printv(line);
}
in.close();
写入
//create url to the file on server
URL url = new URL(config.getCodeBase(),"servlet/jsp name");
URLConnection urlc = url.openConnection();
PrintStream stream = new PrintStream( urlc.getOutputStream() );
stream.println("param name="+"something write to file");
BufferedReader in = new BufferedReader( new InputStreamReader( urlc.getInputStream()));注意q里要接收jspQservlet的responseQ?否则它不q行
// Ҏ旉得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+fileame;//生成的html文g保存路径
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文g输出?/span>
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
]]>import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.text.JTextComponent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
public class SelectableTree extends JFrame
implements TreeSelectionListener,ChangeListener
{
protected Component makeJTreePanel(JTree tree)
{
JPanel panel = new JPanel(false);
panel.setLayout(new GridLayout(1, 1));
panel.add(new JScrollPane(tree));
return panel;
}
protected JTree makeJTree(String text)
{
DefaultMutableTreeNode root
= new DefaultMutableTreeNode(text);
for(int i = 0; i < 4; i++)
{
DefaultMutableTreeNode child
= new DefaultMutableTreeNode(text + i);
for(int j = 0; j < 4; j++)
child.add(new DefaultMutableTreeNode(text + i + j));
root.add(child);
}
JTree tree = new JTree(root);
tree.addTreeSelectionListener(this);
return tree;
}
public void valueChanged(TreeSelectionEvent event)
{
String temp = event.getPath().toString();
for(StringTokenizer token = new StringTokenizer(temp, ","); token.hasMoreTokens();)
temp = token.nextToken();
textArea.setText("Current Selection: " + temp.substring(0, temp.length() - 1));
}
/**Sole method of ChangeListener*/
public void stateChanged(ChangeEvent e)
{
DefaultSingleSelectionModel dSM
= (DefaultSingleSelectionModel)e.getSource();
int index = dSM.getSelectedIndex();
String tabName = tabbedPane.getTitleAt(index);
/** I imagine you will do more than this*/
if(textArea != null)
textArea.setText("Tab Selected: " + tabName);
}
/****************************************/
public SelectableTree()
{
super("ECHO Service Demo");
//WindowUtilities.setNativeLookAndFeel();
//addWindowListener(new ExitListener());
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container content = getContentPane();
tabbedPane = new JTabbedPane();
/**add ChangeListener to Model*/
tabbedPane.getModel().addChangeListener(this);
/*******************************************/
Component panel1 = makeJTreePanel(makeJTree("Taxonomy1"));
tabbedPane.addTab("Taxonomy1", null, panel1, null);
tabbedPane.setSelectedIndex(0);
Component panel2 = makeJTreePanel(makeJTree("Taxonomy2"));
tabbedPane.addTab("Taxonomy2", null, panel2, null);
Component panel3 = makeJTreePanel(makeJTree("Taxonomy3"));
tabbedPane.addTab("Taxonomy3", null, panel3, null);
Component panel4 = makeJTreePanel(makeJTree("Taxonomy4"));
tabbedPane.addTab("Taxonomy4", null, panel4, null);
content.add(tabbedPane, "Center");
textArea = new JTextArea("Services of Current Category: NONE");
content.add(textArea, "South");
setSize(350, 375);
setVisible(true);
}
public static void main(String args[])
{
new SelectableTree().setVisible(true);
}
private JTabbedPane tabbedPane;
private JTextArea textArea;
}
]]>
要ɘq个事g只发生一ơ,可以q样Q?br />
public void itemStateChanged(ItemEvent ie){
if(ie.getStateChanged() == ItemEvent.SELECTED){
// here do whatever you were going to do when the item was selected
}
}
]]>
定义一个cell的Jbutton渲染对象
class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}
setText((value == null) ? "" : value.toString());
return this;
}
}
定义button cell editor
class ButtonEditor extends DefaultCellEditor {
protected JButton button;
private String label;
private boolean isPushed;
private String selectId;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (isSelected) {
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
} else {
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
}
label = (value == null) ? "" : value.toString();
button.setText(label);
//get the value of the first cell in this selected row
selectId = table.getValueAt(row, 0).toString();
isPushed = true;
return button;
}
//q里是点击button执行的操?/span>
public Object getCellEditorValue() {
if (isPushed) {
JOptionPane.showMessageDialog(null, "The first of this row is"+selectId, "", JOptionPane.ERROR_MESSAGE);
}
isPushed = false;
return new String(label);
}
public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
最后在table中加入他? 假设d到table中名为"buttonQ的?br />
table.getColumn("Button").setCellRenderer(new ButtonRenderer());
table.getColumn("Button").setCellEditor( new ButtonEditor(new JCheckBox()));
]]>
//if enable than do some operation
//if disable do nothing
if(this.jButton.isEnabled()){
//if ture to do these
}//false do nothing
]]>
void
caretPositionChanged(InputMethodEvent event)
This method is called when the cursor position within the text is changed.
void
inputMethodTextChanged(InputMethodEvent event)
This method is called when the text is changed.
inputMethodTextChanged
事g在文字改变后触发
The text field fires three events: a key-pressed event, a key-typed event, and a key-released event.
Note :
the key-typed event doesn't have key code information. 含有key charactere信息
key-pressed and key-released events don't have key character information. 含有key code信息
用来验text changed可以用key-released events. 因ؓ它能够通过getTextQ)得到textfield中改变后的text. ?nbsp;key-pressed event, key-typed event 得到的是改变前的文字
]]>
前者是在同一个位|press和release才触发事Ӟ执行操作. 如果在用户一个位|按下鼠标后改变了主意,则移动到另一个位|(按钮外面Q放开׃取消操作Q不执行操作Q?
而后者只是触发press事g, 也就是说按下鼠标执行操?br />
2.
The text field fires three events: a key-pressed event, a key-typed event, and a key-released event.
Note :
the key-typed event doesn't have key code information.
key-pressed and key-released events don't have key character information.
用来验text changed可以用key-released events. 因ؓ它能够通过getTextQ)得到textfield中改变后的text. ?nbsp;key-pressed event, key-typed event 得到的是改变前的文字
The text field fires two events: a key-pressed and a key-released. The text field doesn't fire a key-typed event because Shift, by itself, doesn't correspond to any character.
You'll see the following events, although perhaps not in this order: key-pressed (Shift), key-pressed (A), key typed ('A'), key-released (A), key-released (Shift). Note that Shift is listed as the modifier key for the key-typed and key-pressed events.
You should see the following events: key-pressed (Caps Lock), key-pressed (A), key typed ('A'), key-released (A). Note that Caps Lock is not listed as a modifier key.
]]>
鼠标双击事g
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
q里补充的主要是model的用,去自定义table的format?br />
在DefaultTableModel 基础上改qmodel?在定义new DefaultTableModel时修改override它的内部method
部分代码Q?br />
InputDialog --- 提示输入文本
MessageDialog --- 昄信息
OptionDialog -Q l合其它三个对话框类型?/p>
q四个对话框可以采用showXXXDialog()来显C,如showConfirmDialog()昄认对话框、showInputDialog()昄输入文本对话框、showMessageDialog()昄信息对话框、showOptionDialog()昄选择性的对话框。它们所使用的参数说明如下:
?ParentComponentQ指C对话框的父H口对象Q一般ؓ当前H口。也可以为null即采用缺省的Frame作ؓ父窗口,此时对话框将讄在屏q的正中?/p>
?messageQ指C在对话框内显C的描述性的文字
?String titleQ标题条文字丌Ӏ?/p>
?ComponentQ在对话框内要显C的lgQ如按钮Q?/p>
?IconQ在对话框内要显C的图标
?messageTypeQ一般可以ؓ如下的值ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE、PLAIN_MESSAGE?/p>
?optionTypeQ它军_在对话框的底部所要显C的按钮选项。一般可以ؓDEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION、OK_CANCEL_OPTION?/p>
使用实例Q?/p>
(1) 昄MessageDialog
(2) 昄ConfirmDialog
(3) 昄OptionDialogQ该U对话框可以q戯己来讄各个按钮的个数ƈq回用户点击各个按钮的序P?开始计敎ͼ
(4) 昄InputDialog 以便让用戯行输?/p>
(5) 昄InputDialog 以便让用戯行选择地输?/p>
String htmlLabel = "<html><sup>HTML</sup> <sub><em>Label</em></sub><br>" +
"<font color=\"#FF0080\"><u>Multi-line</u></font>";
JLabel label = new JLabel(htmlLabel);
如果用\n或\rQ则需要定义自qSwing控g?/p>