2012-02-26 23 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。