-1

在此处输入图像描述以下是我的 Java Swing 小程序代码,其中我的第一个字段为employee codeand text box for entering employee code,我想在敲击键盘后输入时从 MySql 数据库中检索员工的employee code数据text boxEnter key并在相应的text boxesand中设置检索数据combo boxes。同样将它作为小程序嵌入到 JSP 中。

请帮忙。

这是我的代码:

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

public class App extends JApplet{

public void start()
{
JFrame frame = new JFrame("Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();

JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
JButton but_per = new JButton("Personalize");
//JButton button2 = new JButton("Cancel");
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);
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);
//pin.setBounds(10, 110, 100, 25);
//pintxt.setBounds(110, 110, 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);
//button2.setBounds(10, 70, 75, 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);


//panel.add(button1);
//panel.add(button2);
//frame.add(panel);
frame.setSize(350,400);
//frame.pack();
frame.setVisible(true);
}

public void stop(){}

}
4

3 回答 3

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

更新:

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

于 2013-01-10T17:08:11.577 回答
0

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

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

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

<code>JFrame</code> 和 <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() {
    }
}
于 2013-01-11T03:45:46.810 回答
0

在你ActionListener的那个按钮中使用检索文本字段的文本,textfield.getText()然后使用这个值来查询数据库。我假设您非常了解如何查询数据库。

于 2013-01-10T16:18:56.790 回答