java - Java中如何有序打印多个文本区域?

标签 java jtextarea

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

我。打印第一个文本区域,并且仅打印第一个文本区域。 二.关闭第一个文本区域后,让第二个文本区域出现。 三.这样做可以使两个文本区域不会同时出现。

这是我的代码,对所有评论表示抱歉,我必须将其教授给项目中的同学:

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

}

}

最佳答案

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

    }

}

答案是假设您将提出 5 个问题。请更改 TOTAL_QUESTIONS 变量的数量以符合您的条件。另外,不要忘记修改 questions() 方法主体。

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

SwingUtilities.invokeLater(new Runnable() {
    public void run() {

        new ActionProgram().setVisible(true);

    }
});

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

关于java - Java中如何有序打印多个文本区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30583798/

相关文章:

java - next() 与 Single() 哪个有效?

java - 处理同一 Java 源代码中的不同 API 版本

java - Java 中的 JTextArea 监听器

java - JScrollPane 不起作用

java - 如何以哈佛引用格式将 JTable 中的值设置为文本区域

java - 从 Java 文件夹中导入我的类

java - Android SDK 如何与内部 GPS SDK 对话

java - 我们可以在 Servlet 中从 destroy() 方法调用 service() 方法吗?

java - 如何在 JTextArea 实例中为选定的文本设置粗体字体样式

java - 如何使 JTextArea 的末尾可编辑