如果声明,控制权不会通过

时间:2017-02-10 18:22:12

标签: java awt

我有一个名为country的数据库,其中包含3个表country(country_name),state(state_name,country_name),city(city_name,state_name),我想通过此框架添加ountry,state和city但是控件不是在actionPerformed()

下查看第二个if语句

here is the image of console and frame

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Admin extends Frame implements ActionListener
{
    MenuBar mb;
    Menu edit;
    MenuItem country,state,city;
    Label l1,l2,l3,l4,l5;
    TextField t1,t2,t3;
    Button b1,b2,b3;
    Choice ch1,ch2;
    String str1,str2,str3,str4;
    Connection con;
    PreparedStatement ps;
    ResultSet rs;

    public Admin()
    {
        super("Admin");

        setLayout(null);
        setSize(700,700);
        mb=new MenuBar();
        edit=new Menu("Edit");
        country=new MenuItem("Add Country");
        state=new MenuItem("Add State");
        city=new MenuItem("Add City");
        edit.add(country);
        edit.add(state);
        edit.add(city);
        mb.add(edit);

        country.addActionListener(this);
        state.addActionListener(this);
        city.addActionListener(this);
        setMenuBar(mb);
        setVisible(true);
        try
        {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                            con=DriverManager.getConnection
 ("jdbc:ucanaccess://D:/java/country.mdb");
        }
        catch(Exception e){}
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==country){
            removeAll();
            l1=new Label("Country Name");
            t1=new TextField(20);
            b1=new Button("Update");
            l1.setBounds(150,100,100,25);
            t1.setBounds(290,100,100,25);
            b1.setBounds(280,140,70,25);

            add(l1);
            add(t1);
            add(b1);
            b1.addActionListener(this);
            setVisible(true);
            //System.out.println("Outside of if");

           if(ae.getSource()==b1){
                System.out.println("Inside of if");
                try
                {
                    ps=con.prepareStatement("insert into country
   (country_name) values('"+t1.getText()+"')");
                    ps.executeUpdate();

                }   
                catch(Exception e){}
            }
            //Add_country ob=new Add_country();
            //ob.setVisible(true);
        }
        else if(ae.getSource()==state){
            removeAll();
            l2=new Label("Select Country");
            ch1=new Choice();
            l3=new Label("Add State");
            t2=new TextField();
            b2=new Button("Update");

            l2.setBounds(150,100,100,25);
            ch1.setBounds(290,100,100,25);
            l3.setBounds(150,160,100,25);
            t2.setBounds(290,160,100,25);
            b2.setBounds(280,200,70,25);

            add(l2);
            add(ch1);
            add(l3);
            add(t2);
            add(b2);
            b2.addActionListener(this);
            setVisible(true);

            try
            {
                ps=con.prepareStatement("select country_name from country");
                rs=ps.executeQuery();
                while(rs.next())
                {
                    str1=rs.getString("country_name");
                    ch1.addItem(""+str1);
                }
                if(ae.getSource()==b2){
                    System.out.println("sdfghjk");
                    str2=ch1.getSelectedItem();
                    ps=con.prepareStatement("insert into state
   (state_name,country_name) values('"+t2.getText()+"','"+str2+"')");
                    ps.executeUpdate();


                }
            }
            catch(Exception e){}
            //Add_state obj=new Add_state();
            //obj.setVisible(true);
        }
        else if(ae.getSource()==city){
            removeAll();
            l4=new Label("Select State");
            ch2=new Choice();
            l5=new Label("Add City");
            t3=new TextField();
            b3=new Button("Update");

            l4.setBounds(150,100,100,25);
            ch2.setBounds(290,100,100,25);
            l5.setBounds(150,160,100,25);
            t3.setBounds(290,160,100,25);
            b3.setBounds(280,200,70,25);

            add(l4);
            add(ch2);
            add(l5);
            add(t3);
            add(b3);
            b3.addActionListener(this);
            setVisible(true);
            try
            {
                ps=con.prepareStatement("select state_name from state");
                rs=ps.executeQuery();
                while(rs.next())
                {
                    str3=rs.getString("state_name");
                    ch2.addItem(""+str3);
                }

                if(ae.getSource()==b3){
                    System.out.println("sdfghjk");
                    str4=ch2.getSelectedItem();
                    ps=con.prepareStatement("insert into city
   (city_name,state_name) values('"+t3.getText()+"','"+str4+"')");
                    ps.executeUpdate();
                }
            }
            catch(Exception e){}
            //Add_city obj=new Add_city();
            //obj.setVisible(true);
        }
    }
    public static void main(String s[])
    {
        Admin ob=new Admin();
    }
}

1 个答案:

答案 0 :(得分:1)

此...

if(ae.getSource()==country){
    //...
    if(ae.getSource()==b1){

是不可能的,你不能拥有ActionEvent由两个不同的控件触发生成一个actionPerformed事件(它们一个接一个地出现)

观察

  • 避免使用null布局,像素完美布局是现代ui设计中的错觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的结束,您将花费越来越多的时间来纠正
  • AWT真的已经过时了,人们对Swing没有日子甚至是JavaFX
  • 有更多的经验
  • 您没有关闭(数据库)资源,这可能导致无法访问数据库的问题。请查看The try-with-resources Statement以获取可能的解决方案
  • 我还建议How to Use CardLayout提供更好的组件和视图管理