简体   繁体   English

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

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

I have been trying to pass the info of my JTextField that is in a JDialog into my JFrame. 我一直试图将JDialog中的JTextField的信息传递到JFrame中。 Both the JDialog and JFrame are in separate classes. JDialog和JFrame都在单独的类中。 I have tried to store the JTextField into a JLable using the .setText and .getText and then passing the JLable into the JFrame but with no luck. 我试图使用.setText和.getText将JTextField存储到JLable中,然后将JLable传递到JFrame中,但是没有运气。

I know there are many similar questions but I have tried many different approaches but still no luck. 我知道有很多类似的问题,但是我尝试了许多不同的方法,但是仍然没有运气。 I am relatively new to Java and do not know all the in's and out's. 我对Java还是比较陌生,不知道所有的来龙去脉。 Any help is very appreciated! 任何帮助都非常感谢!

My code for the JFrame: 我的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);
    }
}

And Dialog: 和对话框:

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

I am trying to pass the stockName and stockPrice variables from the JDialog into the JFrame. 我正在尝试将JDialog的stockName和stockPrice变量传递给JFrame。 I then want the name and price to display at the top of the JFrame. 然后,我希望名称和价格显示在JFrame的顶部。

For demonstration, what the problem is, we need less Fields and Buttons. 为了说明问题所在,我们需要的字段和按钮更少。

So far, no component of StockApp needs to be accessed from different methods, so there is no need to make them visible outside of the ctor. 到目前为止,不需要通过不同的方法来访问StockApp的组件,因此无需在ctor外部使其可见。

More explanations in the code. 代码中的更多说明。

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