我在程序执行时调用了一个输入对话框,并且要将图像设置为背景,但在加载图像之前调用输入框。
package shanu;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
public class toneaccount extends JPanel
{
String url,sql;
Connection conn;
Statement stmt;
ResultSet rs;
ImageIcon i1;
JLabel l1;
jdbc连接
public void connection()
{
url="jdbc:odbc:java";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection(url);
stmt=conn.createStatement();
System.out.println("Successfully connected..!");
}catch(SQLException e){System.out.println("Error="+e);}
catch(Exception err){System.out.println("Error="+err);}
}
部分从对话框中获取输入并查看访问表并显示背景图像
如果显示表,则图像设置为可见false,如果else语句运行,则设置为true ...
在设置图像之前调用对话框,并在对话框运行后设置图像
public toneaccount()
{
i1=new ImageIcon("shanu/pics/menu.jpg");
l1=new JLabel(i1);
l1.setBounds(0,0,1366,768);
add(l1);
}
public void toneaccount1()
{
setLayout(null);
final String[] colHeads={"Account Number","Customer Name","Account Type","Balance","Amount","Date of Transaction","New Balance","Type of Transaction"};
Object[][] data=new Object[44][44];
try
{
String r=JOptionPane.showInputDialog(null,"Enter Account Number");
sql="select * from transaction where accountno='"+r+"' ";
rs=stmt.executeQuery(sql);
if(rs.next())
{
l1.setVisible(false);
int i=0;
String sql1="select * from transaction where accountno='"+r+"' ";
rs=stmt.executeQuery(sql1);
while(rs.next())
{
String daccount=rs.getString("accountno");
String dacctype=rs.getString("customername");
String dname=rs.getString("accounttype");
String dinitialamt=rs.getString("existingbalance");
String damount=rs.getString("amount");
String ddate=rs.getString("currentdate");
String dnbal=rs.getString("newbalance");
String dtype=rs.getString("type");
data[i][0]=daccount;
data[i][1]=dacctype;
data[i][2]=dname;
data[i][3]=dinitialamt;
data[i][4]=damount;
data[i][5]=ddate;
data[i][6]=dnbal;
data[i][7]=dtype;
i++;
显示表格中收到的数据
JTable t=new JTable(data,colHeads);
t.setRowHeight(25);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane js=new JScrollPane(t,v,h);
js.setBounds(0,0,1366,768);
add(js);
}
rs.close();
stmt.close();
conn.close();
}
else
{
JOptionPane.showMessageDialog(null," Account Does Not Exist");
l1.setVisible(true);
}
}catch(Exception e){System.out.println("Error"+e);}
}
主要部分
public static void main(String[] args)
{
toneaccount tlis1=new toneaccount();
JFrame f=new JFrame("list");
f.setContentPane(tlis1);
f.setSize(1366,768);
f.setVisible(true);
}
}