将值从jtext传递到另一帧 [英] pass the value from jtext to another frame

查看:146
本文介绍了将值从jtext传递到另一帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个 Cashier's 程序.假设,如果我有一个包含2个JTextFieldJFrame,则要添加商品的种类和价格.我有一个JButton,如果我希望由用户添加的项目像JButton在新的JFrame中称为出售的项目,当我按下JButton会通过该项目时, 价格和项目类型的值设置为JTable.有没有办法实现这一目标!!!

i have to write a Cashier's program. Assuming that, if i have a JFrame that includes 2 JTextFields, to add the item kind and price. I have one JButton, to create if i want that item which is added by the user to be like JButton in new JFrame called Sell when i pressed that JButton will pass the value of price and item kind to JTable. Is there a way to achieve this!!!!

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

The code for getting the item from database and but the result in JTable but i dont know the code to create 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));   

推荐答案

该问题的要求仍然不清楚,尽管如果您能够对此有所了解,那么我们中的许多人都可以在此方面为您提供帮助.话题.寻求 Google翻译的帮助,以防万一英语不是您喜欢的语言.

The requirements of the question is still unclear, though if you be able to put some more light on it, then many of us be able to help you on the topic. Take the help of Google Translate, just in case English is not the language you comfortable with.

如果我以正确的方式理解了这个问题,那么您想在两个字段中输入两个值: ITEM和PRICE .现在单击JButton,您想将这些项目传递给JTable.如果是这样,一个小的程序为您提供帮助,如下所示:

If I understood the question in the right sense, you want to enter two values in two fields, ITEM and PRICE. Now at the click of a JButton, you wanted to pass those items to the JTable. If that be the case, a small program for your help is as below:

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

尽管附带说明,限制自己在单个应用程序中使用多个JFrame ,但请务必考虑通过评论中已经发布的链接.

Though on a side note, restrain yourself from using multiple JFrame in a single application, do consider going through the link as already posted in the comments.

这篇关于将值从jtext传递到另一帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