JLabel没有显示另一个类的价值

时间:2014-07-04 05:11:29

标签: java swing jlabel

我有两个GUI类。当我在第一个用户界面中单击JTable中的行时,第二个界面应在JLabels中显示相应的值。 但第二个用户界面不显示值。

这是我的第一个GUI类:

public class ChequeGUI extends JFrame {

public String chqNo;
public String payName;
public double chkAmount = 10;
public Date chkDate;

JTable guiTable = new JTable();

DefaultTableModel model = new DefaultTableModel(new Object[][]{},new String[]{"Cheque Number","Payee Name","Cheque Amount","Cheque Date"});

public ChequeGUI() throws SQLException {

    guiTable.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e){

            //guiTable.getTableHeader().getDefaultRenderer();

            int row = guiTable.getSelectedRow();

            chqNo = (String) guiTable.getValueAt(row,0);
            payName = (String) guiTable.getValueAt(row,1);
            chkAmount = (Double) guiTable.getValueAt(row,2);
            chkDate = (Date) guiTable.getValueAt(row, 3);

            try {
                PrintChequeGUI pcg = new PrintChequeGUI();
                pcg.setTitle("Print Cheque");
                pcg.setVisible(true);

                System.out.println(chqNo);
                System.out.println(payName);
                System.out.println(chkAmount);
                System.out.println(chkDate);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    guiTable.setModel(model);
    add(new JScrollPane(guiTable));

    DBConnection connection = new DBConnection();

    //Populate Table
    ChequeDAOImpl chqdi = new ChequeDAOImpl();
    chqdi.setConnection(connection);
    List<Cheque> cheques = chqdi.getCheques();


    for(Cheque cq : cheques){
    model.addRow(new Object[]{cq.getChqNum(), cq.getName(),cq.getAmount(),cq.getDate()});
    }

}



 }

这是我的第二个GUI类:

public class PrintChequeGUI extends JFrame {

private JPanel contentPane;
private JTextField txtAmount;

/**
 * Create the frame.
 * @throws SQLException 
 */
public PrintChequeGUI() throws SQLException {

    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 480, 400);
    contentPane = new JPanel();
    contentPane.setBackground(new Color(176, 224, 230));
    contentPane.setForeground(SystemColor.inactiveCaption);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(10, 11, 454, 84);
    contentPane.add(panel);


    JPanel panel_1 = new JPanel();
    panel_1.setBackground(new Color(220, 220, 220));
    panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
    panel_1.setBounds(10, 106, 454, 198);
    contentPane.add(panel_1);
    panel_1.setLayout(null);

    JLabel lblNewLabel_1 = new JLabel("Date:");
    lblNewLabel_1.setBounds(327, 11, 40, 14);
    panel_1.add(lblNewLabel_1);

    JLabel lblDate = new JLabel();
    lblDate.setBounds(368, 11, 69, 14);
    panel_1.add(lblDate);

    JLabel lblNewLabel_2 = new JLabel("Payee to the Order of");
    lblNewLabel_2.setBounds(10, 50, 125, 14);
    panel_1.add(lblNewLabel_2);

    JLabel lblName = new JLabel();
    lblName.setBounds(134, 50, 214, 14);
    panel_1.add(lblName);

    JLabel lblRs = new JLabel("Rs.");
    lblRs.setBounds(351, 50, 24, 14);
    panel_1.add(lblRs);

    JLabel lblAmount = new JLabel();
    lblAmount.setBounds(375, 50, 69, 14);
    panel_1.add(lblAmount);

    txtAmount = new JTextField();
    txtAmount.setBounds(10, 83, 338, 20);
    panel_1.add(txtAmount);
    txtAmount.setColumns(10);

    JLabel lblRupees = new JLabel("Rupees");
    lblRupees.setBounds(351, 86, 46, 14);
    panel_1.add(lblRupees);

    JLabel lbl = new JLabel("Cheque Number:");
    lbl.setBounds(10, 126, 100, 14);
    panel_1.add(lbl);

    JLabel lblChequeNum = new JLabel();
    lblChequeNum.setBounds(115, 126, 46, 14);
    panel_1.add(lblChequeNum);

