实际Java编程中的经验教训
2008-01-05 19:27:09 来源:WEB开发网核心提示:作者:cctech 我实际工作中的经验教训,在这里与大家共享,实际Java编程中的经验教训, 空的字符串是””,不带空格 outputStream 在打印时,打印的内容后面不要加\n,否则对方接受和判定不正确 G:\PROJECTION\IC2N\1c2nJava>javac Jsubnetframe.java
作者:cctech
我实际工作中的经验教训,在这里与大家共享,
空的字符串是””,不带空格
outputStream 在打印时,打印的内容后面不要加\n,否则对方接受和判定不正确
G:\PROJECTION\IC2N\1c2nJava>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:\PROJECTION\IC2N\1c2njava>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()));
}
}
jiangns3003兄弟还告诉了以下的方法:兄弟,你的这个方案太复杂了。
更多精彩
赞助商链接