TAG:
我实际工作中的经验教训,在这里与大家共享, 请珍惜,因为有些问题,解决了看来是小问题,可是没解决的时候是大问题!这里很多问题,每个花费我几个星期!吐血奉献, chinaunix是一个大家,我有问题,总有人奉献,今天我也奉献一番。 空的字符串是””,不带空格 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兄弟还告诉了以下的方法:兄弟,你的这个方案太复杂了。你的目的是:glasspane显示,但不想处理事件(这时事件将自动传入它底下的组件) 故: 只分用:glasspaneObj.setEnabled(false);表明它不处理事件(注意:它的那些事件处理代码的注册要去掉(因为你现在在程序中已为它加上了add????listener()了)) 我已测试过了,是完全可以的。韩佳伟2003/06/03测试过,是成功的。 变量数组的定义: 下列数据第一句是长度只能为3,超过时系统将出错 // Object[][] data = { {" ", " ", " "},{" ", " ", " "},{" ", " ", " "}}; 可以如下定义 Object[][] data =new Object[100][3]; 父类的参数不能及时地传递到子类: 处理方法1: 1将父类中的参数尽可能早地传入子类 2在子类中在start()以后再调用带这个参数的方法 public void start( ) 父类 { shost= getDocumentBase().getHost(); Jhpportframe1.setnetlabel(shost); Jexhostipframe1.setnetlabel(shost); } public void start() 子类中用到shost变量值的方法,应该在start方法的时候或后面 { this.get_subnet_data_raw(); } 处理方法2:将子类放在父类的大括号中,直接用全局变量传递。 下面两句话的刷新速度取决于run的时间周期,每周期刷新一次: if(tabbedpane.getSelectedIndex()==2){secmanTabbedPane.hide();aaa=1;}else secmanTabbedPane.show(); if(tabbedpane.getSelectedIndex()==3){secmanTabbedPane.show();aaa=0;}else secmanTabbedPane.hide(); public void run(){ while(true) { repaint(); try {Thread.sleep(50);} catch (InterruptedException e) {} } } JTable subnettable在按钮等动作中得到新的数据后无法刷新数据: JTable subnettable在按钮等动作中得到新的数据后无法刷新数据,通过焦点移动等方式均不行,后来采用了reshape方法解决了这个问题,但是由于这个jtable在scrollpane组件上,在reshape方法中应该从0,0坐标开始,否则虽然图象在reshape时由于scrollpane的存在而会弹回原处,但是界面会产生闪烁. subnettable.reshape(0,0,300,200); 在构造时: JScrollPane scrollpane = new JScrollPane(subnettable); //将表格组件加到滚动条组件中 scrollpane.reshape(30,60,300,200); 设置组件的透明与否, 设置组件的使能与否 tabbedpane.setOpaque(true); //设置组件的透明与否 tabbedpane.setEnabled(false); //设置组件的使能与否 取得屏幕尺寸: Toolkit tk=Toolkit.getDefaultToolkit(); Dimension d=tk.getScreensize(); Int screenHeight=d.height; Int screenwidth=d.width; 如果是编译的时候出现:函数体没找到的错误,可能是因为在函数的声明时多加了分号 设置JToggleButton与ButtonGroup sysman_proc_allseljb=new JToggleButton("全选",false); sysman_proc_allcancjb=new JToggleButton("全不选",false); sysman_proc_partjb=new JToggleButton("分别选取",true); ButtonGroup sysman_proc_bg=new ButtonGroup() ; //建立一个组,但是这个组不能被加到容器中,sysman_proc_JPanel.add(sysman_proc_bg);是错的,他也没有必要被加到容器中,只需将组件加入即可,如 sysman_proc_JPanel.add(sysman_proc_allseljb); sysman_proc_JPanel.add(sysman_proc_allcancjb); sysman_proc_JPanel.add(sysman_proc_partjb); 这也就说明了ButtonGroup不是组件 (iwgh) |