2012-02-26 5 views
1

独自のカスタマイズされたフォームを作成できるプログラムを作成しようとしています。 1つのJFrameには、ユーザがフォーム上にある質問を入力できるようにするコードがあります。他のJFrameでは、実際のフォームがどのように見えるかをプリントアウトしたいと思っています。異なるJFrameのLayoutManagerに関する問題

私はこのように私のコードを持っている場合、レイアウトマネージャが正常に動作させることはできません:私はJButtonのを押し

public class WinMaker implements ActionListener, ItemListener { 

// Define variables 
public JLabel title; 
public JButton submit; 
public JButton create; 
public JTextField question1; 
public String q1; 
public JTextField question2; 
public JTextField question3; 
public JTextField question4; 
public JTextField question5; 
public JTextField answer1; 
public JTextField answer2; 
public JTextField answer3; 
public JTextField answer4; 
public JTextField answer5; 
public JLabel response1; 
public JComboBox questions; 
String[] question = { "1", "2", "3", "4", "5" }; 
JFrame j = new JFrame(); 
JFrame f = new JFrame(); 

public WinMaker() { 

    title = new JLabel(
      "Select the # of questions in the form and write your questions in the space below:"); 

    questions = new JComboBox(question); 
    questions.setSelectedIndex(4); 
    questions.addItemListener(this); 

    question1 = new JTextField(30); 
    question2 = new JTextField(30); 
    question3 = new JTextField(30); 
    question4 = new JTextField(30); 
    question5 = new JTextField(30); 

    answer1 = new JTextField(15); 
    answer2 = new JTextField(15); 
    answer3 = new JTextField(15); 
    answer4 = new JTextField(15); 
    answer5 = new JTextField(15); 

    create = new JButton("Create"); 
    create.setFont(new Font("Tahoma", Font.BOLD, 18)); 
    create.setBackground(Color.red); 
    create.addActionListener(this); 

    submit = new JButton("Submit"); // create JButton 
    submit.addActionListener(this); // add actionlistener to JButton 
      // Create layout 
    j.setLayout(new GridLayout(8, 1)); 
    j.getContentPane(); 
    j.add(title); 
    j.add(questions); //JComboBox 

    j.add(question1); 

    q1 = question1.getText(); //I'm trying to get the text in the textfield and    
           store it in a string so I can print it out on the 
           next JFrame 
    response1 = new JLabel(q1); 

    j.add(question2); //textfield 
    j.add(question3); 
    j.add(question4); 
    j.add(question5); 
    j.add(create); //create button 

    j.setSize(300, 300); // dimensions of JFrame 
    j.setTitle("WinMaker"); // title of JFrame 
    j.setLocationRelativeTo(null); 
    j.setResizable(true); 
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    j.setVisible(true); 


    f.setLayout(new FlowLayout()); 
    f.getContentPane(); 

    f.add(response1); //text from string won't display 
    f.add(answer1); 

    f.add(question2); 
    f.add(answer2); 

    f.add(question3); 
    f.add(answer3); 

    f.add(question4); 
    f.add(answer4); 

    f.add(question5); 
    f.add(answer5); 

    f.setSize(300, 300); // dimensions of JFrame f.setTitle("WinMaker"); 
    f.setTitle("WinMaker Form"); // title of JFrame 
    f.setLocationRelativeTo(null); 
    f.setResizable(true); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setVisible(false); 
    } 

    public static void main(String[] args) { 
    new WinMaker(); 
    } 
} 

たら私は別のJFrameがユーザーを疑問視ディスプレイが中に入ったことを起動します最初のJFrame何かアドバイス?

+1

あなたが提示した情報をもとに、あなたの質問は非常に難解です。あなたのプログラムが何をしているのかわからないかのように、たとえ投稿されていないコードやあなたのコードを実行できないようなコードが見えないかのように質問してみてください。あなたのコードを一度も見たことのない誰かの私たちの立場からあなたの質問を再読し、あなたが私の言いたいことを知っているでしょう。 –

+0

問題を説明している[sscce](http://sscce.org/)を含むように質問を編集してください。 – trashgod

+0

確かに、それについて申し訳ありません。私の質問です:これは、このプログラムにアプローチする最善の方法ですか?最初のJFrameには、ユーザーが調査に必要な質問を書くためのテキストフィールドがあります。次にJButtonを押すと、別のJFrameが起動され、これらの質問が印刷されます。JTextfieldsはアンケートに回答する人のためのものです。 –

答えて

1

私が見ている問題の1つは、GUIクラスのコンストラクタのJTextFieldからテキストを取得しようとしていることです。これは、ユーザーがテキストを入力するための時間がある前に行われるので、(あなたがやっているとは思わないものをあらかじめ入力しない限り)機能しないはずです。 JTextFieldまたはJButtonにActionListenersを追加し、ユーザーが入力してJButtonを押した後、またはコンストラクタではなくJTextFieldのトリガーリスナーで<Enter>を押した後に、JTextFieldからテキストを取得する必要があります。 Oracle Java Swingのチュートリアルでは、これを行う方法を説明します。

編集
あなたのコメントについて:最初のJFrameで

私は、ユーザーがアンケートにする必要があります質問を書き込み、テキストフィールドを持っています。次にJButtonを押すと、別のJFrameが起動され、これらの質問が印刷されます。JTextfieldsはアンケートに回答する人のためのものです。

これは「どのようにすればよいの?

物事は問題であり、それがどのような情報を保持しないだけで何カプセル化するために、非GUI質問クラスを作成しています...

  • 含め検討します。
  • 質問オブジェクトを作成する目的でGUIを作成し、おそらくArrayListに入れます。
  • 質問をファイルに書き込むためのクラスを作成し、質問をファイルから読み込むこともできます。
  • 次に、質問を表示するためのコードを作成することを検討します。私はこのコードをJFrameではなくJPanelを作成する方向に向ける。このようにして、JDialog、JFrame、またはJAppletに表示することができます。
  • 質問はコードとは別に保存されるため、レポートソフトウェアの使用を含め、どのように表示するかは自由に選択できます。
0

JFramesが複数ある場合、すべてのSwingコンポーネントをイベントディスパッチスレッドに保持することは困難です。スイングコンポーネントはスレッドセーフではありません。

ホバークラフト・フルズ・オブ・エルスが答えたところでは、Java SwingアプリケーションにはJFrameという唯一のものがあります。 JFrame内には、GUIを作成するのに必要な数だけJPanelsを置くことができます。