当我按下回车键时,如何使用java swing文本框中输入的文本从mysql中检索数据?

时间:2013-01-10 16:08:44

标签: java swing jdbc applet

enter image description here以下是我的Java Swing applet代码,其中第一个字段为employee codetext box for entering employee code,我想在输入{{{I}时从MySql数据库中检索员工的数据在键盘上点击employee code并在相应的text boxEnter key中设置检索数据后,{} {}中的1}}。并将其作为applet嵌入JSP中。

请帮忙。

这是我的代码:

text boxes

3 个答案:

答案 0 :(得分:0)

在您的ActionListener按钮中使用textfield.getText()检索文本字段的文本,然后使用此值查询数据库。我假设你很清楚如何查询数据库。

答案 1 :(得分:0)

等待!你还没有实现ActionListener

执行以下操作:

       but_per.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            /// write your code here!
          String a= field.getText();
           System.out.print(a);
         ///or DB code
        }
        });

如果您不熟悉Action Listener

,请LINK

<强>更新

如果您想在按键后进行所有处理,则必须实现键盘事件, 你可以找到一个教程链接here

答案 2 :(得分:0)

此源代码是我可以生成的代码中最接近的可行版本,但它仍然存在许多严重问题。

  • 错误地使用start()方法。每次从最小化恢复浏览器时,都会调用该方法。
  • 使用null布局和setBounds()
  • 自由浮动的非模态GUI元素会导致焦点问题..

话虽如此,在员工代码字段中键入一个数字,然后按Enter键以查看类似的内容(屏幕截图垂直缩短)..

<code>JFrame</code> with <code>JOptionPane</code>

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/* <applet code=App width=400 height=600></applet> */
public class App extends JApplet {

    public void start() {
        final JFrame frame = new JFrame("Form");

        ActionListener doDB = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                JOptionPane.showMessageDialog(frame, "Query the DB off the EDT!");
            }
        };

        // This cannot be done in a sand-boxed applet
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        // hides the bottom field..
        //frame.setResizable(false);
        JPanel panel = new JPanel();

        JLabel label1 = new JLabel("");
        JTextField field = new JTextField(20);
        JButton but_per = new JButton("Personalize");

        Container c;
        c = frame.getContentPane();
        c.setLayout(null);
        JLabel name = new JLabel("Name :");
        JLabel compcode = new JLabel("Company Code :");
        JLabel cardno = new JLabel("Card Number: ");
        JLabel cardtype = new JLabel("Card Type :");
        JLabel pin = new JLabel("Pin :");
        JLabel bldgrp = new JLabel("Blood Group :");
        JLabel empcode = new JLabel("Employee Code :");
        JLabel dob = new JLabel("DOB :");
        JLabel valupto = new JLabel("Valid Upto :");
        JLabel jdate = new JLabel("Joining Date :");
        JLabel dept = new JLabel("Department :");
        JLabel uid = new JLabel("UID :");
        String data[] = {"A", "AB", "B", "B +", "A +", "O +", "O -"};
        JTextField nametxt = new JTextField(10);
        JComboBox compcodetxt = new JComboBox();
        JTextField cardnumtxt = new JTextField(10);
        JTextField cardtypetxt = new JTextField(10);
        JTextField pintxt = new JTextField(10);
        JComboBox bldgrptxt = new JComboBox(data);
        bldgrptxt.setSelectedIndex(5);
        JTextField empcodetxt = new JTextField(10);
        empcodetxt.addActionListener(doDB);
        JTextField dobtxt = new JTextField(10);
        JTextField valuptotxt = new JTextField(10);
        JTextField jdatetxt = new JTextField(10);
        JTextField depttxt = new JTextField(10);
        JTextField uidtxt = new JTextField(10);

        empcode.setBounds(10, 10, 100, 25);
        empcodetxt.setBounds(110, 10, 100, 25);
        name.setBounds(10, 40, 100, 25);
        nametxt.setBounds(110, 40, 100, 25);
        compcode.setBounds(10, 70, 100, 25);
        compcodetxt.setBounds(110, 70, 100, 25);
        cardno.setBounds(10, 100, 100, 25);
        cardnumtxt.setBounds(110, 100, 100, 25);
        bldgrp.setBounds(10, 130, 100, 25);
        bldgrptxt.setBounds(110, 130, 100, 25);
        dob.setBounds(10, 160, 100, 25);
        dobtxt.setBounds(110, 160, 100, 25);
        valupto.setBounds(10, 190, 100, 25);
        valuptotxt.setBounds(110, 190, 100, 25);
        jdate.setBounds(10, 220, 100, 25);
        jdatetxt.setBounds(110, 220, 100, 25);
        dept.setBounds(10, 250, 100, 25);
        depttxt.setBounds(110, 250, 100, 25);
        uid.setBounds(10, 280, 100, 25);
        uidtxt.setBounds(110, 280, 100, 25);

        but_per.setBounds(10, 340, 120, 25);

        c.add(name);
        c.add(nametxt);
        c.add(compcode);
        c.add(compcodetxt);
        c.add(cardno);
        c.add(cardnumtxt);
        c.add(pin);
        c.add(pintxt);
        c.add(bldgrp);
        c.add(bldgrptxt);
        c.add(empcode);
        c.add(empcodetxt);
        c.add(dob);
        c.add(dobtxt);
        c.add(valupto);
        c.add(valuptotxt);
        c.add(jdate);
        c.add(jdatetxt);
        c.add(dept);
        c.add(depttxt);
        c.add(uid);
        c.add(uidtxt);
        c.add(but_per);

        frame.setSize(350, 400);
        frame.setVisible(true);
    }

    public void stop() {
    }
}