2

我是 Stack Overflow 的新手,正在寻找我在大学一直在研究的 Java 应用程序的一些帮助。

我的问题是,如何使用按钮操作侦听器事件将 Jtextfield(在外部类中)中的整数插入 Jtable。

我的代码是:

将 jtextfield 整数插入表的外部类按钮代码

   package banknew;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;



public class CheckingAccount extends BANKNEW
{

final JButton DepositAmount = new JButton("Deposit");  
final JTextField tAmount = new JTextField();

    public void CheckingAccount() {
        String title = "Checking Account";
        JFrame checkingAccount = new JFrame(title);

        checkingAccount.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        checkingAccount.setSize(400, 200);
        checkingAccount.setLocation(checkingAccount.getHeight() / 2, checkingAccount.getWidth() / 2);

        final JLabel error = new JLabel("");
        checkingAccount.add(error, BorderLayout.SOUTH);
        error.setVisible(true);
        error.setLocation(5, 600);


        JMenuItem file1 = new JMenuItem("Checking Account");
        JMenuItem file2 = new JMenuItem("Checking Accounts");
        JMenuItem file3 = new JMenuItem("Checking Accounts");
        JMenuItem file4 = new JMenuItem("Bank Account");
        JMenuItem file5 = new JMenuItem("Close");



        JMenu filemenu = new JMenu("File");
        filemenu.add(file1);
        filemenu.add(file2);
        filemenu.add(file3);
        filemenu.addSeparator();
        filemenu.add(file4);
        filemenu.addSeparator();
        filemenu.add(file5);

        JMenuBar menubar = new JMenuBar();
        menubar.add(filemenu);
        checkingAccount.setJMenuBar(menubar);
        BorderLayout border = new BorderLayout();
        filemenu.setLayout(border);
        checkingAccount.setVisible(true);





        /**
         * ****************************************
         * Create Second JPanel - Buttons & ComboBox .
 *****************************************
         */
        JPanel abuttons1 = new JPanel();
        checkingAccount.add(abuttons1);
        //abuttons1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        abuttons1.setBorder(BorderFactory.createTitledBorder(""));

        JButton Withdraw = new JButton("Withdraw");
        JLabel transBankName1 = new JLabel("Account Name:");
        JLabel transAmount = new JLabel("Amount:");


        final JTextField tBankName1 = new JTextField(20);
        final JComboBox AccountName = new JComboBox();
        AccountName.setEditable(false);
        AccountName.setMaximumSize(new java.awt.Dimension(100, 20));
        AccountName.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        transBankName1.setMaximumSize(new java.awt.Dimension(100, 20));
        transAmount.setMaximumSize(new java.awt.Dimension(100, 20));
        tAmount.setMaximumSize(new java.awt.Dimension(100, 20));
        tBankName1.setMaximumSize(new java.awt.Dimension(00, 20));

        abuttons1.setLayout(new GridLayout(5, 1, 5, 5));
        abuttons1.add(transBankName1);
        abuttons1.add(AccountName);
        abuttons1.add(transAmount);
        abuttons1.add(tAmount);

        abuttons1.add(Withdraw);
        abuttons1.add(DepositAmount);
        abuttons1.setLocation(0, 0);
        abuttons1.setSize(300,200);

         DepositAmount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (table.getSelectedRow() > -1) {
                    // assuming from your code that you want to set the
                    // textfield's value at the table's selected row
                    try {
                        Integer amount = Integer.parseInt(tAmount.getText());
                        table.getModel().setValueAt(amount, table.getSelectedRow(), 4);
                    } catch (NumberFormatException nfe) {
                        // User did not provide a number.
                        // do nothing? show dialog? you name it!
                    }
                }
            }
        });
    }


    }
  • External 类有一个带有文本字段和“存款”按钮的 GUI。
  • 主类有一个带有 5 列 JTable 的 GUI

我想要做的就是拥有一个带有按钮的外部扩展类,然后在按钮上单击将本地文本字段中的数量插入到主 Jtable 中,如带有“abc”表模型的代码所示。

我搜索了谷歌和堆栈溢出,但大多数帖子都与 SQL 或数据库链接有关。

如果有人能指出我正确的方向,我将不胜感激。

谢谢

编辑2:复制并粘贴代码并尝试上传主类,复制后告诉我...

4

2 回答 2

3

我有点不确定你的课程是否更全球化。但如果 CheckingAccount 应该写入表,则需要在其构造函数中提供对表的引用。

public class CheckingAccount {
    final JButton depositAmount = new JButton("Deposit");  
    final JTextField tAmount = new JTextField();

    //Provide the JTable to the CheckingAccount when you construct it!
    public CheckingAccount(final JTable table) {
        depositAmountButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (table.getSelectedRow() > -1) {
                    // assuming from your code that you want to set the
                    // textfield's value at the table's selected row
                    try {
                        Integer amount = Integer.parseInt(textField.getText());
                        table.getModel().setValueAt(amount, table.getSelectedRow(), 4);
                    } catch (NumberFormatException nfe) {
                        // User did not provide a number.
                        // do nothing? show dialog? you name it!
                    }
                }
            }
        });
    }
}

要了解有关如何使用 Swing 的更多信息,请查看Oracle 教程

于 2013-01-30T11:14:21.567 回答
0

谢谢你,你的代码确实给了我们一个方向,它确实是最有价值的。

我已将此代码作为组件工作。关键在于:

final static

在此处输入图像描述

表格组件和按钮组件

重要提示:单击添加按钮时,我已成功添加新行。但添加不是从 1/0 开始,而是从第二行开始

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class AmendingTable {
    final static JButton        addData         = new JButton("Add");

    static String a;
    static String b;
                                                                //Don't introduce row here otherwise it will add 1 empty row at start
    final static DefaultTableModel model = new DefaultTableModel(null, new String [] {"Products", "Prices" });
    final static JTable table = new JTable(model);

    public static Component table() {
        model.setRowCount(0);
        table.setRowHeight(30);
        return table;
    }
    //ORGINAL
    public static Component addingRowse(final JTable table) {   
        addData.addActionListener(new ActionListener() {
            @Override
                public void actionPerformed(ActionEvent e) {


                            a = String.valueOf(TextFields.jtfProdName.getText());
                            b = String.valueOf(TextFields.jtfProdPrice.getText());

                            String [] row = {a, b};             

                            model.addRow(row);

                }
        });

        return addData;
    }
}

我如何在我的 Java 面板中调用它们。此处调用的表

public static Component orderLister() {
    jpOrderLister = new JPanel();

    jpOrderLister.setBounds(5, 105, 450, 300);
    jpOrderLister.setBackground(Color.GREEN);
    jpOrderLister.setLayout(new GridLayout(1,1,0,0)); //Border : Top / Left / Bottom / Right / Colour
    jpOrderLister.setBorder(BorderFactory.createMatteBorder(1, 5, 1, 5, Color.LIGHT_GRAY));

    //
    JScrollPane sPane = new JScrollPane();
    sPane.setPreferredSize(new Dimension(200, 150));
    sPane.getViewport().add(AmendingTable.table);
    sPane.setBackground(Color.white);

    jpOrderLister.add(sPane);

    return jpOrderLister;
}

Java面板中这样调用的按钮

public static Component buttons() {
    jpButtons = new JPanel();
    jpButtons.setBounds(200, 410, 100, 50);
    jpButtons.setLayout(new GridLayout(1,1,0,0));
    jpButtons.setBackground(Color.GRAY);

    jpButtons.add(AmendingTable.addingRowse(AddingRowsToJTable.table));

    return jpButtons;
}
于 2019-02-15T05:38:11.180 回答