2012-02-26 60 views
1

我正在嘗試創建一個程序,允許您創建自己的自定義表單。在一個JFrame中,我有我的代碼,它允許用戶輸入將在表單上的問題。在另一個JFrame中,我希望能夠打印出實際的外觀。LayoutManager在不同JFrame中的問題

我不能讓我有我的代碼,這樣的佈局管理器才能正常工作:

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

一旦我按了一個JButton,我想另一個JFrame的推出是一種質疑的用戶將顯示在已進入第一個JFrame。有什麼建議?

+1

鑑於您提供的信息,我很難回答您的問題。請嘗試提問您的問題,好像我們完全不知道您的程序做了什麼或應該做什麼,就好像我們看不到未發佈的代碼並且無法運行您的代碼。從我們其中一個人的角度重新閱讀您的問題,作爲以前從未見過您的代碼的人,並且您會明白我的意思。 – 2012-02-26 20:54:00

+0

請修改您的問題以包含展示您描述的問題的[sscce](http://sscce.org/)。 – trashgod 2012-02-26 20:59:10

+0

是的,對不起。我的問題是:這是處理這個程序的最好方法嗎?在第一個JFrame中,我有文本框,用戶在這些文本框中寫下應該在調查中出現的問題。然後,當JButton被按下時,它會啓動另一個JFrame,打印出這些問題以及JTextfields,以供調查人員回答。 – 2012-02-26 21:06:17

回答

1

我看到的一個問題是,您正嘗試從GUI類的構造函數中的JTextField中獲取文本。這是在用戶有任何時間輸入文本之前,所以不應該工作(除非你預先填寫它,我沒有看到你在做什麼)。您需要將ActionListeners添加到JTextField或JButton中,並在用戶輸入它並按下JButton或在JTextField觸發偵聽器中按下JTextField中的文本,而不是在構造函數中。 Oracle Java Swing教程將向您展示如何執行此操作。

編輯
關於你的評論:

在第一的JFrame我有文本框,其中用戶寫道,應在調查中的問題。然後,當JButton被按下時,它會啓動另一個JFrame,打印出這些問題以及JTextfields,以供調查人員回答。

這引出了一個問題:「以什麼方式更好?」。

需要考慮的事項包括...

  • 創建非GUI問題類來封裝究竟什麼是一個問題,它持有什麼樣的信息。
  • 我會創建一個GUI,目的是創建Question對象,也許把它們放在一個ArrayList中。
  • 我會創建一個用於將問題寫入文件並從文件中讀取問題的類。
  • 然後,我會考慮創建顯示問題的代碼。我想這個代碼創建一個JPanel而不是一個JFrame。這樣我可以根據需要將它顯示在一個JDialog或JFrame或JApplet中。
  • 由於問題與代碼分開存儲,您可以選擇以任何您認爲合適的方式顯示它們,包括使用報告軟件。
0

當您有多個JFrames時,很難將所有Swing組件保留在事件派發線程中。 Swing組件不是線程安全的。

作爲氣墊船充滿鰻魚在他的回答中說,Java Swing應用程序有一個且只有一個JFrame。在JFrame之內,您可以擁有儘可能多的JPanels來創建GUI。