检查数据库后的GUI问题

时间:2014-07-15 06:19:19

标签: java swing user-interface jdbc

我现在已经进入测试阶段了。基本上我有一个客户端添加框架和主菜单框架。在客户端添加帧我已保存并保存并退出。由于某种原因,当我输入相同的客户端名称两次并单击保存并退出我的程序选择客户端已经存在于数据库中并返回主菜单但它没有正确绘制所有我得到的是白色屏幕。下面是我的保存和退出按钮的代码,以及主菜单方法。

从客户端添加表单保存并退出按钮。

 b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Login1.class.getName()).log(Level.SEVERE, null, ex);
        }
        //##DECLARES FILEPATH##
        String filePath = "C://Users/Chris/java/Tutorials GUI/2.Login page with Database/Login1/Login1.accdb";

        //##PULL FILEPATH FROM ABOVE##
        String db = "jdbc:ucanaccess://"+filePath;
        Connection connect = null;
        try {
            connect = DriverManager.getConnection(db);
            /// CONNECTION  MADE
        } catch (SQLException ex) {
            Logger.getLogger(Login1.class.getName()).log(Level.SEVERE, null, ex);
        }
        //try below because i was having trouble passing a boolean from a check box.
        Boolean meeting = false;
        Byte a = 0;
        String yn = "No";
        try{
            String clientName = t.getText();
            if (t1.isSelected()){
                meeting = true;
                a = 1;
                yn = "Yes";
            }
            else{
                meeting = false;
                a = 0;
                yn = "No";
            }
            try ( //// WORKING 
                    PreparedStatement statement = (PreparedStatement) connect.prepareStatement("INSERT INTO ClientTable(ClientName,Meeting)VALUES('"+clientName+"','"+yn+"')")) {
               //statement.setString(1,clientName);
               //statement.setString(2,yn);
                statement.executeUpdate();
                statement.close();
                connect.close();
                System.out.println("Success!");
                JOptionPane.showMessageDialog(null, "Client added to database returning to main menu.");
            }
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, "Client name already in Database!");
            System.out.println("Error" + ex);


        }
        adcFrame.setVisible(false); // makes current frame invisible to user.
        mainMenu(); // goes to main menu frame.
        t.setText(""); // emptys text fields on current frame incase we recall it.
        qcount(); //Calls a count method for use further on in program
        }
    });

主菜单方法。

private void mainMenu(){
    if (mmDraw== true){
        frame.setVisible(true);
    }else{
    //Create and set up the window.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Set up the content pane.
    mainMenuPain(frame.getContentPane());
    //Display the window.
    //frame.setSize(900,900);
    frame.pack();
    frame.setVisible(true);
    mmDraw=true;
    }
}

其他主菜单设置。

private void mainMenuPain(Container pane) {

  // pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
    pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

JButton logout = new JButton("Logout");
JButton adc = new JButton("Add Client");
JButton adf = new JButton("Add File");
JButton adtf = new JButton("Add Todo To File");
JButton ttdl = new JButton("Todays Todo List");
JButton fftl = new JButton("Find Files Todo list");

    logout.addActionListener((ActionEvent e) -> {
        JOptionPane.showMessageDialog(null, "Goodbye");
        frame.setVisible(false);
        loginframe.setVisible(true);
    });

    adf.addActionListener((ActionEvent e) -> {
        try{
        frame.setVisible(false);
        if (adfDraw == false){
            fileFrame();
            adfDraw = true;
        }
        else{
            adfFrame.setVisible(true);
            adfDraw = true;
        }
        }catch(Exception exe){
            System.out.println("ERROR:"+exe);
            frame.setVisible(true);
        }
    });

    adtf.addActionListener((ActionEvent e)->{
        frame.setVisible(false);
        if (adtdDraw==false){
            todoFrame();
            adtdDraw=true;  
            adtdFrame.setVisible(true);
        }else{
            adtdFrame.setVisible(true);
            adtdDraw=true;
        }
    });

    adc.addActionListener((ActionEvent e) -> {
        frame.setVisible(false);
        if (adcDraw == false){
            clientFrame();
            adcDraw = true;
        }
        else{
            adcFrame.setVisible(true);
            adcDraw = true;
        }
   });

    fftl.addActionListener((ActionEvent e)->{
        frame.setVisible(false);
        if (fftlDraw==false){
            findtodoFrame();
            fftlDraw=true;  
            fftlFrame.setVisible(true);
        }else{
            fftlFrame.setVisible(true);
            fftlDraw=true;
        }
    });
    ttdl.addActionListener((ActionEvent e)->{
        check();
       frame.setVisible(false);

           todays();
           tdDraw=true;
           tdFrame.setVisible(true);

    });

pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;  

int buttonMovement = 0;
int numButtons = 6;

while (buttonMovement < numButtons){
c.gridy = buttonMovement;
buttonMovement = buttonMovement + 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weightx = 0.5;
c.gridx = 1;
c.gridwidth = 5;
    if (buttonMovement == 1){
        pane.add(adc, c);
    }
    if (buttonMovement == 2){
        pane.add(adf, c);
    }
    if (buttonMovement ==3){
        pane.add(adtf, c);    
    }
    if (buttonMovement ==4){
        pane.add(ttdl, c);   
    }
    if (buttonMovement ==5){
        pane.add(fftl, c);   
    }
    if (buttonMovement ==6){
        pane.add(logout, c);   
    }

}

}

对我来说这是一个非常令人困惑的事情,如果这是一个简单的解决方案,那么道歉!我也为每个帧设置了一个布尔值设置,如果先前已经绘制了该帧,则布尔值变为真。并且在恢复该帧时,仅设置setVisible。

感谢

0 个答案:

没有答案