锘??xml version="1.0" encoding="utf-8" standalone="yes"?> ---- 鎴戜滑鐭ラ亾錛屽湪Java Applet涓嚭浜庡畨鍏ㄦц冭檻錛孉pplet鏄笉鍏佽瀵規枃浠惰繘琛屾搷浣滅殑錛屼笉浠呬笉鍏佽鍐欐枃浠訛紝鑰屼笖涓嶅厑璁歌鏂囦歡銆傚敖綆℃垜浠湪緙栧埗Applet鏃跺嵆浣夸嬌鐢ㄤ簡鏂囦歡鎿嶄綔鐨勮鍙ava涓嶄細鎶ラ敊錛屽湪寮鍙戝伐鍏鳳紙濡侰afe錛変腑璋冭瘯鏃朵篃鑳藉姝e父榪愯錛屼絾褰撴垜浠湪嫻忚鍣ㄤ腑榪愯榪欎釜Applet鏃舵祻瑙堝櫒灝變細鎶ラ敊銆備絾鏈夋椂鎴戜滑鐨勭‘瑕佽鍙栨枃浠朵腑鐨勫唴瀹癸紝姣斿瑕佸皢鏈嶅姟鍣?/a>涓殑.txt鏂囦歡鍐呭鍦ˋpplet涓樉紺哄嚭鏉ワ紝鏄笉鏄氨娌℃湁鍔炴硶浜嗗憿錛? ---- 涓嶏紒鏈夊姙娉曘傚喅紿嶅氨鏄垜浠笉瑕佸皢榪欎簺鏈嶅姟鍣ㄤ笂鐨勬枃浠朵綔涓烘櫘閫氭枃浠舵潵澶勭悊錛岃屾槸灝嗗畠浠綔涓虹綉緇滆祫婧愭潵鑾峰彇瀹冧滑鐨勫唴瀹廣傚湪Java涓彲鐢ㄤ簬鑾峰彇緗戠粶璧勬簮鐨勭被涓昏鏈変袱縐嶏紝涓鏄疷RL綾伙紝鍙︿竴涓槸URLConnection綾匯備袱涓被閮芥彁渚涗簡浠ュ瓧鑺傛祦鐨勬柟寮忚鍙栬祫婧愪俊鎭殑鏂規硶錛岃屼笖鍙互瀵硅祫婧愪俊鎭殑綾誨瀷浣滃嚭鍒ゆ柇錛屼互渚夸綔鐩稿簲鐨勫鐞嗐備笉鍚屼箣澶勬槸URLConnection綾誨彲鎻愪緵鐨勪俊鎭瘮URL綾昏澶氬緱澶氾紝瀹冮櫎浜嗗彲浠ヨ幏鍙栬祫婧愭暟鎹錛岃繕鍙互鎻愪緵璧勬簮闀垮害銆佽祫婧愬彂閫佹椂闂淬佽祫婧愭渶鏂版洿鏂版椂闂淬佽祫婧愮紪鐮併佽祫婧愮殑鏍囬絳夎澶氫俊鎭? ---- 浠ヤ笅鏄袱涓被鐨勫父鐢ㄦ柟娉曘? URL綾伙細 · URL(String, String, int, String) 鏋勯犳柟娉曪紝鍒涘緩涓涓寘鍚崗璁被鍨嬨佷富鏈哄悕銆?br />
绔彛鍙峰拰璺緞鐨刄RL瀵硅薄 · URL(String, String, String) 鏋勯犳柟娉曪紝鍒涘緩涓涓寘鍚崗璁被鍨嬨佷富鏈哄悕鍜岃礬寰?br />
鐨刄RL瀵硅薄錛屽叾涓鍙e彿涓虹己鐪佸?br />
· URL(String) 鏋勯犳柟娉曪紝鍒涘緩涓涓猆RL瀵硅薄錛屽弬鏁板皢鍗忚 銆佷富鏈哄悕銆佺鍙e彿鍜岃礬寰勭粍鍚堣搗鏉?br />
· URL(URL,String) 鏋勯犳柟娉曪紝鏍規嵁緇欏畾URL瀵硅薄涓庣浉瀵硅礬寰勫垱寤轟竴涓柊鐨刄RL瀵硅薄 · Object getContent( ) 媯绱RL鍐呭淇℃伅錛屽茍榪斿洖緇欏璞?br />
· InputStream openStream( ) 浠庤祫婧愬榪斿洖涓涓緭鍏ユ祦 · URLConnection openConnection( ) 鐢熸垚涓涓猆RLConnection瀵硅薄 URLConnection綾伙細 · protected URLConnection(URL) 鏋勯犳柟娉曪紝鍒涘緩涓涓拡瀵規寚瀹歎RL瀵硅薄鐨刄RLConnection綾?br />
· Object getContent( ) 榪斿洖URL瀵硅薄鎵瀵瑰簲鐨勫唴瀹?br />
· InputStream getInputStream( ) 鑾峰彇浠庡璞′腑璇誨彇鐨勫瓧鑺傛祦 · Protected static String guessContentTypeFromStream(InputStream is) 鏍規嵁杈撳叆嫻佺寽嫻嬪唴瀹圭殑綾誨瀷 ---- 涓嬮潰浠ヨ鍙栨湇鍔″櫒涓婄殑.txt鏂囦歡鍐呭涓轟緥璇存槑濡備綍鍦ˋpplet涓鍙栨枃浠躲傝鏈嶅姟鍣ㄧ殑IP鍦板潃涓?02.114.1.16錛?txt鏂囦歡鐨勮礬寰勪負/file/sample.txt銆備互涓嬫槸璇誨彇sample.txt鍐呭鐨凙pplet鐨勬簮浠g爜銆? //getfile.html
//getFile.java 浠ヤ笂JAVA紼嬪簭鍦ㄤ袱縐嶇郴緇熶腑璋冭瘯鍧囬氳繃錛屼袱縐嶇郴緇熺殑閰嶇疆鍒嗗埆涓猴細 錛?錛?鏈嶅姟鍣細Digital Unix + Oracle Webserver3.0 嫻忚鍣細Netscape4.0.5鎴朓E4.0 錛?錛?鏈嶅姟鍣細Windows98 + Pws 嫻忚鍣細Netscape4.0.5鎴朓E4.0 urlc.setDoOutput(
private static ThreadLocal codeBase = new ThreadLocal();
codeBase.set(this.getCodeBase());
public static Object getUrl(){
return codeBase.get();
}
鍦ㄥ叾浠栫殑form涓紝鍙互鐢╣etUrl榪欎釜static method璋冪敤寰楀埌main frame鐨刢odebase
URL codeBase = (URL)MainFrame.getUrl();
]]>
< HTML >
< HEAD >
< TITLE >璇誨彇鏂囦歡鐨凙pplet< /TITLE >
< /HEAD >
< BODY >
榪欐槸鏈嶅姟鍣ㄤ笂TXT鏂囦歡鐨勫唴瀹?/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;
}
}
鐢╞ufferedreader鐨勬柟娉?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()));娉ㄦ剰榪欓噷瑕佹帴鏀秊sp錛弒ervlet鐨剅esponse錛?鍚﹀垯瀹冧笉榪愯
// 鏍規嵁鏃墮棿寰楁枃浠跺悕
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+fileame;//鐢熸垚鐨刪tml鏂囦歡淇濆瓨璺緞
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//寤虹珛鏂囦歡杈撳嚭嫻?/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;
}
]]>
瑕佷嬌榪欎釜浜嬩歡鍙彂鐢熶竴嬈★紝鍙互榪欐牱錛?br />
public void itemStateChanged(ItemEvent ie){
if(ie.getStateChanged() == ItemEvent.SELECTED){
// here do whatever you were going to do when the item was selected
}
}
]]>
瀹氫箟涓涓猚ell鐨凧button娓叉煋瀵硅薄
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;
}
//榪欓噷鏄偣鍑籦utton鎵ц鐨勬搷浣?/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涓姞鍏ヤ粬浠? 鍋囪娣誨姞鍒皌able涓悕涓猴紓button錛傜殑鍒?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
浜嬩歡鍦ㄦ枃瀛楁敼鍙樺悗瑙﹀彂
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淇℃伅
鐢ㄦ潵媯楠宼ext changed鍙互鐢╧ey-released events. 鍥犱負瀹冭兘澶熼氳繃getText錛堬級寰楀埌textfield涓敼鍙樺悗鐨則ext. 鑰?nbsp;key-pressed event, key-typed event 寰楀埌鐨勬槸鏀瑰彉鍓嶇殑鏂囧瓧
]]>
鍓嶈呮槸鍦ㄥ悓涓涓綅緗畃ress鍜宺elease鎵嶈Е鍙戜簨浠訛紝鎵ц鎿嶄綔. 濡傛灉鍦ㄧ敤鎴蜂竴涓綅緗寜涓嬮紶鏍囧悗鏀瑰彉浜嗕富鎰忥紝鍒欑Щ鍔ㄥ埌鍙︿竴涓綅緗紙鎸夐挳澶栭潰錛夋斁寮灝變細鍙栨秷鎿嶄綔錛堜笉鎵ц鎿嶄綔錛?
鑰屽悗鑰呭彧鏄Е鍙憄ress浜嬩歡, 涔熷氨鏄鎸変笅榧犳爣灝辨墽琛屾搷浣?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.
鐢ㄦ潵媯楠宼ext changed鍙互鐢╧ey-released events. 鍥犱負瀹冭兘澶熼氳繃getText錛堬級寰楀埌textfield涓敼鍙樺悗鐨則ext. 鑰?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.
]]>
榧犳爣鍙屽嚮浜嬩歡
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
榪欓噷琛ュ厖鐨勪富瑕佹槸model鐨勪嬌鐢紝鍘昏嚜瀹氫箟table鐨刦ormat銆?br />
鍦―efaultTableModel 鍩虹涓婃敼榪沵odel銆?鍦ㄥ畾涔塶ew DefaultTableModel鏃朵慨鏀筼verride瀹冪殑鍐呴儴method
閮ㄥ垎浠g爜錛?br />
銆銆InputDialog銆---銆鎻愮ず杈撳叆鏂囨湰
銆銆MessageDialog銆---銆鏄劇ず淇℃伅
銆銆OptionDialog銆-錛嶃緇勫悎鍏跺畠涓変釜瀵硅瘽妗嗙被鍨嬨?/p>
銆銆榪欏洓涓璇濇鍙互閲囩敤showXXXDialog()鏉ユ樉紺猴紝濡俿howConfirmDialog()鏄劇ず紜瀵硅瘽妗嗐乻howInputDialog()鏄劇ず杈撳叆鏂囨湰瀵硅瘽妗嗐乻howMessageDialog()鏄劇ず淇℃伅瀵硅瘽妗嗐乻howOptionDialog()鏄劇ず閫夋嫨鎬х殑瀵硅瘽妗嗐傚畠浠墍浣跨敤鐨勫弬鏁拌鏄庡涓嬶細
銆銆鈶?ParentComponent錛氭寚紺哄璇濇鐨勭埗紿楀彛瀵硅薄錛屼竴鑸負褰撳墠紿楀彛銆備篃鍙互涓簄ull鍗抽噰鐢ㄧ己鐪佺殑Frame浣滀負鐖剁獥鍙o紝姝ゆ椂瀵硅瘽妗嗗皢璁劇疆鍦ㄥ睆騫曠殑姝d腑銆?/p>
銆銆鈶?message錛氭寚紺鴻鍦ㄥ璇濇鍐呮樉紺虹殑鎻忚堪鎬х殑鏂囧瓧
銆銆鈶?String title錛氭爣棰樻潯鏂囧瓧涓層?/p>
銆銆鈶?Component錛氬湪瀵硅瘽妗嗗唴瑕佹樉紺虹殑緇勪歡錛堝鎸夐挳錛?/p>
銆銆鈶?Icon錛氬湪瀵硅瘽妗嗗唴瑕佹樉紺虹殑鍥炬爣
銆銆鈶?messageType錛氫竴鑸彲浠ヤ負濡備笅鐨勫糆RROR_MESSAGE銆両NFORMATION_MESSAGE銆乄ARNING_MESSAGE銆丵UESTION_MESSAGE銆丳LAIN_MESSAGE銆?/p>
銆銆鈶?optionType錛氬畠鍐沖畾鍦ㄥ璇濇鐨勫簳閮ㄦ墍瑕佹樉紺虹殑鎸夐挳閫夐」銆備竴鑸彲浠ヤ負DEFAULT_OPTION銆乊ES_NO_OPTION銆乊ES_NO_CANCEL_OPTION銆丱K_CANCEL_OPTION銆?/p>
銆銆浣跨敤瀹炰緥錛?/p>
銆銆(1) 鏄劇ずMessageDialog
銆銆(2) 鏄劇ずConfirmDialog
銆銆(3) 鏄劇ずOptionDialog錛氳縐嶅璇濇鍙互鐢辯敤鎴瘋嚜宸辨潵璁劇疆鍚勪釜鎸夐挳鐨勪釜鏁板茍榪斿洖鐢ㄦ埛鐐瑰嚮鍚勪釜鎸夐挳鐨勫簭鍙鳳紙浠?寮濮嬭鏁幫級
銆銆(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鎴朶r錛屽垯闇瑕佸畾涔夎嚜宸辯殑Swing鎺т歡銆?/p>