【问题标题】:How can make a JTextArea?怎样才能做一个JTextArea?
【发布时间】:2021-04-13 10:07:26
【问题描述】:

我正在尝试为我的输入创建一个 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, 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 一起工作?如果我可以让它工作,我想用它替换扫描仪。

【问题讨论】:

标签: java swing jtextarea


【解决方案1】:

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

【讨论】:

  • 我现在有一个蓝色方块、一个高但 1 个字符宽的文本区域,以及它旁边的一个按钮。如何移动文本区域、按钮和画布并调整其大小?
  • 您需要通过在构造函数中指定行/列来设置 JTextArea 的大小。请参阅docs.oracle.com/javase/tutorial/uiswing/components/… 了解更多信息。
  • 我的构造函数有参数 (20, 1),它给出了相同的结果。我该如何解决这个问题?
  • 我还需要将绘制方法使用的画布设置为 1280 x 700 像素,并且我需要移动按钮。我不知道该怎么做。
  • @ssddf 你为什么要使用 (20, 1)???那只会显示一个字符。首先从教程中下载工作演示代码,然后根据您的要求对其进行修改。
猜你喜欢
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 2022-11-09
相关资源
最近更新 更多