0
请帮我在下面的代码人,我试图从SearchPort
类的方法返回arraylist元素(搜索端口)。然后这将用于相应的类通信中,其中将调用方法returnArray()
以提取用于Jcombox
选项的字符串。但我该如何去做呢?请帮忙。Java与ComboBox协会arraylist
public class SearchPort {
CommPortIdentifier portIdentifier;
ArrayList <String> portFound ;
public void listPorts() {
portFound = new ArrayList();
//Enumeration holds all port objects
Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) //while Enumeration contains more port objects
{
portIdentifier = portEnum.nextElement(); //switches through each port
portFound.add(portIdentifier.getName());
}
}
public String returnArray(){
listPorts();
for(int i = 0; i < portFound.size(); i++){
System.out.println(portFound.get(i));
}
return portFound;
}
public static void main(String[] args){
SearchPort run = new SearchPort();
run.listPorts();
}
}
public class Communication {
JLabel jLabel1;
JPanel jPanel1;
JComboBox Connections;
public Communication() {
JFrame commFrame = new JFrame("gec");
commFrame.pack();
commFrame.setVisible(true);
commFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
commFrame.setSize(300,300);
jLabel1 = new JLabel();
jPanel1 = new JPanel();
jLabel1.setText("GEC");
Font font = new Font("Calibri", Font.BOLD, 14);
SearchPort port = new SearchPort();
String [] portStrings = { port.returnArray()}; //add found ports into array
Connections = new JComboBox(portStrings);
Connections.addItemListener(null);
jLabel1.setFont(font);
jPanel1.add(jLabel1, BorderLayout.EAST);
jPanel1.add(Connections, BorderLayout.CENTER);
/*Add Contents to the Frame*/
commFrame.add(jPanel1);
}
public static void main(String args[]) {
Communication GUI = new Communication();
}
}//end class
public JComboBox(Object [] items)中的最后一行代码行,我如何实现这一点。我只是希望组合框选项是来自SearchPort类 – user1106130 2012-02-07 15:18:58
中的arraylist元素,其中1)'while(portEnum.hasMoreElements())',2)'for(int i = 0; i
mKorbel
2012-02-07 16:36:44
我仍然不确定,因为它们在两个不同的类中,一个示例将有助于感谢 – user1106130 2012-02-07 17:02:58