簡體   English   中英

無法使用Java代碼從組合框中獲取所選值

[英]Unable to get the selected value from Combo box using Java code

當我嘗試從Select事件中的組合框中獲取選定值時,它不返回任何值。

ProcessFile1()
{

        Process=new Button("Process File");
        Process.setBackground(new Color(164,108,153));
        File_Name=new Label("Enter FileName");
        TxtFileName = new TextField(20);
        String FileName =new String(TxtFileName.getText().trim());
        setSize(600,600);
        setLocation(400,100);
        addWindowListener(new ProcessFile1.WindowEventHandler());
        setLayout(new GridLayout(8,1));
        Panel p=new Panel();
        p.add(File_Name);p.add(TxtFileName);add(p);
        String fileName= File_Name.getText().trim();
        Panel panel = new Panel();
        panel.add(new JLabel("Please make a selection:"));
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement("Please Select One Application");
        model.addElement("GPS");
        model.addElement("IBS");
        model.addElement("BVT Tool");
        JComboBox File_Combo = new JComboBox(model);
        File_Combo.setBackground(new Color(181,81,129));
        panel.add(File_Combo);
        add(panel);
        System.out.println(value);
        Panel p1= new Panel();
        add(p1);
        p1.add(Process);
        Process.addActionListener(this);
        File_Combo.addActionListener(this);
        setVisible(true);
}

public void actionPerformed(ActionEvent ae) 
  {
           String name; 
           System.out.println("Inside actionevent");
           if(ae.getSource().equals(Process))
            {
                System.out.println("Working properly");
                ItemSelectable is;
                is = (ItemSelectable)ae.getSource();
                name = selectedString(is).trim();
                System.out.println(name);
               switch (name) {
                   case "IBS":
                       System.out.println("Inside IBS");
                       /*try
                       {
                       String[] command = new String[] {"mv /bgw/feeds/ibs/FileName /bgw/feeds/ibs/incoming/"};
                       Runtime.getRuntime().exec("command");
                       }catch(Exception e)
                       {
                       System.out.println("execption is :"+ e);
                       e.printStackTrace();
                       }*/break;
                   case "BVT Tool":
                       System.out.println("Inside BVT Tool");
                       /*try
                       {
                       String[] command1 = new String[] {"mv /bgw/feeds/ibs/FileName /bgw/feeds/ibs/incoming/"};
                       Runtime.getRuntime().exec("command1");
                       }catch(Exception e)
                       {
                       System.out.println("execption is :"+ e);
                       e.printStackTrace();
                       }*/break;
                   default:
                       System.out.println("Inside GPS");
                       try
                       {
                           String[] command2 = new String[] {"mv /bgw/feeds/ibs/$File_Name /bgw/feeds/ibs/incoming/"};
                           Runtime.getRuntime().exec(command2);
                       }catch(Exception e)
                       {
                           System.out.println("execption is :"+ e);
                           e.printStackTrace();
                       }
                       break;
               }
            }
        }

但是單擊組合框無法從組合框中獲取所選值。 請幫助我找到我失蹤的地方以獲得所選值。

您應該在組合框上使用ItemListener而不是動作偵聽器

JComboBox c = new JComboBox ();
c.addItem ("Up");
c.addItem ("Down");
c.addItem ("Strange");
c.addItem ("Charm");
c.addItem ("Top");
c.addItem ("Bottom");
c.addItemListener (this);

/** Get the combobox item events here. **/
public void itemStateChanged (ItemEvent e) {
  String command = e.getItem ().toString ();
  if (command.equals ("Charm") )
    System.out.println(command);;

} 

actionPerformed方法中,您只處理ActionEvent使用Process的源執行的操作。 您可以在自己的下方添加另一個條件:

public void actionPerformed(ActionEvent ae) {
        if(ae.getSource().equals(Process)){
            ...
        } else if (ae.getSource().equals(File_Combo){
            ...
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM