在try other方法中获取字符串的值

tktrz96b  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(462)

所以,这个程序有价值 car_number 从数据库 "getReprt" method ,并将按钮的名称作为db中的值。另外,我接下来需要得到这个值,到action listener btn_listener 每个按钮都有自己的值。但在这段代码中,我只得到最后一个数据值,每个按钮都是一样的。我怎样才能给每个actionlistener情境赋予不同的价值观?我做错什么了?

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class Car2q {
    private Connection con;
    private ResultSet rs;
    static JTextArea textArea = new JTextArea();
    static login frame = new login();
    static JScrollPane scrollPane = new JScrollPane();
    static JSplitPane splitPane = new JSplitPane();
    static JPanel panel_left = new JPanel();
    static JPanel panel_right = new JPanel();

    static class login extends JFrame {

        public static void main(String[] args) throws Exception {
            final Car2q c2q = new Car2q();
            c2q.getReport();

            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
            frame.setSize(550, 300);
            frame.setVisible(true);
            frame.add(splitPane);
            splitPane.setSize(550, 300);
            splitPane.setLeftComponent(scrollPane);
            splitPane.setLeftComponent(panel_left);
            splitPane.setRightComponent(panel_right);
            panel_left.setLayout(new GridLayout(0, 1, 5, 5));
            panel_right.setLayout(new GridLayout(0, 2, 5, 5));
        }
    }

    public Car2q() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("");
            con.createStatement();
        }
        catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }

    public String getReport() {
        String car_number = null;
        String report = null;
        List car_number_list = new ArrayList<>();
        try {
            PreparedStatement st = null;
            String query2 = "select * FROM car_register";
            st = con.prepareStatement(query2);
            rs = st.executeQuery();
            while (rs.next()) {
                car_number = rs.getString("car_number");
                String gotit = rs.getString("car_number");
                JButton btn = new JButton(gotit);
                btn.setSize(100, 100);
                panel_left.add(btn);
                ActionListener actionListener = new btn_listener();
                btn.addActionListener(actionListener);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return car_number;
    }

    public class btn_listener implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            Car2q c2q = new Car2q();
            String car = c2q.getReport();
            System.out.println(car);
        }
    }
}
x8goxv8g

x8goxv8g1#

car_number = rs.getString("car_number");
String gotit = rs.getString("car_number");
JButton btn = new JButton(gotit);

您不需要两个包含相同值的变量。你只需要把车号设置为按钮的文本。
然后你的密码 ActionListener 变成:

public void actionPerformed(ActionEvent e)
{
    JButton button = (JButton)e.getSource();
    System.out.println( button.getText() );
}

这个 ActionListener 现在是通用的,可以被所有按钮共享。
所以你的代码变成:

ActionListener buttonListener = new CarButtonListener();

        while (rs.next()) {
            //car_number = rs.getString("car_number");
            String gotit = rs.getString("car_number");
            JButton btn = new JButton(gotit);
            btn.setSize(100, 100);
            panel_left.add(btn);
            //ActionListener actionListener = new btn_listener();
            //btn.addActionListener(actionListener);
            btn.addActionListener(buttonListener);
        }

这允许您的代码更高效,使用更少的内存,因为您只需要侦听器的一个示例。
注意,我将您的“btn\u listener”类重命名为“carbuttonlistener”,因为:
类名应该是描述性的
类名中的每个单词都应该以大写字符开头

wrrgggsh

wrrgggsh2#

按以下方式编写按钮侦听器:

public class btn_listener implements ActionListener {
        private final String car_number;

        public bin_listener(String car_number) {
            this.car_number = car_number;
        }
        public void actionPerformed(ActionEvent arg0) {
            System.out.println(car_number);
        }
    }

然后在循环中创建actionlistener:

ActionListener actionListener = new btn_listener(car_number);

当然,要成为惯用的java,您应该使用 carNumber 以及 BtnListener 等等。

相关问题