将值从jtext传递到另一个帧

时间:2014-07-03 11:18:52

标签: java swing jdbc jtable jbutton

我必须写一个收银员的计划。假设,如果我有JFrame包含2 JTextField s,则添加商品种类和价格。我有一个JButton,如果我希望用户添加的项目与新JButton中的JFrame类似,那么当我按下该项时JButton会将价格和商品种类的值传递给JTable。有没有办法实现这个!!!!

从数据库中获取项目的代码,但结果在JTable但我不知道要创建的代码JButton

try {    
    String host = "jdbc:derby://localhost:1527/PROCAT";
    String uName = "zain";
    String uPass = "zain";
    Connection con = DriverManager.getConnection( host, uName, uPass);
    String sql = "Select * from ITEMB where ITEM =2";
    stmt = con.createStatement();

    rs = stmt.executeQuery(sql);

    while (rs.next()) {
        String myNamatxt = rs.getString("PRISE");
        String myHargatxt = rs.getString("ITEMNAME");
        String satuan = rs.getString("ITEM");
        String[] data = {myNamatxt, myHargatxt, satuan,};
        tabMode.addRow(data);
        double price = Double.parseDouble(rs.getString("prise"));
        totalpay = price + totalpay;
        ++rowCount;
    }
} catch (Exception e) {
    //ignore
}   
jTextField3.setText(String.valueOf(totalpay));   

1 个答案:

答案 0 :(得分:2)

问题的要求仍然不明确,但如果你能够更多地了解它,那么我们中的许多人都能够帮助你解决这个问题。在Google Translate的帮助下,以防万一英语不是您熟悉的语言。

如果我理解正确意义上的问题,您希望在两个字段中输入两个值, ITEM和PRICE 。现在点击JButton,您希望将这些项目传递给JTable。如果是这种情况,请帮助您的小程序如下:

import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class JTableExample {

    private JFrame frame;
    private JTextField itemTField;
    private JFormattedTextField priceFTField;
    private NumberFormat priceFormat;
    private JButton submitButton;

    private Vector<String> columnNames = new Vector<String>();
    private JDialog sellingDialog;
    private JTable table;
    private DefaultTableModel model;

    private ActionListener buttonAction = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            if (!sellingDialog.isShowing()) {
                model.setRowCount(0);
                sellingDialog.setVisible(true);
            }
            Vector<Object> rowData = new Vector<Object>(2);
            String item = itemTField.getText().trim();
            double price = Double.parseDouble(
                            priceFTField.getText().trim());
            rowData.add(item);
            rowData.add(new Double(price));
            model.addRow(rowData);
        }
    };

    public JTableExample() {
        columnNames.add("Item");
        columnNames.add("Price");
    }

    private void displayGUI() {
        frame = new JFrame("Swing Worker Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new GridLayout(2, 1, 5, 5));

        JPanel headerPanel = new JPanel();
        itemTField = new JTextField(10);
        priceFormat = NumberFormat.getInstance();
        priceFormat.setMaximumFractionDigits(2);
        priceFTField = new JFormattedTextField(priceFormat);
        priceFTField.setColumns(10);
        headerPanel.add(itemTField);
        headerPanel.add(priceFTField);

        JPanel footerPanel = new JPanel();
        submitButton = new JButton("SUBMIT");
        submitButton.addActionListener(buttonAction);
        footerPanel.add(submitButton);

        contentPane.add(headerPanel);
        contentPane.add(footerPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        createDialog();
    }

    private void createDialog() {
        sellingDialog = new JDialog(frame, "Sell Items: ", false);
        JPanel contentPane = new JPanel(new BorderLayout(5, 5));
        model = new DefaultTableModel();
        model.setColumnIdentifiers(columnNames);
        table = new JTable(model);
        table.setPreferredScrollableViewportSize(
                                    new Dimension(200, 200));
        table.setFillsViewportHeight(true);
        JScrollPane itemScroller = new JScrollPane();
        itemScroller.setViewportView(table);
        contentPane.add(itemScroller);

        sellingDialog.setContentPane(contentPane);
        sellingDialog.pack();
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new JTableExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

虽然在单个应用程序中注释restrain yourself from using multiple JFrame,但请考虑浏览已在评论中发布的链接。