2

对你来说很基本的问题。我是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.");

            }

        }
4

1 回答 1

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

爪哇文档:

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

于 2013-04-24T13:58:28.943 回答