例程中的一部分,运行错误 :Cannot make a static reference to the non-static method
如上.
MyComm类中,简单定义 了一个列举所有串口的方法.
package my_swt_prj;
import java.io.*;
import java.util.*;
/*commport pkg*/
import javax.comm.CommPort;/*支持串口,并口 的管理*/
import javax.comm.CommPortIdentifier;/*串口的设置和管理*/
import javax.comm.SerialPort;/*串口的读 写*/
//public class MyComm implements Runnable, SerialPortEventListener {
public class MyComm{
public void listPortChoices(){
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}
//portChoice.select(parameters.getPortName());/*select's menu,focus at portname's */
}
}
在Myframe中,声明 一个对象.
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Properties;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.Color;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
//inport comm pkg
import java.io.*;
import java.util.*;
public class MyJFrame extends JFrame {
private JPanel contentPane;
MyComm com1=new MyComm();
然后在下面的鼠标事件中调用 .
btnNewButton_4.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// btnNewButton.setText("Batton D was clicked");
textArea.append("button D was clicked by systom out\n");
//MyComm.
MyComm.listPortChoices();
}
});
不点鼠标,其他正常,点了button4,出现上面的报错,,,不能在static的方法里引用非static的方法或参数?
把方法改成.
public class MyComm{
public CommPortIdentifier portId;
public void listPortChoices(){
Enumeration en = CommPortIdentifier.getPortIdentifiers();
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}
//portChoice.select(parameters.getPortName());/*select's menu,focus at portname's */
}
}
不行,指点该 哪里修改?