繁体   English   中英

如何将JTextfield信息从JDialog传递到JFrame(单独的类)

[英]How to pass JTextfield info from a JDialog into a JFrame (separate classes)

我一直试图将JDialog中的JTextField的信息传递到JFrame中。 JDialog和JFrame都在单独的类中。 我试图使用.setText和.getText将JTextField存储到JLable中,然后将JLable传递到JFrame中,但是没有运气。

我知道有很多类似的问题,但是我尝试了许多不同的方法,但是仍然没有运气。 我对Java还是比较陌生,不知道所有的来龙去脉。 任何帮助都非常感谢!

我的JFrame代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JPanel;

public class StockApp extends JFrame implements PropertyChangeListener {

    private JPanel main = new JPanel();
    private JPanel north = new JPanel();
    private JPanel center = new JPanel();
    private JPanel south = new JPanel();
    private JButton buyStock = new JButton("Buy Stock");
    private JButton sellStock = new JButton("Sell Stock");
    public TestTest variables = new TestTest();
    private JLabel stockNameNorth = new JLabel("Stock Name");
    private JLabel stockPriceNorth = new JLabel("Stock Price");
    String stockName = variables.getStockName();
    String stockPrice = variables.getStockPrice();

    public StockApp() {
        setTitle("StockApp");
        getContentPane().setBackground(Color.white);
        setSize(400,400);
        setLocation(500,200);
        setVisible(true);
        main.setLayout(new BorderLayout());
        north.setLayout(new FlowLayout());
        center.setLayout(new FlowLayout());
        south.setLayout(new FlowLayout());
        stockNameNorth.setText(stockName);
        stockPriceNorth.setText(stockPrice);
        add(main);
        north.add(stockNameNorth);
        north.add(stockPriceNorth);
        south.add(buyStock);
        south.add(sellStock);
        main.add(north, BorderLayout.NORTH);
        main.add(center, BorderLayout.CENTER);
        main.add(south, BorderLayout.SOUTH);
    }
}

和对话框:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestTest extends JDialog implements ActionListener {

private JPanel main = new JPanel();
    private JPanel north = new JPanel();
    private JPanel center = new JPanel();
    private  JPanel south = new JPanel();
    private JLabel stockNameLabel = new JLabel("Stock name: ");
    private JLabel stockPriceLabel = new JLabel("Stock price(£): ");
    private JTextField stockNameIn = new JTextField(5);
    private JTextField stockPriceIn = new JTextField(5);
    private JButton buttonOK = new JButton("OK");
    public JLabel stockPrice = new JLabel();
    public JLabel stockName = new JLabel();

    public TestTest() {
        getContentPane().setBackground(Color.white);
        setSize(400,400);
        setLocation(500,200);
        setModal(false);
        setVisible(true);
        getRootPane().setDefaultButton(buttonOK);
        main.setLayout(new BorderLayout());
        north.setLayout(new FlowLayout());
        center.setLayout(new FlowLayout());
        south.setLayout(new FlowLayout());
        add(main);
        north.add(stockNameLabel);
        north.add(stockNameIn);
        center.add(stockPriceLabel);
        center.add(stockPriceIn);
        south.add(buttonOK);
        main.add(north, BorderLayout.NORTH);
        main.add(center, BorderLayout.CENTER);
        main.add(south, BorderLayout.SOUTH);
        buttonOK.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonOK){
            stockName.setText(stockNameIn.getText());
            stockPrice.setText(stockPriceIn.getText());
            dispose();
            new StockApp();
            }
        }
    public String getStockName() {
        return stockNameIn.getText();
    }
    public String getStockPrice() {
        return stockPriceIn.getText();
    }
}    

我正在尝试将JDialog的stockName和stockPrice变量传递给JFrame。 然后,我希望名称和价格显示在JFrame的顶部。

为了说明问题所在,我们需要的字段和按钮更少。

到目前为止,不需要通过不同的方法来访问StockApp的组件,因此无需在ctor外部使其可见。

代码中的更多说明。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;

public class StockApp extends JFrame {

    public StockApp() {
        // move those unreferenced panels here, so we don't have to reason about them:
        JPanel main = new JPanel();
        JPanel north = new JPanel();
        JPanel center = new JPanel();
        JPanel south = new JPanel();
        // add price later, when name works
        JButton buyStock = new JButton("Buy Stock");
        JLabel stockNameNorth = new JLabel("Stock Name");

        // critical change: Make the label, which you like to update,
        // accessible by whom it should be updated:
        TestTest variables = new TestTest (stockNameNorth);

        setTitle ("StockApp");
        getContentPane().setBackground(Color.white);
        setSize (600,400);
        setLocation (500,200);
        setVisible (true);
        // make the close-frame action terminate the program:
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        main.setLayout (new BorderLayout());
        north.setLayout (new FlowLayout());
        center.setLayout (new FlowLayout());
        south.setLayout (new FlowLayout());
        add (main);
        north.add (stockNameNorth);
        south.add (buyStock);
        main.add (north, BorderLayout.NORTH);
        main.add (center, BorderLayout.CENTER);
        main.add (south, BorderLayout.SOUTH);
    }

    // Main method to start the damn thing
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new StockApp ();
            }
        });
    }
}

// no need to make this class public in a short test:   
class TestTest extends JDialog implements ActionListener {

    // this are elements, visible outside the construction phase,
    // we need to have access to from more than one method.
    // Make this important distinction visible to the reader: 
    JLabel name;
    JTextField stockNameIn = new JTextField (5);
    JButton buttonOK = new JButton ("OK");

    // add the JLabel to update to the ctor, so that it can't be forgotten
   // to be set
    public TestTest (JLabel pname) {
        // we copy the reference to the label, to have access to it in 
        // the actionPerformed method.
        name = pname;
        JPanel main = new JPanel();
        JPanel north = new JPanel();
        JPanel center = new JPanel();
        JPanel south = new JPanel();
        JLabel stockNameLabel = new JLabel ("Stock name: ");
        getContentPane().setBackground(Color.white);
        // different size/location than frame, so that they don't hide
        // each other completly
        setSize (400,600);
        setLocation (700,300);
        setModal (false);
        setVisible (true);
        getRootPane().setDefaultButton(buttonOK);
        main.setLayout (new BorderLayout());
        north.setLayout (new FlowLayout());
        center.setLayout (new FlowLayout());
        south.setLayout (new FlowLayout());
        add (main);
        north.add (stockNameLabel);
        north.add (stockNameIn);
        south.add (buttonOK);
        main.add (north, BorderLayout.NORTH);
        main.add (center, BorderLayout.CENTER);
        main.add (south, BorderLayout.SOUTH);
        buttonOK.addActionListener(this);
    }

    // here we need access to the button - was it the OK-Button, clicked?    
    // and the textfield stockNameIn, to read the text 
    // and the name field from the frame, to set the text 
    public void actionPerformed(ActionEvent e) {
        if (e.getSource () == buttonOK) {
            name.setText (stockNameIn.getText());
            dispose();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM