【问题标题】:How to pass JTextfield info from a JDialog into a JFrame (separate classes)如何将 JTextfield 信息从 JDialog 传递到 JFrame(单独的类)
【发布时间】:2018-03-06 12:47:06
【问题描述】:

我一直在尝试将 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();
    }
}    

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

【问题讨论】:

  • a) 创建并显示对话框。 b) 使用 getter 检索值 c) 创建框架并设置这些值?
  • 嗨,我试过了,得到了以下错误。线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException
  • 没有 main 方法来启动你的程序,所以每个愿意帮助的人都必须自己编写。然后有太多的元素,不需要证明你的问题。将您的问题减少到最低限度以重现它。

标签: java variables jframe jdialog


【解决方案1】:

为了演示,问题是什么,我们需要更少的字段和按钮。

到目前为止,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();
        }
    }
}

【讨论】:

  • 我建议您在将所有组件添加到“主”组件后移动setVisible() 调用。你可能会发现一些“竞争条件”可能会导致奇怪的绘画错误......并且还要避免extending JFrame其余的我想我不知道......
  • @Frakcool:这是一个演示如何将值从一个组件传递到另一个组件,而不是完整的 Swing 教程。几乎没有什么是无法改进的,但你必须有点切中要害,不要让话题杂乱无章。画虫子不是这里的主题。
  • 虽然这是一个演示,但它应该遵守规则,因此我们没有其他关于“绘画错误”的帖子与此代码相关。我不是想用噪音来混淆这个话题,而是提供一个更高质量的答案。就是这样。您可以将这些编辑添加到您的答案中,或者至少我将 2 美分留在这里,以便 OP 阅读您当前代码中可以改进的地方。
  • @Frakcool:那么你指的是什么规则?这个demo,好用还是不好用?你的问题跑题了。它头脑很好,但有货物崇拜编程的味道。虽然在某些情况下,延迟 'setVisible' 可能会有所帮助,但在这里它不是,直到你能证明它。如果“setVisible”有问题,为什么 Javadocs 中没有提示?无论是权威来源,还是经验证明。
  • Java Docs 显示步骤,其中setVisible 是最后完成的。 “你的问题跑题了” 什么问题?我不确定 “cargo-cult-programming” 术语是什么意思。无论如何,我不打算与一个心胸狭窄的人讨论,他会因为他的回答中的一条评论而生气。对不起,如果我冒犯了你。这不是我的本意。正如我所说,我只是想说明您的代码可以改进。就是这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
相关资源
最近更新 更多