下边这段程序,我希望实现的功能:方法weixiuluru()实现在两个文本框输入两个字符串,并把输入的两个字符串用静态方法test传给方法weixiutongji(),而方法weixiutongji()则实现输出所有包含这两个字符串的记录和数量.import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.util.*;
import java.sql.*;class luruweixiu extends Frame
{ Panel p1,p2,p3,p4;
Label e1,e2;
TextField text1,text2;
Button b1;public String luruweixiu()
{
e1=new Label("设备名称");
e2=new Label("设备型号");
//test t11=new test();
text1=new TextField(20);
text2=new TextField(20);
b1=new Button("设备维修信息统计");
p1=new Panel();p2=new Panel();p3=new Panel();
p1.add(e1);p1.add(text1);
p1.add(e2);p1.add(text2);
p1.add(b1);
setLayout(new FlowLayout());
add(p1); b1.addActionListener(new B1());
addWindowListener(new WinClose());
setSize(800,600);
setTitle("设备维修信息统计条件输入窗口");
setVisible(true);
}
class B1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
test.t1=text1.getText();
test.t2=text2.getText();
System.out.println(text1.getText());
System.out.println(text2.getText());
System.out.println("t1"+t1);
//return t1;
//new weixiuliulan().showRecord();
//焦点移到第一个输入框
} catch(Exception e2){
System.out.println("输入数据有误!");
text1.requestFocus(); }
}
}
class WinClose extends WindowAdapter //关闭窗口
{
public void windowClosing(WindowEvent e)
{
System.exit(0); }
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////class weixiutongji implements ActionListener
{
JFrame f;
Container cp;
JPanel jpS,jpanelWest;
JButton jbt1,jbt2;//按钮,查询、取消、修改
JLabel label,L; //标签
//定义文本框
JTable table;//用来接收数据库中返回的信息
Object columnName[]={"设备维修编号","设备编号","设备名称","设备型号","制造厂家","安装地点","维修时间","维修数量","维修内容"};
Object ar[][] =new Object[60][9];
String sno;
String count="xx";
weixiutongji()
{ System.out.println("t1"+test.t1);
f=new JFrame();
cp=f.getContentPane(); // 初始化面板、按钮、标签、文本框
jpS=new JPanel();
jpanelWest=new JPanel();
//------------------------------------------------
jbt1=new JButton("确定");
jbt2=new JButton("返回");
//------------------------------------------------
label=new JLabel("<html><font color=#CC00FF size='4'>设备维修信息总览</font>",SwingConstants.CENTER);
label.setForeground(Color.blue);
L=new JLabel("设备信息库共有设备维修信息"+count+"条");
//------------------------------------------------
table=new JTable(ar,columnName);//ar存放表中的数据,columnname表示列名
JScrollPane scrollpane = new JScrollPane(table);
//------------------------------------------------
//布局,添加控件
jpS.add(jbt1);
jpS.add(jbt2);
JPanel jpanel=new JPanel();
jpanel.add(label);
JPanel pp4=new JPanel();
JPanel jpE=new JPanel();
cp.add(jpanel,"North");
JPanel jp=new JPanel();
//jp.add(scrollpane);
JPanel p=new JPanel();//用来放两个表
p.setLayout(new BorderLayout());
p.add(L,"North");
p.add(scrollpane);
cp.add(pp4,"West");
cp.add(p,"Center");
cp.add(jpS,"South");
cp.add(jpE,"East");
//------------------------------------------------
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width; /*取得显示器窗口的宽度*/
int y=screen.height; /*取得显示器窗口的高度*/
//f.setSize(400,330);
f.setSize(800,600);
//int xcenter=(x-350)/2;
int xcenter=(x-780)/2;
//int ycenter=(y-330)/2;
int ycenter=(y-580)/2;
f.setLocation(xcenter,ycenter);/*显示在窗口中央*/
f.setVisible(true);
//-------------------------------------------------
jbt1.addActionListener(this);//注册监听器
jbt2.addActionListener(this);
/* f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);*/
}
//------------------------------------------------
int i=0;
public void showRecord()
{
while(i>=0)
{
ar[i][0]="";
ar[i][1]="";
ar[i][2]="";
ar[i][3]="";
ar[i][4]="";
ar[i][5]="";
ar[i][6]="";
ar[i][7]="";
ar[i][8]="";
i--;
}
i=0;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){System.out.println("加载驱动程序失败!");}
try{
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=设备信息库.mdb";//直接使用当前类目录下的数据库文件
Connection con=DriverManager.getConnection(url);
System.out.println("1"+test.t1);
/*String s="select weixiu.设备维修编号,weixiu.设备编号,shebei.设备名称,shebei.设备型号,shebei.制造厂家,shebei.安装地点,weixiu.设备维修时间,weixiu.设备维修数量,weixiu.设备维修内容 from weixiu inner join shebei on weixiu.设备编号=shebei.设备编号";*/
String s="select weixiu.设备维修编号,weixiu.设备编号,shebei.设备名称,shebei.设备型号,shebei.制造厂家,shebei.安装地点,weixiu.设备维修时间,weixiu.设备维修数量,weixiu.设备维修内容 from weixiu,shebei where 设备名称=test.t1 and 设备型号=test.t2 and weixiu.设备编号=shebei.设备编号";
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery(s);
while(rs.next())
{
String a1=rs.getString(1);
String a2=rs.getString(2);
String a3=rs.getString(3);
String a4=rs.getString(4);
String a5=rs.getString(5);
String a6=rs.getString(6);
String a7=rs.getString(7);
String a8=rs.getString(8);
String a9=rs.getString(9);
ar[i][0]=a1;
ar[i][1]=a2;
ar[i][2]=a3;
ar[i][3]=a4;
ar[i][4]=a5;
ar[i][5]=a6;
ar[i][6]=a7;
ar[i][7]=a8;
ar[i][8]=a9;
System.out.println(ar[i][0]); i++;
}
count=""+i+"";
L.setText("设备信息库共有设备维修信息"+count+"条");
f.repaint();
con.close();
}catch(SQLException g)
{
System.out.println("E Code"+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("确定"))
{
f.setVisible(false);
}
if(cmd.equals("返回"))
f.setVisible(false);
}public static void main(String []arg){
new luruweixiu();
weixiutongji a=new weixiutongji();
a.showRecord();
}
class test{
static String t1;
static String t2;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.util.*;
import java.sql.*;class luruweixiu extends Frame
{ Panel p1,p2,p3,p4;
Label e1,e2;
TextField text1,text2;
Button b1;public String luruweixiu()
{
e1=new Label("设备名称");
e2=new Label("设备型号");
//test t11=new test();
text1=new TextField(20);
text2=new TextField(20);
b1=new Button("设备维修信息统计");
p1=new Panel();p2=new Panel();p3=new Panel();
p1.add(e1);p1.add(text1);
p1.add(e2);p1.add(text2);
p1.add(b1);
setLayout(new FlowLayout());
add(p1); b1.addActionListener(new B1());
addWindowListener(new WinClose());
setSize(800,600);
setTitle("设备维修信息统计条件输入窗口");
setVisible(true);
}
class B1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
test.t1=text1.getText();
test.t2=text2.getText();
System.out.println(text1.getText());
System.out.println(text2.getText());
System.out.println("t1"+t1);
//return t1;
//new weixiuliulan().showRecord();
//焦点移到第一个输入框
} catch(Exception e2){
System.out.println("输入数据有误!");
text1.requestFocus(); }
}
}
class WinClose extends WindowAdapter //关闭窗口
{
public void windowClosing(WindowEvent e)
{
System.exit(0); }
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////class weixiutongji implements ActionListener
{
JFrame f;
Container cp;
JPanel jpS,jpanelWest;
JButton jbt1,jbt2;//按钮,查询、取消、修改
JLabel label,L; //标签
//定义文本框
JTable table;//用来接收数据库中返回的信息
Object columnName[]={"设备维修编号","设备编号","设备名称","设备型号","制造厂家","安装地点","维修时间","维修数量","维修内容"};
Object ar[][] =new Object[60][9];
String sno;
String count="xx";
weixiutongji()
{ System.out.println("t1"+test.t1);
f=new JFrame();
cp=f.getContentPane(); // 初始化面板、按钮、标签、文本框
jpS=new JPanel();
jpanelWest=new JPanel();
//------------------------------------------------
jbt1=new JButton("确定");
jbt2=new JButton("返回");
//------------------------------------------------
label=new JLabel("<html><font color=#CC00FF size='4'>设备维修信息总览</font>",SwingConstants.CENTER);
label.setForeground(Color.blue);
L=new JLabel("设备信息库共有设备维修信息"+count+"条");
//------------------------------------------------
table=new JTable(ar,columnName);//ar存放表中的数据,columnname表示列名
JScrollPane scrollpane = new JScrollPane(table);
//------------------------------------------------
//布局,添加控件
jpS.add(jbt1);
jpS.add(jbt2);
JPanel jpanel=new JPanel();
jpanel.add(label);
JPanel pp4=new JPanel();
JPanel jpE=new JPanel();
cp.add(jpanel,"North");
JPanel jp=new JPanel();
//jp.add(scrollpane);
JPanel p=new JPanel();//用来放两个表
p.setLayout(new BorderLayout());
p.add(L,"North");
p.add(scrollpane);
cp.add(pp4,"West");
cp.add(p,"Center");
cp.add(jpS,"South");
cp.add(jpE,"East");
//------------------------------------------------
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width; /*取得显示器窗口的宽度*/
int y=screen.height; /*取得显示器窗口的高度*/
//f.setSize(400,330);
f.setSize(800,600);
//int xcenter=(x-350)/2;
int xcenter=(x-780)/2;
//int ycenter=(y-330)/2;
int ycenter=(y-580)/2;
f.setLocation(xcenter,ycenter);/*显示在窗口中央*/
f.setVisible(true);
//-------------------------------------------------
jbt1.addActionListener(this);//注册监听器
jbt2.addActionListener(this);
/* f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);*/
}
//------------------------------------------------
int i=0;
public void showRecord()
{
while(i>=0)
{
ar[i][0]="";
ar[i][1]="";
ar[i][2]="";
ar[i][3]="";
ar[i][4]="";
ar[i][5]="";
ar[i][6]="";
ar[i][7]="";
ar[i][8]="";
i--;
}
i=0;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){System.out.println("加载驱动程序失败!");}
try{
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=设备信息库.mdb";//直接使用当前类目录下的数据库文件
Connection con=DriverManager.getConnection(url);
System.out.println("1"+test.t1);
/*String s="select weixiu.设备维修编号,weixiu.设备编号,shebei.设备名称,shebei.设备型号,shebei.制造厂家,shebei.安装地点,weixiu.设备维修时间,weixiu.设备维修数量,weixiu.设备维修内容 from weixiu inner join shebei on weixiu.设备编号=shebei.设备编号";*/
String s="select weixiu.设备维修编号,weixiu.设备编号,shebei.设备名称,shebei.设备型号,shebei.制造厂家,shebei.安装地点,weixiu.设备维修时间,weixiu.设备维修数量,weixiu.设备维修内容 from weixiu,shebei where 设备名称=test.t1 and 设备型号=test.t2 and weixiu.设备编号=shebei.设备编号";
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery(s);
while(rs.next())
{
String a1=rs.getString(1);
String a2=rs.getString(2);
String a3=rs.getString(3);
String a4=rs.getString(4);
String a5=rs.getString(5);
String a6=rs.getString(6);
String a7=rs.getString(7);
String a8=rs.getString(8);
String a9=rs.getString(9);
ar[i][0]=a1;
ar[i][1]=a2;
ar[i][2]=a3;
ar[i][3]=a4;
ar[i][4]=a5;
ar[i][5]=a6;
ar[i][6]=a7;
ar[i][7]=a8;
ar[i][8]=a9;
System.out.println(ar[i][0]); i++;
}
count=""+i+"";
L.setText("设备信息库共有设备维修信息"+count+"条");
f.repaint();
con.close();
}catch(SQLException g)
{
System.out.println("E Code"+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("确定"))
{
f.setVisible(false);
}
if(cmd.equals("返回"))
f.setVisible(false);
}public static void main(String []arg){
new luruweixiu();
weixiutongji a=new weixiutongji();
a.showRecord();
}
class test{
static String t1;
static String t2;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货