2016-03-06 65 views
-1

enter code here嗨我是新的Java開發人員我想將JComboBox放入公共類中以獲取選定的項目,但Java無法識別它,例如我想要執行此操作。公共類中的JComboBox

myComboBox.getSelectedItem().toString() 

當我嘗試把JTextField它工作正常。

對不起,我表達我不會講英語非常好

那是我的代碼

package guarderia; 

import java.awt.BorderLayout; 

public class enfermedades extends JFrame { 

    private JPanel contentPane; 
    private JTextField txtid; 
    private JTextField textField_1; 
    private JTextField txtnom; 
    private static JTable table; 



    //VARIABLES PARA CONEXION A MYSQL 
    static Connection con; 
    static CallableStatement ps; 
    static ResultSet rs; 
    static DefaultTableModel tm; 
    static Statement sql; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        enfermedades frame = new enfermedades(); 
        frame.setVisible(true); 
        llenar(); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public enfermedades() { 


     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 1211, 663); 
     contentPane = new JPanel(); 
     contentPane.setBackground(new Color(255, 255, 255)); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     JPanel panel = new JPanel(); 
     panel.setLayout(null); 
     panel.setBackground(new Color(0, 0, 0)); 
     panel.setBounds(0, 0, 1624, 53); 
     contentPane.add(panel); 

     JLabel lblEnfermedades = new JLabel("ENFERMEDADES"); 
     lblEnfermedades.setForeground(Color.WHITE); 
     lblEnfermedades.setFont(new Font("Dialog", Font.BOLD, 19)); 
     lblEnfermedades.setBounds(12, 12, 231, 41); 
     panel.add(lblEnfermedades); 

     JPanel panel_1 = new JPanel(); 
     panel_1.setLayout(null); 
     panel_1.setBounds(10, 102, 426, 510); 
     contentPane.add(panel_1); 

     JLabel lblIdNio = new JLabel("ID Niño"); 
     lblIdNio.setBounds(12, 30, 70, 15); 
     panel_1.add(lblIdNio); 

     txtid = new JTextField(); 
     txtid.addKeyListener(new KeyAdapter() { 
      @Override 
      public void keyTyped(KeyEvent e) { 
       int c = e.getKeyChar(); 
       if (c < '0' || c > '9') e.consume(); 
        } 

      @Override 
      public void keyReleased(KeyEvent e) { 
       con= (Connection) conexion.conectar(); 
       try { 
        if (txtid.getText().equals("")) 
        { 
         txtnom.setText(""); 
        } 
        ps = (CallableStatement) con.prepareCall("{call select_nin (?) }"); 
        ps.setLong(1, Integer.parseInt(txtid.getText())); 



        ResultSet res = ps.executeQuery(); 


        if (res.next()) 
        { 
        String tipo = res.getString("Nombre"); 
        txtnom.setText(tipo); 
        llenar(); 
        } 
        else 
        { 
         txtnom.setText(null); 

        } 
       } catch (SQLException e1) { 
        JOptionPane.showMessageDialog(null, e1); 
        } 





      } 


     }); 
     txtid.setColumns(10); 
     txtid.setBounds(120, 28, 146, 19); 
     panel_1.add(txtid); 



     JLabel cbi = new JLabel("Enfermedad"); 
     cbi.setBounds(12, 95, 194, 15); 
     panel_1.add(cbi); 

     JLabel lblTratamiento = new JLabel("Tratamiento"); 
     lblTratamiento.setBounds(12, 130, 194, 15); 
     panel_1.add(lblTratamiento); 

