Java Inheritance/Reuse-ability 摇摆不定

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();
    }
}
}

上面的代码是我Swing的入门,作为新手我只是尝试创建2个方法

错误出现在无法识别'idtextfield'和'nametextfield'

的executeUpdate语句中

谢谢!!!

在class级别声明/定义它们:

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);

    .....
}   

我建议您从 Swing tutorial 中的示例开始学习如何更好地构建您的程序。

也许 How to Use Labels 部分是一个很好的简单示例。在此示例中,面板用于包含所有组件。这将允许您创建实例变量,您可以从您在面板 class.

中实现的任何方法访问这些实例变量

从工作示例开始的其他好处:

  1. 您摆脱了静态方法。

  2. 它不使用 setBounds()。 Swing 旨在与布局管理器一起使用。请参阅有关布局管理器的教程部分。

  3. 代码将在 EDT 上创建。请参阅 Concurrency.

  4. 上的教程部分

你的程序基本上只有一个问题。您正在使用 2 种方法 placeComponentsconnectDB()。您已在 placeComponents() 方法中声明并初始化变量 idtextfieldnametextfield,并尝试从 connectDB() 方法访问它。变量的范围仅在方法placeComponents()中。所以,它给出了 "unable to recognize the 'idtextfield' and 'nametextfield'" 的错误。

您可以通过两种方式解决这个问题。

  1. 您可以在 class 中声明它们。

  2. 您可以从 JPanel 实例中获取实例。

在这方面,我还想提请您注意另一件事。您已使用 Statement 使用用户输入的文本执行查询。有'SQL Injection'的潜力。所以如果你使用 PreparedStatement 会更好。