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