gpt4 book ai didi

java - 如何在 JTextarea/JTextfield 中显示字符串

转载 作者:行者123 更新时间:2023-12-02 07:50:07 25 4
gpt4 key购买 nike

对你来说这是一个非常基本的问题。我是Java新手,到目前为止已经制作了一些相对简单的Javascript程序。我特别喜欢的一个程序是我称之为“完美侮辱”的程序,我决定正确地开发它,所以我可以说我已经制作了一个程序。

核心程序还没有完成,但是我到目前为止所做的已经足够了。将进行一些计算来决定计算机应给出哪种响应,但所有变量将保持不变。我只需要知道如何将代码打印到 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.");

}

}

最佳答案

JTextArea.setText(string)
JTextArea.append(string)

Java 文档:

http://docs.oracle.com/javase/6/docs/api/javax/swing/JTextArea.html

关于java - 如何在 JTextarea/JTextfield 中显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16190536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com