最近在做毕设,碰到图片所示的问题,请问有人知道如何解决吗?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class NumberDialog extends JDialog implements ActionListener{
Container c;
JLabel l,l1;
JComboBox jcb;
JCheckBox jcb1,jcb2;
JButton SureButton,CancelButton;
String ColName;
public NumberDialog(String s){//
ColName=s;
c=this.getContentPane();
this.setLayout(null);//不采用任何布局方式
init7();
setTitle("数据类型选择");
this.setSize(420,330);
this.setLocation(480,170);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
}
void init7() {
c.setBounds(0,0,420,330);
l=new JLabel("对于NUMBER类型的"+ColName+"变量,");
l.setBounds(50,15,400,30);
l.setFont(new Font("微软雅黑",Font.PLAIN,12));
c.add(l);
l1=new JLabel("您选择的数据类型为:");
l1.setBounds(50,40,400,30);
l1.setFont(new Font("微软雅黑",Font.PLAIN,12));
c.add(l1);
jcb=new JComboBox();
jcb.setOpaque(false);
jcb.setFont(new Font("微软雅黑",Font.PLAIN,12));
jcb.setBounds(50,75,250,25);
jcb.addItem("INT");
jcb.addItem("FLOAT");
jcb.addItem("DOUBLE");
c.add(jcb);
jcb1=new JCheckBox("作为之后预校验的默认选择");
jcb1.setFont(new Font("微软雅黑",Font.PLAIN,12));
jcb1.setBounds(50,110,400,30);
c.add(jcb1);
jcb2=new JCheckBox("作为之后数据库迁移的默认选择");
jcb2.setFont(new Font("微软雅黑",Font.PLAIN,12));
jcb2.setBounds(50,140,400,30);
c.add(jcb2);
SureButton=new JButton("确定");
SureButton.setBounds(200,230,70,35);
SureButton.addActionListener(this);
SureButton.setFont(new Font("微软雅黑",Font.PLAIN,12));
SureButton.setBackground(Color.white);
c.add(SureButton);
CancelButton=new JButton("取消");
CancelButton.setBounds(280,230,70,35);
CancelButton.addActionListener(this);
CancelButton.setFont(new Font("微软雅黑",Font.PLAIN,12));
CancelButton.setBackground(Color.white);
c.add(CancelButton);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==SureButton) {
dispose();
}
if(e.getSource()==CancelButton) {
}
}
}
我测了你这个类,没问题,能够弹出来。
测试代码如下:
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
/**
* 1:按钮JButton
* @author biexiansheng
*
*/
public class MyFrame extends JFrame {
public void MyFrame(){
JFrame jf=new JFrame();//实例化一个JFrame对象
Container container=jf.getContentPane();//将窗体转化为容器
//Container container=getContentPane();
container.setLayout(null);
JLabel jl=new JLabel("这是一个JFrame窗体");//在窗体中设置标签
jl.setHorizontalAlignment(JLabel.CENTER);//将标签中的文字置于标签中间的位置
container.add(jl);//将标签添加到容器中
JButton jb=new JButton("点我");//实例化一个按钮属性
jb.setBounds(20, 20,100, 50);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//使MyJDialog窗体可见
new NumberDialog("hello").setVisible(true);
//上面一句话使对话框窗体可见,这样就实现了当用户单机该按钮后将弹出对话框的功能
}
});
container.add(jb);//将按钮属性添加到容器中
//设置容器里面的属性特点
container.setBackground(Color.blue);
//设置容器的框架结构特性
jf.setTitle("这是一个容器");//设置容器的标题
jf.setVisible(true);//设置容器可视化
jf.setSize(450, 400);//设置容器的大小
//设置容器的关闭方式
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyFrame fm=new MyFrame();
fm.MyFrame();
}
}
检查下你的容器代码,是不是调用方式有问题。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.PrintStream;
import java.sql.*;
import java.util.Arrays;
public class PreverifyFrame extends JFrame implements ActionListener{
Container c;
JLabel l,l1,l2;
JButton LastButton,StartButton,CancelButton;
String Oip,Oport,Odbname,Ousername,Opassword,Mip,Mport,Musername,Mpassword;
String [] OracleDataType= {"FLOAT","CHAR","VARCHAR2","NCHAR","NVARCHAR2","DATE","TIMESTAMP"};
String [] MySQLDataType= {"FLOAT","CHAR","VARCHAR","NCHAR","NVARCHAR","DATETIME","TIMESTAMP"};
public PreverifyFrame(String s1,String s2,String s3,String s4,String s5,String s6,String s7,String s8,String s9){//
Oip=s1;
Oport=s2;
Odbname=s3;
Ousername=s4;
Opassword=s5;
Mip=s6;
Mport=s7;
Musername=s8;
Mpassword=s9;
c=this.getContentPane();
c.setLayout(null);//不采用任何布局方式
init4();
setTitle("Oracle到MySQL数据库的迁移工具");
this.setSize(600,500);
this.setLocation(400,100);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
void init4() {
c.setBounds(0,0, 600, 500);
l=new JLabel("预校验");
l.setBounds(50,15,500,100);
l.setFont(new Font("微软雅黑",Font.PLAIN,20));
c.add(l);
l1=new JLabel("在开始数据库迁移之前,需要进行预校验。");
l1.setBounds(80,100,300,30);
l1.setFont(new Font("微软雅黑",Font.PLAIN,15));
c.add(l1);
l2=new JLabel("点击“开始”则开始进行预校验。");
l2.setBounds(80,150,300,30);
l2.setFont(new Font("微软雅黑",Font.PLAIN,15));
c.add(l2);
LastButton=new JButton("<上一步");
LastButton.setBounds(250, 410, 90, 35);
LastButton.addActionListener(this);
LastButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
LastButton.setBackground(Color.white);
c.add(LastButton);
StartButton=new JButton("开始");
StartButton.setBounds(360, 410, 90, 35);
StartButton.addActionListener(this);
StartButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
StartButton.setBackground(Color.white);
c.add(StartButton);
CancelButton=new JButton("取消");
CancelButton.setBounds(470, 410, 90, 35);
CancelButton.addActionListener(this);
CancelButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
CancelButton.setBackground(Color.white);
c.add(CancelButton);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==LastButton) {
}
if(e.getSource()==StartButton) {
try {
long start=System.currentTimeMillis();
Font font=new Font("微软雅黑",Font.PLAIN,12);
UIManager.put("OptionPane.messageFont", font);
UIManager.put("OptionPane.buttonFont", font);
Connection con1=JavaConOracle.getConnection("jdbc:oracle:thin:@"+Oip+":"+Oport+":"+Odbname,Ousername,Opassword);
Connection con2=JavaConMySQL.getConnection("jdbc:mysql://"+Mip+":"+Mport+"/sys?useSSL=false&serverTimezone=Asia/Shanghai",Musername,Mpassword);
Statement st,st1,st2,st3,st4,st5,st6;
ResultSet res1,res2,res3,res4,res5,res6,res7;
PreparedStatement pst1;
String sqll="select value from nls_database_parameters where parameter='NLS_CHARACTERSET'";
String characterset="";
try {
st1=con1.createStatement();
res1=st1.executeQuery(sqll);
while(res1.next()) {
String value=res1.getString("value");
characterset=value.substring(value.length()-3, value.length());
}
}catch(Exception e1) {
e1.printStackTrace();
}
String sql="create database if not exists "+Odbname+" default character set "+characterset;
try {
st=con2.createStatement();
st.executeUpdate(sql);
st.close();
}catch(Exception e1) {
System.out.println("在MySQL中创建"+Odbname+"数据库失败");
}
Connection con3=JavaConMySQL.getConnection("jdbc:mysql://"+Mip+":"+Mport+"/"+Odbname+"?useSSL=false&serverTimezone=Asia/Shanghai",Musername,Mpassword);
String [] TabName= new String[10];
Arrays.fill(TabName, "");
// String sql1="select table_name from user_tables";//查看用户下所有表名
String sql1="select OBJECT_NAME from user_objects where OBJECT_TYPE='TABLE' order by CREATED";
int i=0;
try {
st1=con1.createStatement();
res1=st1.executeQuery(sql1);
while(res1.next()) {
String tablename=res1.getString("OBJECT_NAME");
TabName[i]=tablename;
i++;
}
res1.close();
st1.close();
}catch(Exception e1) {
e1.printStackTrace();
}
for(int q=0;q<i;q++) {
try {
String [] ColName= new String[10];
String [] ColType= new String[10];
String [] ColLen= new String[10];
String [] NullAble= new String[10];
Arrays.fill(ColName, "");
Arrays.fill(ColType, "");
Arrays.fill(ColLen, "");
Arrays.fill(NullAble, "");
String sql2="select column_name,data_type,data_length,nullable from user_tab_columns where table_name='"+TabName[q]+"' order by COLUMN_ID";
st2=con1.createStatement();
res2=st2.executeQuery(sql2);
int m=0;
while(res2.next()) {
String columnname=res2.getString("column_name");
String datatype=res2.getString("data_type");
String datalength=res2.getString("data_length");
String Isnull=res2.getString("nullable");
ColName[m]=columnname;
ColType[m]=datatype;
ColLen[m]=datalength;
NullAble[m]=Isnull;
m++;
}
res2.close();
st2.close();
for(int k=0;k<m;k++) {
if(ColType[k].equals("NUMBER")) {
new NumberDialog(ColName[k]).setVisible(true);
}
else {
for(int l=0;l<7;l++) {
if(ColType[k].equals(OracleDataType[l]))
ColType[k]=MySQLDataType[l];
}
}
}
if(NullAble[0].equals("N")) {
String sql3;
if(ColType[0].equals("DATETIME")) {
sql3="create table "+TabName[q]+"("+ColName[0]+" DATETIME(6) not null)";
}
else if(ColType[0].substring(0, 3).equals("TIM")) {
sql3="create table "+TabName[q]+"("+ColName[0]+" TIMESTAMP(6) not null)";
}
else if(ColType[0].equals("DOUBLE")) {
sql3="create table "+TabName[q]+"("+ColName[0]+" DOUBLE not null)";
}
else {
sql3="create table "+TabName[q]+"("+ColName[0]+" "+ColType[0]+"("+ColLen[0]+") not null"+")";
}
st3=con3.createStatement();
st3.executeUpdate(sql3);
st3.close();
}
else {
String sql3;
if(ColType[0].equals("DATETIME")) {
sql3="create table "+TabName[q]+"("+ColName[0]+" DATETIME(6) )";
}
else if(ColType[0].substring(0, 3).equals("TIM")) {
sql3="create table "+TabName[q]+"("+ColName[0]+" TIMESTAMP(6) )";
}
else if(ColType[0].equals("DOUBLE")) {
sql3="create table "+TabName[q]+"("+ColName[0]+" DOUBLE )";
}
else {
sql3="create table "+TabName[q]+"("+ColName[0]+" "+ColType[0]+"("+ColLen[0]+")"+")";
}
st3=con3.createStatement();
st3.executeUpdate(sql3);
st3.close();
}
for(int a=1;a<m;a++) {
String sql4;
if(NullAble[a].equals("N")) {
if(ColType[a].equals("DATETIME")) {
sql4="alter table "+TabName[q]+" add column "+ColName[a]+" DATETIME(6) not null";
}
else if(ColType[a].substring(0, 3).equals("TIM")){
sql4="alter table "+TabName[q]+" add column "+ColName[a]+" TIMESTAMP(6) not null";
}
else if(ColType[a].equals("DOUBLE")) {
sql4="alter table "+TabName[q]+" add column "+ColName[a]+" DOUBLE not null";
}
else
{
sql4="alter table "+TabName[q]+" add column "+ColName[a]+" "+ColType[a]+"("+ColLen[a]+") not null";
}
st4=con3.createStatement();
st4.executeUpdate(sql4);
st4.close();
}
else {
if(ColType[a].equals("DATETIME")) {
sql4="alter table "+TabName[q]+" add column "+ColName[a]+" DATETIME(6)";
}
else if(ColType[a].substring(0, 3).equals("TIM")) {
sql4="alter table "+TabName[q]+" add column "+ColName[a]+" TIMESTAMP(6)";
}
else if(ColType[a].equals("DOUBLE")) {
sql4="alter table "+TabName[q]+" add column "+ColName[a]+" DOUBLE";
}
else {
sql4="alter table "+TabName[q]+" add column "+ColName[a]+" "+ColType[a]+"("+ColLen[a]+")";
}
st4=con3.createStatement();
st4.executeUpdate(sql4);
st4.close();
}
}
}catch(Exception e1) {
System.out.println("创建"+TabName[q]+"表失败");
}
}//创建表名、列名、列属性、非空约束成功
double time=(System.currentTimeMillis()-start)/1000.0;
System.out.println("预校验成功,共耗时"+time+"s");
JOptionPane.showMessageDialog(this,"预校验成功,共耗时"+time+"s");
String sql9="drop database "+Odbname;
st6=con3.createStatement();
st6.executeUpdate(sql9);
st6.close();
}catch(Exception e1) {
System.out.println("预校验失败");
JOptionPane.showMessageDialog(this,"预校验失败");
}
}
if(e.getSource()==CancelButton) {
}
}
}