    JLabel lblSig = new JLabel("<<Sig>>");
    lblSig.setBounds(321, 151, 90, 14);
    panel_1.add(lblSig);

    JLabel lblSigName = new JLabel("A.B.C.Test Name");
    lblSigName.setBounds(311, 176, 100, 14);
    panel_1.add(lblSigName);

    JPanel panel_2 = new JPanel();
    panel_2.setBackground(new Color(220, 220, 220));
    panel_2.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
    panel_2.setBounds(10, 315, 454, 40);
    contentPane.add(panel_2);
    panel_2.setLayout(null);

    JButton btnPrint = new JButton("Print");
    btnPrint.setBounds(102, 11, 89, 23);
    panel_2.add(btnPrint);

    JButton btnBack = new JButton("Back");
    btnBack.setBounds(263, 11, 89, 23);
    panel_2.add(btnBack);

    ChequeGUI gui = new ChequeGUI();
    lblChequeNum.setText(gui.chqNo);
    lblAmount.setText(Double.toString(gui.chkAmount));
    lblName.setText(gui.payName);
    lblDate.setText(String.valueOf(gui.chkDate));

}
}

1 个答案:

答案 0 :(得分:2)

这可以通过多种方式解决:

1

正如我在评论中提到的,将ChequeGUI的当前实例传递给PrintChequeGUI的构造函数并使用该实例而不是在PrintChequeGUI中创建新实例

PrintChequeGUI pcg = new PrintChequeGUI(this);

并在PrintChequeGUI

