简体   繁体   English

Java继承/重用能力

[英]Java Inheritance/Reuse-ability in swing

package fisheriesdatabase;

import java.sql.*;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FisheriesDatabase {

public static void main(String[] args) {
    JFrame frame=new JFrame("Fish Data Entry");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel=new JPanel();
    frame.add(panel);
    placeComponents(panel);

    frame.setVisible(true);
}

public static void placeComponents(JPanel panel){
    panel.setLayout(null);

    JLabel idlabel=new JLabel("id");
    idlabel.setBounds(10, 10, 80, 25);
    panel.add(idlabel);
    JTextField idtextfield=new JTextField(20);
    idtextfield.setBounds(100, 10, 160, 25);
    panel.add(idtextfield);

    JLabel namelabel=new JLabel("Name");
    namelabel.setBounds(10, 40, 80, 25);
    panel.add(namelabel);        
    JTextField nametextfield=new JTextField(20);
    nametextfield.setBounds(100, 40, 160, 25);
    panel.add(nametextfield);

    JButton button=new JButton("Enter Data");
    button.setBounds(10, 80, 80, 25);
    panel.add(button);

}

public static void connectDB() throws SQLException{
    final String url="jdbc:mysql://localhost:3306";
    final String driver="com.mysql.jdbc.Driver";
    final String dbName="netbeans_test";
    final String uname="root";
    final String pass="";

    Connection conn=null;
    try{
        //Registering the Driver
        Class.forName(driver).newInstance();

        //Open a connection
        conn=DriverManager.getConnection(url+dbName,uname,pass);
        Statement st=conn.createStatement();
        st.executeUpdate("insert into test values('"+idtextfield.getText()+"','"+nametextfield.getText()+"')");            

    }
    catch(ClassNotFoundException | InstantiationException | IllegalAccessException se){
        if(conn==null)
            System.err.println("DATABASE NOT CONNECTED");
        se.printStackTrace();
    }
}
}

The above mentioned code is my beginning to Swing, and as a novice I am just trying to create 2 methods 上面提到的代码是我开始使用Swing的开始,作为一个新手,我只是尝试创建2个方法

  • One for the database connectivity 一种用于数据库连接
  • Other for the gui 其他的gui
    It shows an error when I try to access the placeComponent()'s properties inside the connectDB(). 当我尝试访问connectDB()内的placeComponent()的属性时,它显示一个错误。 Can anybody help me out here? 有人可以帮我吗?

The error is in executeUpdate statement where it is unable to recognize the 'idtextfield' and 'nametextfield' 错误发生在executeUpdate语句中,在该语句中无法识别“ idtextfield”和“ nametextfield”

Thanks!!! 谢谢!!!

I suggest you start with examples from the Swing tutorial to learn how to better structure your program. 我建议您从Swing教程中的示例开始,以学习如何更好地构建程序。

Maybe the section on How to Use Labels would be a good simple example to start with. 也许有关如何使用标签的部分将是一个很好的简单示例。 In this example a panel is used to contain all the components. 在此示例中,面板用于包含所有组件。 This will allow you to create instance variables that you can access from any method you implement in the panel class. 这将允许您创建实例变量,您可以从在面板类中实现的任何方法访问该实例变量。

Other benefits of starting with a working example: 从工作示例开始的其他好处:

  1. You get rid of your static methods. 您摆脱了静态方法。

  2. It doesn't use setBounds(). 它不使用setBounds()。 Swing was designed to be used with Layout Managers. Swing被设计为与布局管理器一起使用。 See the tutorial section on Layout Managers. 请参见布局管理器中的教程部分。

  3. The code will be created on the EDT. 该代码将在EDT上创建。 See the tutorial section on Concurrency . 请参阅关于Concurrency的教程部分。

Declare / Define both of them at class level: 在课程级别声明/定义它们两者:

public class FisheriesDatabase {

JTextField idtextfield=new JTextField(20);
JTextField nametextfield=new JTextField(20);

public static void main(String[] args) {
    ....
    ....
}

public static void placeComponents(JPanel panel){
    panel.setLayout(null);

    .....
}   

There is basically a single problem in your program. 您的程序中基本上只有一个问题。 You are using 2 methods placeComponents and connectDB() . 您正在使用2个方法placeComponentsconnectDB() You have declared and initialised the variables idtextfield and nametextfield in placeComponents() method and tried to access it from the connectDB() method. 您已经在placeComponents()方法中声明并初始化了变量idtextfieldnametextfield ,并尝试从connectDB()方法访问它。 The scope of the variables is only in the method placeComponents() . 变量的范围仅在方法placeComponents() So, it gives the error of "unable to recognize the 'idtextfield' and 'nametextfield'". 因此,它给出了“无法识别'idtextfield'和'nametextfield'”的错误。

You could solve this problem in 2 ways. 您可以通过两种方式解决此问题。

  1. You could declare them in the class. 您可以在课程中声明它们。

  2. You could get the instances from the JPanel instance. 您可以从JPanel实例获取实例。

Also in this regard I would like to draw you attention to another thing. 在这方面,我想提醒您注意另一件事。 You have used Statement to execute query using text that is entered by users. 您已使用Statement使用用户输入的文本执行查询。 There is a potential of 'SQL Injection'. 有可能发生“ SQL注入”。 So it would be better if you used PreparedStatement instead. 因此,最好改用PreparedStatement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM