簡體   English   中英

如何在Java中按順序打印多個文本區域?

[英]How do you print multiple text areas in an orderly manner in Java?

好吧,所以我有一個難題。 我想知道是否可以對兩個文本區域執行以下操作:

I.打印第一個文本區域,僅打印第一個文本區域。 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);

}

}
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()方法主體。

最好在構造中而不是在mainJFrame設置所有動作。 我已經相應地修改了代碼。 另外,使用SwingUtilities運行Swing應用程序也是一個好習慣。

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

        new ActionProgram().setVisible(true);

    }
});

如有任何疑問,請隨時發表評論。

除非將JTextArea包裝在JScrollPane中,然后將JScrollPan添加到JPanel,否則您無法關閉JTextArea。

如果您能夠執行所有這些步驟,則可以使用FocusListener ,該焦點FocusListener器用於將焦點從一個組件轉移到另一個組件。

您可以在此處閱讀有關它們的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM