如何制作 JTextArea?

问题描述

我正在尝试为我的输入创建一个 textArea,但它破坏了我的代码。它不再绘制字符或网格,我看到的只是右上角的一个小按钮。 Screenshot of output

这是我的 JTextArea 代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class DnD extends JPanel implements ActionListener {
    public static final long serialVersionUID = 1;
    public Scanner sc = new Scanner(system.in);
    public JFrame window = new JFrame("D&D");
    public ArrayList<Person> charactersList = new ArrayList<Person>();
    public ArrayList<Person> others = new ArrayList<Person>();
    public int colorR = 0;
    public int colorG = 0;
    public int colorB = 0;
    public JButton button;
    public JTextArea textArea;
    public String textInput = "";
    public void run(DnD dnd){
        button = new JButton("Enter");
        button.addActionListener(dnd);
        this.window.setSize(1280,700);
        this.window.setLocation(0,0); 
        this.window.setUndecorated(true);
        this.window.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        this.window.add(dnd);
        textArea = new JTextArea(20,1);
        textArea.setBounds(1180,100,20);
        button.setBounds(1205,20,50,25);
        this.window.add(button);
        this.window.add(textArea);
        this.window.setVisible(true);
        int turn = 0;
        SwingUtilities.updateComponentTreeUI(this.window);
        this.window.invalidate();
        this.window.validate();
        this.window.repaint();
        boolean going = true;
        Environment environment = new Environment();
        while(going){
            System.out.println("Entering new room? Enter a bool.");
            if(sc.nextBoolean()){
                charactersList = environment.refreshOrder(charactersList,sc,this);
                others = environment.renderRoom(others,this);
                this.repaint();
            }
            sc.nextLine();
            turn ++;
            System.out.print("Turn number " + turn + "\n");
            for(int currentPlayer = 0; currentPlayer < charactersList.size(); currentPlayer++){
                System.out.println(charactersList.get(currentPlayer).name + "'s turn");
                System.out.println("You have " + this.charactersList.get(currentPlayer).hp + " hp this turn.");
                charactersList.get(currentPlayer).Turn(sc,charactersList,this);
                this.window.repaint();
            }
        }
    }
    public void actionPerformed(ActionEvent e){
        String event = e.getActionCommand();
        if(event.equals("Enter")){
            this.textInput = textArea.getText();
        }
    }
    public void setup(Scanner sc){
        boolean going = true;
        int x; int y; String name; int size; int hp; int speed; int ac; int r; int g; int b;
        while(going){
            System.out.print("Name: ");
            name = sc.nextLine();
            System.out.print("HP: ");
            hp = sc.nextInt();
            System.out.print("X: ");
            x = sc.nextInt();
            System.out.print("Y: ");
            y = sc.nextInt();
            System.out.print("Size: ");
            size = sc.nextInt();
            System.out.print("Speed: ");
            speed = sc.nextInt();
            System.out.print("AC: ");
            ac = sc.nextInt();
            System.out.print("R: ");
            r = sc.nextInt();
            System.out.print("G: ");
            g = sc.nextInt();
            System.out.print("B: ");
            b = sc.nextInt();
            sc.nextLine();
            this.charactersList.add(new Person(x,y,name,size,hp,speed,new Color(r,g,b),ac));
            System.out.print("Add another character? Enter a boolean: ");
            going = sc.nextBoolean();
            sc.nextLine();
        }
    }
    public ArrayList<Person> setup(Scanner sc,ArrayList<Person> charactersLists){
        boolean going = true;
        int x; int y; String name; int size; int hp; int speed; int ac; int r; int g; int b;
        while(going){
            System.out.print("Name: ");
            name = sc.nextLine();
            System.out.print("HP: ");
            hp = sc.nextInt();
            System.out.print("X: ");
            x = sc.nextInt();
            System.out.print("Y: ");
            y = sc.nextInt();
            System.out.print("Size: ");
            size = sc.nextInt();
            System.out.print("Speed: ");
            speed = sc.nextInt();
            System.out.print("AC: ");
            ac = sc.nextInt();
            System.out.print("R: ");
            r = sc.nextInt();
            System.out.print("G: ");
            g = sc.nextInt();
            System.out.print("B: ");
            b = sc.nextInt();
            sc.nextLine();
            charactersLists.add(new Person(x,ac));
            System.out.print("Add another character? Enter a boolean: ");
            going = sc.nextBoolean();
            sc.nextLine();
        }
        return charactersLists;
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(new Color(colorR,colorG,colorB));
        g2d.fillRect(0,1280,700);
        g2d.setColor(new Color(255 - colorR,255 - colorG,255 - colorB));
        for(int i = 0; i < 1280; i += 10){
            g2d.setColor(new Color(255,255,255));
            if(i % 20 == 10){
                g2d.setColor(new Color(150,150,150));
            }
            g2d.drawLine(i,i,700);
        }
        for(int i = 0; i < 700; i += 10){
            g2d.setColor(new Color(255,150));
            }
            g2d.drawLine(0,i);
        }
        Person temp = null;
        for(int i = 0; i < this.charactersList.size(); i++){
            temp = charactersList.get(i);
            g2d.setColor(temp.color);
            g2d.fillRect(temp.x,temp.y,temp.length,temp.length);
        }
        for(int i = 0; i < this.others.size(); i++){
            temp = others.get(i);
            g2d.setColor(temp.color);
            g2d.fillRect(temp.x,temp.width,temp.height);
        }
      }
    public static void main(String args[]){
        DnD dnd = new DnD();
        Scanner sc = new Scanner(system.in);
        dnd.setup(sc);
        dnd.run(dnd);
        sc.close();
    }
}

我该怎么做才能使我的代码与 JTextArea 一起工作?如果可以的话,我想用它替换扫描仪。

解决方法

默认情况下,JFrame(这就是您的窗口)使用 BorderLayout,它在中间有一个可扩展的区域。如果您想要多个项目,请先在 JFrame 中添加一个 JPanel,然后再将它们添加到 JPanel。