Java在一个Jframe中获取输入字符串并在另一个Jframe中显示

Java在一个Jframe中获取输入字符串并在另一个Jframe中显示,java,swing,Java,Swing,我必须在一个JFrame中获取字符串输入,并在另一个JFrame中显示。 我的第二个任务是在第二帧中以1秒的间隔以较大的字体闪烁给定的字符串。 如何进行 import java.awt.*; import java.awt.event.*; import javax.swing.*; class Input{ String hinput; private JFrame mainFrame; private JLabel headerLabel; private

我必须在一个JFrame中获取字符串输入,并在另一个JFrame中显示。 我的第二个任务是在第二帧中以1秒的间隔以较大的字体闪烁给定的字符串。 如何进行

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Input{

    String hinput; 
    private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;

    private void prepareGUI(){
         mainFrame = new JFrame("STRING");
         mainFrame.setSize(500,100); 
         headerLabel = new JLabel("", JLabel.CENTER);        
         controlPanel = new JPanel();
         controlPanel.setLayout(new FlowLayout());
         mainFrame.add(headerLabel);
         mainFrame.add(controlPanel);
         mainFrame.setVisible(true);  
    }

    private void showTextField(){

         JLabel  stringlabel= new JLabel("String ", JLabel.RIGHT);
         final JTextField userText = new JTextField(20);    
         JButton submitButton = new JButton("Submit");
         submitButton.addActionListener(new mylistener());
         submitButton.setActionCommand("open");
         controlPanel.add(stringlabel);
         controlPanel.add(userText);
         controlPanel.add(submitButton);
         mainFrame.setVisible(true);  

    }
    private class mylistener implements ActionListener{
        public void actionPerformed(ActionEvent e){
        String cmd = e.getActionCommand();
        if(cmd.equals("open")){
            mainFrame.dispose();
            NewJFrame nj= new NewJFrame(hinput);
        }
        }
    }
    public static void main(String args[]){
         Input  Inp = new Input();      
         Inp.prepareGUI();
         Inp.showTextField();
    }
}
class NewJFrame{

   JFrame mainFrame;
   String text;
   JLabel l1;
   JTextField tb1;
   public NewJFrame(String t){

          text=t;
          mainFrame=new JFrame("STRING");
          mainFrame.setSize(800,800);

          mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          l1=new JLabel("Entered string");
          tb1.setText(text);
          mainFrame.add(l1);
          mainFrame.add(tb1);
          mainFrame.setVisible(true);
   }
}    
单击“提交”按钮后,我将得到回溯。
请指出错误。

您可以通过在NewJFrame类中安装tb1来消除错误,如下所示:

class NewJFrame{

   JFrame mainFrame;
   String text;
   JLabel l1;
   JTextField tb1;
   public NewJFrame(String t){

          text=t;
          mainFrame=new JFrame("STRING");
          mainFrame.setSize(800,800);

          // *** must init tb1!!! ***///
          JTextField tb1 = new JTextField();

          mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          l1=new JLabel("Entered string");
          tb1.setText(text);
          mainFrame.add(l1);
          mainFrame.add(tb1);
          mainFrame.setVisible(true);
   }
} 
至于让在一个JFrame中键入的文本在另一个JFrame中打开,我有一个稍微修改的解决方案。可能在另一个JPanel的一个JPanel显示屏上的JTextField中输入了文本。为此,可以使用以下代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;

public class SimpleGUI extends JFrame {

    private final JPanel firstPanel;
    private final JPanel secondPanel;
    private final JButton submitButton;
    private final JTextField textField;
    private final JLabel secondPanelLabel;

    public SimpleGUI() {

        // sets the title of the JFrame
        super("SimpleGUI");

        setLayout(new FlowLayout());

        // inits both JPanels
        firstPanel = new JPanel();
        secondPanel = new JPanel();

        // inits empty second JLabel and adds to the secondPanel
        secondPanelLabel = new JLabel();
        secondPanel.add(secondPanelLabel);

        // makes the secondPanel invisible for the time being
        secondPanel.setVisible(false);

        // inits the submit button
        submitButton = new JButton("Submit");

        // event-handler for submit button, will set the text in the
        // secondPanelLabel to the text in the JTextField the user types
        // into. It then makes the firstPanel (with the text field and button),
        // invisible, and then makes the second panel visible.
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                secondPanelLabel.setText(textField.getText());
                firstPanel.setVisible(false);
                secondPanel.setVisible(true);
            }
        });

        // inits the textField
        textField = new JTextField(10);

        // adds the button and the text field to the firstPanel
        firstPanel.add(submitButton);
        firstPanel.add(textField);

        // adds both panels to this JFrame
        this.add(firstPanel);
        this.add(secondPanel);
    }
}
下面是一个具有main方法的类,它构造了SimpleGUI,因此您可以自己测试它:

import javax.swing.JFrame;

public class SimpleGUITest {
    public static void main(String[] args) {
        SimpleGUI frame = new SimpleGUI();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}

初始化。tb1删除了回溯。但是第二帧中没有显示任何内容。是的,我不知道如何在代码中实现第二帧。你有没有看过我的建议,看看你能不能从中得到些什么?如果我是你,我不会用两个JFrames…是的,那就行了。非常感谢。你可以使用
JDialog
JOptionPane
它可能会让你的生活更轻松