     JButton btnNueva = new JButton("Nueva"); 
     btnNueva.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 


      } 
     }); 
     btnNueva.setBounds(282, 90, 88, 25); 
     panel_1.add(btnNueva); 

     JTextPane textPane = new JTextPane(); 
     textPane.setBounds(12, 157, 402, 118); 
     panel_1.add(textPane); 

     JLabel lblFechaDeteccion = new JLabel("Fecha Deteccion"); 
     lblFechaDeteccion.setBounds(12, 299, 194, 15); 
     panel_1.add(lblFechaDeteccion); 

     textField_1 = new JTextField(); 
     textField_1.setColumns(10); 
     textField_1.setBounds(135, 297, 95, 19); 
     panel_1.add(textField_1); 

     JLabel lblNombre = new JLabel("Nombre"); 
     lblNombre.setBounds(12, 61, 70, 15); 
     panel_1.add(lblNombre); 

     txtnom = new JTextField(); 
     txtnom.setEditable(false); 
     txtnom.setColumns(10); 
     txtnom.setBounds(120, 56, 294, 19); 
     panel_1.add(txtnom); 

     JLabel lblMedidadRecomendadas = new JLabel("Medidas recomendadas"); 
     lblMedidadRecomendadas.setBounds(12, 330, 194, 15); 
     panel_1.add(lblMedidadRecomendadas); 

     JTextPane textPane_1 = new JTextPane(); 
     textPane_1.setBounds(12, 355, 402, 93); 
     panel_1.add(textPane_1); 

     JButton btnGuardar = new JButton("Guardar"); 
     btnGuardar.setBounds(297, 470, 117, 25); 
     panel_1.add(btnGuardar); 

     JComboBox cbx = new JComboBox(); 
     cbx.setBounds(120, 90, 146, 24); 
     panel_1.add(cbx); 

     JPanel panel_2 = new JPanel(); 
     panel_2.setLayout(null); 
     panel_2.setBackground(new Color(220, 20, 60)); 
     panel_2.setBounds(10, 65, 426, 32); 
     contentPane.add(panel_2); 

     JLabel lblRegistrarEnfermedad = new JLabel("Registrar enfermedad"); 
     lblRegistrarEnfermedad.setForeground(Color.WHITE); 
     lblRegistrarEnfermedad.setFont(new Font("Dialog", Font.BOLD, 19)); 
     lblRegistrarEnfermedad.setBounds(12, 5, 401, 23); 
     panel_2.add(lblRegistrarEnfermedad); 

     JScrollPane scrollPane = new JScrollPane(); 
     scrollPane.setBounds(448, 102, 751, 510); 
     contentPane.add(scrollPane); 

     table = new JTable(); 
     scrollPane.setViewportView(table); 

     JPanel panel_3 = new JPanel(); 
     panel_3.setLayout(null); 
     panel_3.setBackground(new Color(128, 0, 128)); 
     panel_3.setBounds(448, 65, 751, 32); 
     contentPane.add(panel_3); 

     JLabel lblEnfermedades_1 = new JLabel("Enfermedades"); 
     lblEnfermedades_1.setForeground(Color.WHITE); 
     lblEnfermedades_1.setFont(new Font("Dialog", Font.BOLD, 19)); 
     lblEnfermedades_1.setBounds(12, 5, 401, 23); 
     panel_3.add(lblEnfermedades_1); 
    } 



    public static void llenar() 
    { 
     try 
     { 

      con = (Connection) conexion.conectar(); 
      String[]titulos={"ID", "ENFERMEDAD", "DESCRIPCION", "RIESGO CONTAGIO", "TIPO"}; 
      String cmd = "select * from enfermedades"; 
      tm = new DefaultTableModel(null,titulos); 
      sql = (Statement) con.createStatement(); 
      ResultSet rs = sql.executeQuery(cmd); 
      String[]fila = new String[5]; 

      while(rs.next()) 
      { 
       fila[0]= rs.getString("id"); 
       fila[1]= rs.getString("enfermedad"); 
       fila[2]= rs.getString("descripcion"); 
       fila[3]= rs.getString("riesgo"); 
       fila[4]= rs.getString("tipo"); 

       tm.addRow(fila); 

      } 
      table.setModel(tm); 
      TableColumnModel columnModel = table.getColumnModel(); 
      columnModel.getColumn(0).setPreferredWidth(3); 
      columnModel.getColumn(1).setPreferredWidth(50); 
      columnModel.getColumn(2).setPreferredWidth(290); 



     } 
     catch(Exception d) 
     { 
      JOptionPane.showMessageDialog(null, d + " No se pudo conectar."); 
     } 


    } 
    public void ns() 
    { 
     cbx. //this is where not recognize the jcombobox 
    } 

} 
+0

請在代碼中顯示錯誤消息和上下文代碼 –

+0

幾個問題:您通常不希望公開您的字段,因爲這會增加錯誤的風險。更好的是讓它保密,然後讓外部課程通過受控的公共方法來選擇。下一個大問題是我們對程序的結構知之甚少,或者你想如何使用這個組件。請在您的問題中添加更多信息和相關代碼。 –

+0

抱歉,我現在投入 –

回答

0

使其成爲公共神奇不會讓你從世界任何地方訪問它。

您需要有該實例才能訪問該實例。

1

你的問題似乎只是一個變量作用域。當你在你的構造函數中聲明cbx變量時,它在構造函數中只可見。如果您將聲明移至該類,則該變量在整個課程中都可見。

因此,如果您將JComboBox cbx = new JComboBox();移動到類級別,換句話說,如果將此語句移動到private JTextField txtnom;語句正下方,那麼cbx字段將在ns方法中可見。

所以,換句話說,這些更改:在代碼中

public class enfermedades extends JFrame { 
    private JPanel contentPane; 
    private JTextField txtid; 
    private JTextField textField_1; 
    private JTextField txtnom; 
    private static JTable table; 

    // !! added 
    private JComboBox cbx = new JComboBox(); 

    // ...... code removed for brevity's sake 


    public enfermedades() { 


     // ...... code removed for brevity's sake 

     panel_1.add(btnGuardar); 
     // !! JComboBox cbx = new JComboBox(); // ***** Don't declare this here **** 


     // ...... code removed for brevity's sake 


    } 

    // ...... code removed for brevity's sake 


    public void ns() { 

     // cbx field is now visible here 

    } 
} 

其它無關的問題:

  • 避免使用空佈局和的setBounds。雖然這可能看起來是創建GUI的最簡單方法,但相信我,事實並非如此。學習如何使用佈局管理器並使用它們會更好。
  • 沒有任何mysql字段或JTable表字段應該是靜態的。它們只應該是實例(非靜態)字段。這意味着你不應該試圖以靜態方式訪問它們。
  • llenar方法不應該是靜態的,你應該從你創建的實例中調用它。
  • 瞭解並遵循Java命名約定,以便其他人(我們)可以更好地理解您的代碼。類名以大寫字母開頭,變量和方法名以小寫字母開頭。
  • 你的SQL代碼應該位於它自己的類中,而不是在GUI類的代碼中。
  • 您將需要在GUI的主事件線程「EDT」的單獨線程中進行sql調用。