如何在Java中以有序的方式打印多个文本区域?

时间:2015-06-01 21:41:52

标签: java jtextarea

好吧,所以我陷入两难境地。我想知道我是否有办法在两个文本区域中执行以下操作:

予。打印第一个文本区域,仅打印第一个文本区域。 II。关闭第一个文本区域后,请显示第二个文本区域。 III。这样做是为了使两个文本区域不会同时出现。

这是我的代码,对所有的评论感到抱歉,我必须教给同学一个项目:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; //Used for events and the Action Listener

public class ActionProgram extends JFrame /*"extends JFrame" will extend the frame into the variable used to call the class*/
{
//Declare fields (Do not require public/private identification)
JTextArea area;
JLabel instructions;
JLabel question;
JLabel ask;
JButton submitt;
JScrollPane sp;

//Create a constructor to start applying these variables in the creation of the Text Area
public ActionProgram()
{
    //Create the flow layout
        setLayout(new FlowLayout());

    //Create the text area and set how long and wide it should be. Add it to the frame
    area = new JTextArea(10,30);
    add(area);

    //Create scroll pane and add it to the frame
    sp = new JScrollPane(area);
    add(sp);

    //Set the line wrap and word wrap to true for the frame
    area.setLineWrap(true);
    area.setWrapStyleWord(true);

    //Create submitt button, and add it to the frame
    submitt = new JButton ("Submitt");
    add(submitt);

    //Create label asking user to answer the question and add it to the frame
    instructions = new JLabel("Please Answer the Following Question:");
    add(instructions);

    //Create label for the question and add it to the frame
    question = new JLabel("-----Briefly Describe how to print something in java-----");
    add(question);

    //Create label telling user what to do when finished, and add it to the frame
    ask = new JLabel("Please enter Submitt when you have finished");
    add(ask);


    //As you can tell, we do not need to put all these parts into the frame, for the class puts it all into the variable calling it

    /*In order for the program to take what the user has writen into text area and make it an input, we have to create an
    Action Listener. An Action Listener is a piece of code that will do a specific event when an action is done. In this case
    we need the action listener to respond when the user presses "Submitt". To do this, we need to create an event class*/

    //This will call the action listener class
    event action = new event();

    //This will add the action listener to the submitt button
    submitt.addActionListener(action);

}
/*The class called event will create the aciton listener. There are two different methods for the action event, the action listener
and the action performer. The action performer is the method used to create code when the action listener is activated. The listener
waits for the submitt button to be pressed, and the performer does user-inputed code when the button is pressed*/
public class event implements ActionListener
{
    public void actionPerformed(ActionEvent e) //We use a nested method to create the action performer
    {
        /*The following code is what the performer will do when the listner is activated. It will get the text typed in the
        text area when the user hits the submitt button. The performer will then print the text obtained and close the text area*/
        String text = area.getText();
        System.out.println(text);

        //Use System.exit(0) to close the text area
        System.exit(0);
    }
}
public static void main(String[] args)
{
            //Call the class like you usually do, and set a variable to it
            ActionProgram display = new ActionProgram();

            //display also acts as the frame of the text area since the class set it equal to the JFrame
            display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Set the length and width of the text area in pixels
            display.setSize(500,300);

            //Set it so the text area can be seen
            display.setVisible(true);

}

}

2 个答案:

答案 0 :(得分:1)

MeasureOverride of element 'Xamarin.Forms.Platform.WinPhone.ViewToRendererConverter+WrapperControl' should not return PositiveInfinity or Nan as its DesiredSize.

答案的基础是假设您要提出5个问题。请更改import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //Used for events and the Action Listener import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class ActionProgram extends JFrame /* * "extends JFrame" will extend the * frame into the variable used to call * the class */ { // Declare fields (Do not require public/private identification) JTextArea area; JLabel instructions; JLabel question; JLabel ask; JButton submitt; JScrollPane sp; final int TOTAL_QUESTIONS = 5; // assuming you have 5 questions int quizCounter = 0; String[] quizQuestions; // Create a constructor to start applying these variables in the creation of // the Text Area public ActionProgram() { // Create the flow layout setLayout(new FlowLayout()); // display also acts as the frame of the text area since the class set // it equal to the JFrame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the length and width of the text area in pixels setSize(500, 300); // Create the text area and set how long and wide it should be. Add it // to the frame area = new JTextArea(10, 30); add(area); // Create scroll pane and add it to the frame sp = new JScrollPane(area); add(sp); // Set the line wrap and word wrap to true for the frame area.setLineWrap(true); area.setWrapStyleWord(true); // Create submitt button, and add it to the frame submitt = new JButton("Submit"); add(submitt); // Create label asking user to answer the question and add it to the // frame instructions = new JLabel("Please Answer the Following Question:"); add(instructions); // Create label for the question and add it to the frame quizQuestions = questions(); question = new JLabel(quizQuestions[quizCounter]); add(question); // Create label telling user what to do when finished, and add it to the // frame ask = new JLabel("Please enter Submit when you have finished"); add(ask); // As you can tell, we do not need to put all these parts into the // frame, for the class puts it all into the variable calling it /* * In order for the program to take what the user has writen into text * area and make it an input, we have to create an Action Listener. An * Action Listener is a piece of code that will do a specific event when * an action is done. In this case we need the action listener to * respond when the user presses "Submitt". To do this, we need to * create an event class */ // This will call the action listener class event action = new event(); // This will add the action listener to the submitt button submitt.addActionListener(action); } /* * The class called event will create the aciton listener. There are two * different methods for the action event, the action listener and the * action performer. The action performer is the method used to create code * when the action listener is activated. The listener waits for the submitt * button to be pressed, and the performer does user-inputed code when the * button is pressed */ public class event implements ActionListener { public void actionPerformed(ActionEvent e) // We use a nested method to // create the action // performer { /* * The following code is what the performer will do when the listner * is activated. It will get the text typed in the text area when * the user hits the submitt button. The performer will then print * the text obtained and close the text area */ if (e.getSource() == submitt) { String text = area.getText(); System.out.println(text); dispose(); // increment the amount of times questions were asked - i.e. the frame opened quizCounter++; if (quizCounter < TOTAL_QUESTIONS ) { quizQuestions = questions(); area.setText(""); question.setText(quizQuestions[quizCounter]); setVisible(true); } else { System.exit(0); } } } } public String[] questions() { String[] newQuestion = new String[TOTAL_QUESTIONS]; switch(quizCounter) { case 0: newQuestion[0] = "-----Briefly Describe how to print something in java-----"; break; case 1: newQuestion[1] = "----- Question 2 -------"; break; case 2: newQuestion[2] = "Question 3"; break; case 3: newQuestion[3] = "Question 4"; break; case 4: newQuestion[4] = "Question 5"; break; } return newQuestion; } public static void main(String[] args) { // Call the class like you usually do, and set a variable to it ActionProgram display = new ActionProgram(); // Set it so the text area can be seen display.setVisible(true); } } 变量的数量以符合您的条件。另外,不要忘记修改TOTAL_QUESTIONS方法正文。

最好为构建中的questions()设置所有操作,而不是JFrame。我已相应地修改了代码。此外,使用main来运行swing应用程序也是一个好习惯。

SwingUtilities

如果您有任何疑问,请随时发表评论。

答案 1 :(得分:0)

除非将JTextArea包装在JScrollPane中,然后将JScrollPan添加到JPanel中,否则无法关闭JTextArea。

如果您能够执行所有这些步骤,那么您可以使用FocusListener 用于将焦点从一个组件转移到另一个组件。

您可以阅读有关他们的更多信息here