2016-04-06 13 views
2

1つのJFrameでString入力を取得し、別のJFrameで表示する必要があります。 私の2番目の作業は、指定された文字列を2番目のフレームの1秒間隔で大きなフォントでフラッシュすることです。 続行するには?Javaは1つのJframeで入力文字列を受け取り、別のものに表示します

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

[送信]ボタンをクリックするとトレースバックが発生します。 エラーを指摘してください。

+0

'JDialog'や' JOptionPane'の中ではおそらくあなたの人生が楽になるでしょう – MadProgrammer

答えて

1

あなたがそうのようなあなたのNewJFrameクラスでTB1をinstatiatingことにより、エラーを取り除くことができます。

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

別に開くように1つのJFrameの中に入力したテキストを取得するためとして、私はわずかに変更されたソリューションを持っています。他のJPanel内の1つの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); 
    } 
} 

をそしてここであなたは自分自身でそれを試すことができるようにSimpleGUIを構築mainメソッドを持つクラスです:あなたが利用することができ

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); 
    } 
} 
+0

init。 tb1がトレースバックを削除しました。しかし、2番目のフレームには何も表示されません。 – Souvik

+0

ええ、私はあなたのコードで2番目のフレームを実装する方法を理解できませんでした。あなたは私の提案を見て、それから何かを使うことができるかどうかを見ましたか?私があなたの場合、私は2つのJFramesを使用しません... –

+0

はい、それは行います。どうもありがとう。 – Souvik

関連する問題