好吧,所以我有一个难题。我想知道是否可以对两个文本区域执行以下操作:
I.打印第一个文本区域,仅打印第一个文本区域。
二。关闭第一个文本区域后,使其显示第二个文本区域。
三,这样做是为了使两个文本区域不会同时出现。
这是我的代码,对于所有评论,我们深感抱歉,我必须将此项目教给同学:
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()
方法主体。最好在构造中而不是在
JFrame
中为main
设置所有操作。我已经相应地修改了代码。另外,使用SwingUtilities
运行swing应用程序也是一个好习惯。SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ActionProgram().setVisible(true);
}
});
如有任何疑问,请随时发表评论。