如何创建jtextarea?

kwvwclae  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(339)

我试图为我的输入创建一个textarea,但它破坏了我的代码。它不再绘制字符或网格,我看到的只是右上角的一个小按钮。输出截图
以下是我的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, 0, 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, sc, 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, 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();
        }
        return charactersLists;
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(new Color(colorR, colorG, colorB));
        g2d.fillRect(0, 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, 0, i, 700);
        }
        for(int i = 0; i < 700; i += 10){
            g2d.setColor(new Color(255, 255, 255));
            if(i % 20 == 10){
                g2d.setColor(new Color(150, 150, 150));
            }
            g2d.drawLine(0, i, 1280, 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.y, 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一起工作?如果我能让它工作的话,我想把它换掉。

5f0d552i

5f0d552i1#

jframe(这就是您的窗口)默认情况下使用borderlayout,它在中间有一个扩展的区域。如果您想在其中包含多个项目,请先将jpanel添加到jframe中,然后将它们添加到jpanel中。

相关问题