且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将值从jtext传递到另一帧

更新时间:2023-10-17 21:41:46

该问题的要求仍然不清楚,尽管如果您能够对此有所了解,那么我们中的许多人都可以在此方面为您提供帮助.话题.寻求 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.