java 如何在 JTextarea/JTextfield 中显示字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16190536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Display Strings in JTextarea/JTextfield
提问by JayD2208
Pretty basic question for you. I am new to Java, and so far have made some relatively simple Javascript programs. One particular favourite of mine is one I call The Perfect Insult, and I have decided to develop it properly, so I can say I've made a program.
对你来说非常基本的问题。我是Java新手,目前已经做了一些比较简单的Javascript程序。我最喜欢的是我称之为完美侮辱的一个,我决定正确地开发它,所以我可以说我已经制作了一个程序。
The core program isn't finished yet, but what I've done so far is enough. There will be some calculations to decide which response the computer chould give, but all the variables will remain the same. I just need to know how to print the code to a JTextArea or a JTextField rather that System.out
. Any help would be much appreciated :).
核心程序还没有完成,但我目前所做的已经足够了。会有一些计算来决定计算机应该给出哪种响应,但所有变量都将保持不变。我只需要知道如何将代码打印到 JTextArea 或 JTextField 而不是System.out
. 任何帮助将非常感激 :)。
import java.awt.Color;
import java.awt.Font;
import java.util.Random;
import java.util.Scanner;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JEditorPane;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class tpiCore {
public class rootInterface extends JFrame {
private JPanel contentPane;
private JTextField input;
public void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
rootInterface frame = new rootInterface();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public rootInterface() {
setBackground(Color.GREEN);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 550);
contentPane = new JPanel();
contentPane.setForeground(Color.GREEN);
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
input = new JTextField();
input.setFont(new Font("Lucida Console", Font.PLAIN, 11));
input.setBackground(Color.DARK_GRAY);
input.setBounds(10, 447, 414, 54);
contentPane.add(input);
input.setColumns(10);
JEditorPane dtrpnThePerfectInsult = new JEditorPane();
dtrpnThePerfectInsult.setForeground(Color.GREEN);
dtrpnThePerfectInsult.setBackground(Color.BLACK);
dtrpnThePerfectInsult.setFont(new Font("Lucida Console", Font.PLAIN, 20));
dtrpnThePerfectInsult.setText(" The Perfect Insult");
dtrpnThePerfectInsult.setBounds(10, 11, 414, 35);
contentPane.add(dtrpnThePerfectInsult);
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setBackground(Color.BLACK);
textArea.setFont(new Font("Lucida Console", Font.PLAIN, 13));
textArea.setForeground(Color.GREEN);
textArea.setBounds(10, 57, 414, 383);
contentPane.add(textArea);
}
}
public static void core(String args[]) {
String[] insults = new String[15];
insults[0] = "insult 0";
insults[1] = "insult 1";
insults[2] = "insult 2";
insults[3] = "insult 3";
insults[4] = "insult 4";
insults[5] = "insult 5";
insults[6] = "insult 6";
insults[7] = "insult 7";
insults[8] = "insult 8";
insults[9] = "insult 9";
insults[10] = "insult 10";
insults[11] = "insult 11";
insults[12] = "insult 12";
insults[13] = "insult 13";
insults[14] = "insult 14";
double count = 0;
System.out.println("Come on then, hit me with your best shot!");
while (count<10) {
String insult1 = insults[(new Random()).nextInt(insults.length)];
System.out.println(insult1);
Scanner input = new Scanner(System.in);
input.nextLine();
String response = input.toString();
count++;
}
System.out.println("Okay, I give in. You win.");
}
}
回答by Kent
JTextArea.setText(string)
JTextArea.append(string)
java doc:
java文档:
http://docs.oracle.com/javase/6/docs/api/javax/swing/JTextArea.html
http://docs.oracle.com/javase/6/docs/api/javax/swing/JTextArea.html