Java 公共类中的JComboBox

Java 公共类中的JComboBox,java,swing,class,jcombobox,Java,Swing,Class,Jcombobox,在此处输入代码嗨,我是Java的新开发人员,我想在公共类中放置一个JComboBox以获取所选项目,但Java无法识别它,例如,我想这样做 myComboBox.getSelectedItem().toString() 当我尝试放置JTextField时,效果很好 对不起,我的表达我英语说得不太好 这是我的密码 package guarderia; import java.awt.BorderLayout; public class enfermedades extends JFrame

在此处输入代码
嗨,我是Java的新开发人员,我想在公共类中放置一个JComboBox以获取所选项目,但Java无法识别它,例如,我想这样做

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
     }

}
package-guarderia;
导入java.awt.BorderLayout;
公共类enfermedades扩展JFrame{
私有JPanel内容窗格;
私有JTextField txtid;
私有JTextField textField_1;
私有JTextField txtnom;
私有静态JTable表;
//MYSQL上的变量PARA CONEXION
静态连接con;
静态callableps语句;
静态结果集;
静态模式tm;
静态语句sql;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
enfermedades框架=新的enfermedades();
frame.setVisible(true);
llenar();
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共恩费梅代兹(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(1001001211663);
contentPane=newjpanel();
setBackground(新颜色(255、255、255));
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel面板=新的JPanel();
panel.setLayout(空);
面板.立根(新颜色(0,0,0));
面板立根(0,0,1624,53);
contentPane.add(面板);
JLabel lblEnfermedades=新JLabel(“ENFERMEDADES”);
前景色(颜色:白色);
setFont(新字体(“对话框”,Font.BOLD,19));
lblEnfermedades.挫折(12,12,231,41);
面板。添加(lblEnfermedades);
JPanel panel_1=新的JPanel();
面板1.设置布局(空);
面板1.立根(10102426510);
contentPane.add(面板1);
JLabel lblIdNio=新JLabel(“厄尔尼诺”);
立根(12,30,70,15);
面板1.添加(lblIdNio);
txtid=新的JTextField();
addKeyListener(新的KeyAdapter(){
@凌驾
public void keyTyped(KeyEvent e){
int c=e.getKeyChar();
如果(c<'0'| c>'9')e.consume();
}
@凌驾
公共无效密钥已释放(密钥事件e){
con=(连接)conexion.conectar();
试一试{
if(txtid.getText().equals(“”)
{
txtnom.setText(“”);
}
ps=(CallableStatement)con.prepareCall({callselect_nin(?)});
ps.setLong(1,Integer.parseInt(txtid.getText());
ResultSet res=ps.executeQuery();
if(res.next())
{
字符串tipo=res.getString(“Nombre”);
txtnom.setText(tipo);
llenar();
}
其他的
{
txtnom.setText(空);
}
}捕获(SQLException e1){
showMessageDialog(null,e1);
}
}
});
txtid.setColumns(10);
立根(120,28,146,19);
面板1.添加(txtid);
JLabel cbi=新JLabel(“Enfermedad”);
cbi.挫折(12,95,194,15);
面板1.添加(cbi);
JLabel lblTratamiento=新JLabel(“trataminto”);
lblTratamiento.setBounds(1213019415);
小组1.增补(lblTratamiento);
JButton btnNueva=新JButton(“Nueva”);
addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件arg0){
}
});
btnNueva.setBounds(282,90,88,25);
面板1.添加(btnNueva);
JTextPane textPane=新的JTextPane();
textPane.setBounds(12、157、402、118);
面板1.添加(文本窗格);
JLabel lblfechadetecion=新JLabel(“Fecha检测”);
lblFechaDeteccion.setBounds(1229919415);
面板1.添加(LBLFechadetecion);
textField_1=新的JTextField();
textField_1.设置列(10);
textField_1.立根(135297,95,19);
面板_1.添加(文本字段_1);
JLabel lblNombre=新JLabel(“Nombre”);
lblNombre.挫折(12,61,70,15);
面板1.添加(lblNombre);
txtnom=newjtextfield();
txtnom.setEditable(false);
txtnom.setColumns(10);
txtnom.setBounds(120,56,294,19);
面板1.添加(txtnom);
JLabel LBLMEDADRECOMENDAS=新JLabel(“Medidas建议”);
lblMedidadRecomendadas.setBounds(123319415);
小组1.增补(LBLMEDIDADRECOMENDAS);
JTextPane textPane_1=新的JTextPane();
文本窗格_1.立根(1235540293);
面板1.添加(文本面板1);
JButton btnGuardar=新JButton(“Guardar”);
b Nguardar.挫折(297、470、117、25);
面板1.添加(btnGuardar);
JComboBox cbx=新的JComboBox();
cbx.立根(120,90,146,24);
面板1.添加(cbx);
JPanel panel_2=新的JPanel();
面板2.设置布局(空);
面板2.立根背景(新颜色(220、20、60));
面板2.立根(10,65,426,32);
contentPane.add(面板2);
JLabel lblRegistrarEnfermedad=新JLabel(“注册商enfermedad”);
lblRegistrarEnfermedad.setForeground(颜色:白色);
lblRegistrarEnfermedad.setFont(新字体(“对话框”,Font.BOLD,19));
lblRe
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

    }
}