public PrintChequeGUI(ChequeGUI gui) throws SQLException {

(但我认为这不是一个好方法)

2

对于更好的选项,将所选的Cheque实例作为构造函数参数传递给PrintChequeGUI。为此,您需要创建TableModel,其实例为Cheque 样品如下:

package com.test;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class ChequeGUI extends JFrame {

JTable guiTable = new JTable();

ChequeTableModel model;

public ChequeGUI() throws SQLException {

    guiTable.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {

            // guiTable.getTableHeader().getDefaultRenderer();

            int row = guiTable.getSelectedRow();

            Cheque c = model.getChequeByRow(row);

            try {
                PrintChequeGUI pcg = new PrintChequeGUI(c);
                pcg.setTitle("Print Cheque");
                pcg.setVisible(true);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    model = new ChequeTableModel();
    guiTable.setModel(model);
    add(new JScrollPane(guiTable));

    model.loadData();

}

public class ChequeTableModel extends AbstractTableModel {

    List<Cheque> dataModel;

    String[] columns = { "Cheque Number", "Payee Name", "Cheque Amount",
            "Cheque Date" };

    public ChequeTableModel() {
        super();
        dataModel = new ArrayList<Cheque>();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public int getRowCount() {
        return dataModel.size();
    }

    @Override
    public Object getValueAt(int row, int column) {
        Object value = null;

        Cheque c = getChequeByRow(row);
        switch (column) {
        case 0:
            value = c.chqNo;
            break;
        case 1:
            value = c.payName;
            break;
        case 2:
            value = c.chkAmount;
            break;
        case 3:
            value = c.chkDate;
            break;

        default:
            break;
        }
        return value;
    }

    public Cheque getChequeByRow(int row) {
        if (dataModel.size() <= 0)
            return null;
        if (row < 0)
            return null;

        return dataModel.get(row);
    }

    public void loadData() {
        /*
         * 
         * //Uncomment this for database connection and load data from
         * database; //Please note to disconnect database if not needed
         * 
         * 
         * DBConnection connection = new DBConnection();
         * 
         * // Populate Table ChequeDAOImpl chqdi = new ChequeDAOImpl();
         * chqdi.setConnection(connection); List<Cheque> cheques =
         * chqdi.getCheques();
         * 
         * for (Cheque cq : cheques) { model.addRow(new Object[] {
         * cq.getChqNum(), cq.getName(), cq.getAmount(), cq.getDate() }); }
         */

        for (int i = 0; i < 10; i++) {
            Cheque c = new Cheque();
            c.chkAmount = i * 1000;
            c.chqNo = String.valueOf(i);
            c.chkDate = new Date(System.currentTimeMillis());
            dataModel.add(c);
        }
        fireTableRowsInserted(0, dataModel.size());
    }

}

public static void main(String[] args) throws SQLException {
    ChequeGUI c = new ChequeGUI();
    c.pack();
    c.setVisible(true);
}

}

第二课

package com.test;

 import java.awt.Color;
 import java.awt.SystemColor;
 import java.sql.SQLException;

 import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class PrintChequeGUI extends JFrame {

private JPanel contentPane;
private JTextField txtAmount;

/**
 * Create the frame.
 * 
 * @throws SQLException
 */
public PrintChequeGUI(Cheque cheque) throws SQLException {

    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 480, 400);
    contentPane = new JPanel();
    contentPane.setBackground(new Color(176, 224, 230));
    contentPane.setForeground(SystemColor.inactiveCaption);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(10, 11, 454, 84);
    contentPane.add(panel);

    JPanel panel_1 = new JPanel();
    panel_1.setBackground(new Color(220, 220, 220));
    panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
    panel_1.setBounds(10, 106, 454, 198);
    contentPane.add(panel_1);
    panel_1.setLayout(null);

    JLabel lblNewLabel_1 = new JLabel("Date:");
    lblNewLabel_1.setBounds(327, 11, 40, 14);
    panel_1.add(lblNewLabel_1);

    JLabel lblDate = new JLabel();
    lblDate.setBounds(368, 11, 69, 14);
    panel_1.add(lblDate);

    JLabel lblNewLabel_2 = new JLabel("Payee to the Order of");
    lblNewLabel_2.setBounds(10, 50, 125, 14);
    panel_1.add(lblNewLabel_2);

    JLabel lblName = new JLabel();
    lblName.setBounds(134, 50, 214, 14);
    panel_1.add(lblName);

    JLabel lblRs = new JLabel("Rs.");
    lblRs.setBounds(351, 50, 24, 14);
    panel_1.add(lblRs);

    JLabel lblAmount = new JLabel();
    lblAmount.setBounds(375, 50, 69, 14);
    panel_1.add(lblAmount);

    txtAmount = new JTextField();
    txtAmount.setBounds(10, 83, 338, 20);
    panel_1.add(txtAmount);
    txtAmount.setColumns(10);

    JLabel lblRupees = new JLabel("Rupees");
    lblRupees.setBounds(351, 86, 46, 14);
    panel_1.add(lblRupees);

    JLabel lbl = new JLabel("Cheque Number:");
    lbl.setBounds(10, 126, 100, 14);
    panel_1.add(lbl);

    JLabel lblChequeNum = new JLabel();
    lblChequeNum.setBounds(115, 126, 46, 14);
    panel_1.add(lblChequeNum);

    JLabel lblSig = new JLabel("<<Sig>>");
    lblSig.setBounds(321, 151, 90, 14);
    panel_1.add(lblSig);

    JLabel lblSigName = new JLabel("A.B.C.Test Name");
    lblSigName.setBounds(311, 176, 100, 14);
    panel_1.add(lblSigName);

    JPanel panel_2 = new JPanel();
    panel_2.setBackground(new Color(220, 220, 220));
    panel_2.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
    panel_2.setBounds(10, 315, 454, 40);
    contentPane.add(panel_2);
    panel_2.setLayout(null);

    JButton btnPrint = new JButton("Print");
    btnPrint.setBounds(102, 11, 89, 23);
    panel_2.add(btnPrint);

    JButton btnBack = new JButton("Back");
    btnBack.setBounds(263, 11, 89, 23);
    panel_2.add(btnBack);

    // ChequeGUI gui = new ChequeGUI();
    lblChequeNum.setText(cheque.chqNo);
    lblAmount.setText(Double.toString(cheque.chkAmount));
    lblName.setText(cheque.payName);
    lblDate.setText(String.valueOf(cheque.chkDate));

}
}

您已用于从数据库中获取详细信息的Cheque