我實際工作中的經驗教訓,在這裡與大家共享,
空的字符串是””,不帶空格
outputStream 在打印時,打印的內容後面不要加
,否則對方接受和判斷不正確
G:PROJECTIONIC2N1c2njava>javac Jsubnetframe.java
Jsubnetframe.java:22: Jsubnetframe should be declared abstract; it does not defi
ne valueChanged(javax.swing.event.ListSelectionEvent) in Jsubnetframe
public class Jsubnetframe extends JFrame implements ListSelectionListener {
^
Jsubnetframe.java:68: cannot resolve symbol
symbol : class TableModel
location: class Jsubnetframe
TableModel dataModel = new AbstractTableModel() {
^
Jsubnetframe.java:68: cannot resolve symbol
symbol : class AbstractTableModel
location: class Jsubnetframe
TableModel dataModel = new AbstractTableModel() {
^
Jsubnetframe.java:89: addActionListener(java.awt.event.ActionListener) in javax.
swing.AbstractButton cannot be applied to (Jsubnetframe)
cancelb.addActionListener(this);
^
Note: Jsubnetframe.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
4 errors
原因是沒有import javax.swing.table.*;
G:PROJECTIONIC2N1c2njava>javac Jhpportframe.java
Jhpportframe.java:27: Jhpportframe should be declared abstract; it does not defi
ne mousePressed(java.awt.event.MouseEvent) in Jhpportframe
public class Jhpportframe extends JFrame implements
^
Note: Jhpportframe.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
1 error
Jhpportframe should be declared abstract; it does not defi 原因是因為在這裡必須用MouseListener 的相關方法進行覆蓋,也就是說必須加入如下的方法:
public void mousePressed(MouseEvent evt) {//if (audio != null) audio.play();}
public void mouseEntered (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
鼠標左鍵和右件的事件獲取:
public void mousePressed(MouseEvent evt) {
Point point1=evt.getPoint();
int x,y;
int onmask = evt.BUTTON1_DOWN_MASK;//左鍵的ID號
if ((evt.getModifiersEx() & onmask ) == onmask) // 點擊左鍵時
{
}
else //點擊右鍵時
抽象類,不能進行初始化或者生成實例
hpportimg=new Image(getDocumentBase(),”aaa.gif”);
這句話是錯的,因為Image()是抽象類,不能進行初始化或者生成實例.,應該用:
hpportimg=getImage(“adf.gif”);
並且應該注意它不能放在構造函數中,而應該放在INIT()等方法中.
各種動作和事件的處理:
監視鍵盤的動作,用usertf.addKeyListener(this);它的接口軟件包是KeyListener,應該import java.awt.event.*; 必須重載的接口函數是
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
監視焦點的動作,用usertf.add FocusListener (this);它的接口軟件包是FocusListener應該import java.awt.event.*; 必須重載的接口函數是
public void focusLost(FocusEvent e) { }
public void focusGained(FocusEvent e) {}
監視鼠標的動作,用usertf.addMouseListener(this);它的接口軟件包是MouseyListener,應該import java.awt.event.*;
//必須增加MouseListener(this) 或component. MouseListener(this)
必須重載的接口函數是
public void mouseClicked(MouseEvent e){ }
public void mousePressed(MouseEvent evt) {}
public void mouseEntered (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
監視鼠標移動的動作,用usertf.addMouseListener(this);它的接口軟件包是MouseyMotionListener,應該import java.awt.event.*; 必須重載的接口函數是
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){ }
以上各種事件必須在組件上增加相應的偵聽,如
user.addKeyListener(this);
pass.addKeyListener(this);
user.addFocusListener(this);
pass.addFocusListener(this);
按鈕事件的執行在JAVA2中應該用下列函數,無須重載:
public void actionPerformed(ActionEvent ae) {
JButton source=(JButton)ae.getSource();
if(source==okb)
{ slkdfjalskdjflaksdjlfjads;flkjasld;kjfl;kasjdfl;k}
焦點事件:focusLost(),focusGain()
transferFocus()是將焦點傳遞到下一個組件
requestFocus()是將焦點傳遞到指定的組件,
if(e.getComponent()==user) user.transferFocus();
else if(e.getComponent()==pass) pass.transferFocus();
else if(e.getComponent()==ok) ok.transferFocus();
經過觀察發現,焦點可以在文本框上傳遞,但是在按鈕上好象沒法傳遞.
在表格中
subnettable.reshape(0,0,300,200); //一定要,用以刷新屏幕
JTable中的每一行可否設置不同的背景顏色???
可以的,使用自己的TableCellRenderer從TableModel中的value獲取顏色值.
請參見:
http://www2.gol.com/users/tame/swing/examples/
有關玻璃層:
1、 GLASSPANE不能設置大小,它將布滿整個工作區, //jtv.reshape(300,300,300,200); 沒用
2、 先建立一個JComponent 的組件,jtv=new Jtopview(jlp);其中jlp是新建母窗口的JLayeredPane
3、 將這個組建用setGlassPane(jtv)方法設置
4、 如果要求它透明,這一句不能少jtv.setOpaque(false);
5、 這一句是顯示的,也不能少jtv.setVisible(true);
6、 玻璃層下面層的組建獲取動作用redispatchMouseEvent(MouseEvent me)
這段代碼是我在英文網址上拿下來修改的,同時參照了<<精通JAVA2>>P437頁
public void redispatchMouseEvent(MouseEvent me,boolean repaint)
{
//能夠直接取到的組件,用廣義的component 就可以了
Point point=me.getPoint();
int eventID = me.getID();
Component component=jlp.getComponentAt(point);
Point componentPoint = SwingUtilities.convertPoint(
jlp,
point,
component);
boolean componentBOOL=component.contains(componentPoint );
if(componentBOOL){
//System.out.println("7777777777777777" component);
component.dispatchEvent(new MouseEvent(component,eventID,me.getWhen(),me.getModifiers(),
componentPoint.x,componentPoint.y,me.getClickCount(),me.isPopupTrigger()));
}
////不能夠直接取到的組件,用具體的組件名字
Point alarmtablePoint = SwingUtilities.convertPoint(
jlp,
point,
alarmtable);
boolean alarmtableBOOL= alarmtable.contains(alarmtablePoint );
if (alarmtableBOOL ) {
alarmtable.dispatchEvent(new MouseEvent(alarmtable,eventID,me.getWhen(),
me.getModifiers(),
alarmtablePoint.x,
alarmtablePoint.y,
me.getClickCount(),
me.isPopupTrigger()));
}
}