在netbeans中从一个java类移动到另一个java类

时间:2014-10-19 17:49:11

标签: java swing netbeans

单击contact1.class中的按钮时,数据将插入数据库。这意味着,查询正在执行,程序运行正常,直到ps.execute();.之后,我调用的语言类没有正确打开。从某种意义上说,框架是以白色背景颜色打开的。但是,如果我通过我的主要方法调用它;它工作正常,如果我也跳过这个类,那么该项目将顺利运行。

这是我的contact1.java类

public class Contact1 implements ActionListener{

 boolean clicked=false;
        JFrame jf; 
      JLabel  lblName,lblEmail;
      JTextField txtName,txtPhone;
      JButton btnSave;
      JPanel p1;
     GridLayout gl42;
    FlowLayout fl;
    static Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
    static int w=(int)dim.getWidth();    static int h=(int)dim.getHeight();
        int xpos=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2-215;
        int ypos=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2-215;


@SuppressWarnings("LeakingThisInConstructor")
    public Contact1() throws IOException{

        jf=new JFrame("THE COUNTRY CLUB");

          jf.setSize(w,h);
          jf.setVisible(true);
          jf.setAlwaysOnTop(true);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        //jf.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("D:/workspace/Crorepati/Images/bg.jpg")))));         
          lblName= new JLabel("Name");
          lblNo= new JLabel("Contact No");
          txtName=new JTextField(40);
          txtPhone=new JTextField(40);
          btnSave=new JButton("Play Game");
              txtName.setBounds(xpos,ypos,50,40);
              btnSave.setActionCommand("Play Game");
              btnSave.addActionListener(this);
          p1=new JPanel();

           p1.add(lblName);
           p1.add(txtName);
           p1.add(lblNo);
           p1.add(txtPhone);
           p1.add(btnSave);


           jf.add(p1);

           jf.setVisible(true);
    }


public static void main(String[] args) throws IOException
{
Contact1 a1 = new Contact1();
}
    public void actionPerformed (ActionEvent a)
    {
    String name = txtName.getText();
    String contact = txtPhone.getText();
    try
    {

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection con = DriverManager.getConnection("Jdbc:Odbc:details");
   PreparedStatement ps = con.prepareStatement("insert into details (Name,Contact) values (?,?)");
   ps.setString(1,name);
   ps.setString(2,contact);       
   ps.execute();
   txtName.setText("");
   txtPhone.setText("");

  // Language.main(new String []{"A"});
   Language language = new Language();
    }

    catch(Exception e)
    {
    JOptionPane.showMessageDialog(null, e);
    }

    }

这是我的第二个名为Language.java的类

 public class Language extends JPanel {

JFrame jf;
LanguageSelect l = new LanguageSelect();
English eng = new English();
Arabic arab = new Arabic();  


 public Language() {
    try{
        buildGUI();
        perform();
    }catch(Exception e){}
}

public void buildGUI() throws Exception{

      jf=new JFrame("THE COUNTRY CLUB");

      Toolkit t=Toolkit.getDefaultToolkit();
      Dimension dim=t.getScreenSize();
      int width=(int)dim.getWidth();  int height=(int)dim.getHeight();


      JLayeredPane jlp1=new JLayeredPane(),
                              jlp2=new JLayeredPane(),
                              jlp3=new JLayeredPane();

      Box panelbox=new Box(BoxLayout.Y_AXIS);

      jlp1.setLayout(new BoxLayout(jlp1,BoxLayout.X_AXIS));
      jlp2.setLayout(new BorderLayout());
      jlp3.setLayout(new GridLayout(1,1));

      jlp1.add(BorderLayout.EAST,new LogoPanel());
    //  jlp1.add(BorderLayout.WEST,d);

      jlp3.add(eng);jlp3.add(arab);
      jlp3.setPreferredSize(new Dimension(1250,160));

      panelbox.add(jlp1);panelbox.createVerticalStrut(200);
      panelbox.add(l);
      panelbox.add(jlp3);

      jf.getContentPane().add(panelbox);

      jf.setSize(width,height);
      jf.setVisible(true);
      jf.setAlwaysOnTop(true);
      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void perform()throws Exception{

    while ((eng.clicked==false)&&(arab.clicked==false) )
    {

    }

    if(eng.clicked==true) 
    {
    if(eng.optiona.equals("English")) 
    {    
    eng.repaint(); 
    Run_Project.main();                            
    }
    }
    else if(arab.clicked==true)
    {
    if(arab.optiona.equals("Arabic")) 
    {
    arab.repaint(); 
    Run_Project1.main();                         
    }

    }

    }

    public static void main (String[] args) throws Exception{
        new Language();
    } 

我不知道这里发生了什么。提前致谢。

0 个答案:

没有答案