- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,所以我陷入了困境。我想知道是否有办法对两个文本区域执行以下操作:
我。打印第一个文本区域,并且仅打印第一个文本区域。二.关闭第一个文本区域后,让第二个文本区域出现。三.这样做可以使两个文本区域不会同时出现。
这是我的代码,对所有评论表示抱歉,我必须将其教授给项目中的同学:
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/
有没有更好的方法用 PHP 将数据输出到 html 页面? 如果我想在 php 中用一些 var 制作一个 div,我会写类似的东西 print (''.$var.''); 或 echo "''.$v
我可以使用 java awt print 来打印文档/文件而不是使用 javax print 吗?我发现在 java awt print 中有一个选项可以使用 AttributedString 将内容
目前我通过以下方式运行 R 脚本: R --slave argument1 argument2 ... 我想知道 R 中关于如何退出脚本并发出警告的最佳实践,q() 会这样做吗? if(!file.
谁能告诉我如何编写一个程序,用 gcc 编译时打印 c ,用 g++ 编译时打印 c++? 最佳答案 #ifdef __cplusplus printf("c++\n"); #else
我需要支持在 KitKat 设备上打印,但我的目标 SDK 是 13(无法更改)。 特别是我需要打印一个 webview。 这是用于打印 webview 的 API: http://developer
我正在尝试创建一个简单的函数,其中 python 将根据您的年份输入计算年龄。我已经尝试了几种方法,但我没有运气 atm。 附:对不起,我是新手。 ame = input(" Enter your n
JavaFX 2.0 是否支持打印?我有一个文本区域,我从中获取文本然后我想打印它,但似乎没有这个功能。 当然,这里我说的是打印到打印机。 :) 最佳答案 尚不支持。作为一种解决方法,您可以使用 Ja
我试图找出printOn的重点。我查看了一些实现它的类,看起来它只是帮助打印不同数据类型的单位。这是准确的吗? 如果是这样,有人能指出我如何为我自己的类(class)实现这一点的正确方向吗?我将在可能
我无法让 IE 打印我的 Canvas (使用 excanvas 生成)...我使用的是最新版本的 excanvas。 http://dl.dropbox.com/u/997831/canvas.ht
我搜索了很多但没有人回答我的问题,我读到在这样的信号处理程序中使用 cout 是不安全的: void ctrlZHandler(int sig_num) { //SIGTSTP-18
我有兴趣打印一系列查询。我有以下代码。 start = datetime.datetime(2012, 2, 2, 6, 35, 6, 764) end = datetime.datetime(201
public class javaClass { public static void main(String [] arg) { String row1 = "A____A"
我需要写入前一行的命令,例如不带\n 的 print()。 下面是一些示例代码: a=0 print("Random string value") if a==0: print_to_prev
我有一个使用 UIKit 和 Objective C 的旧 iOS 应用程序,我目前正在将其移植到 SwiftUI 和 Swift。一切都很顺利,我喜欢 Swift 和 SwiftUI。该应用程序已经
我创建了一个求和函数,它接受一个开始编号和一个结束编号,并返回这两点之间的总和答案 def print_sum_equations(start_number,end_number):
在 Perl 6 中,print 和有什么区别? , put和 say ? 我怎么看 print 5不同,但 put 5和 say 5看起来一样。 最佳答案 put $a就像 print $a.Str
我正在使用 here 中的 getOrgChart 库,我正在尝试打印整个图表,而不仅仅是可见部分。不幸的是,当使用标准库打印功能时,它只会打印出第一部分,而我不知道如何打印整个图表(该图表相当宽,大
我制作了一个非常适合 A4 页面的 View 。现在我想打印它。请注意,我没有使用drawRect或类似的东西,只是一个带有 subview 和文本标签的普通 View 。我的问题是,我对该 View
由于 Cocoa-Java 已弃用,我正在将 Cocoa-Java 代码迁移到 Cocoa + JNI。该代码打印存储在文件中的图像。新的 Cocoa 代码基本上是: NSImage *image =
这个问题已经有答案了: Printing a TDBGrid (4 个回答) 已关闭 6 年前。 如何在不安装或下载组件的情况下打印 DBGrid? 或者 如何将 DBGrid 的数据放入 RichE
我是一名优秀的程序员,十分优秀!