Добавление компонентов, таких как кнопки, на другие, такие как JTextAreas, в коде не отображается при запуске - PullRequest
1 голос
/ 19 апреля 2019

Ниже приведен рисунок основного интерфейса, который я хочу создать. Все отлично работает, кроме следующего: -

  1. JScrollPane не появляется, когда я пытаюсь вставить много текста в InformationDisplay JTextArea, и я не понимаю, почему
  2. Кнопки, которые я вставил в блок ButtonTanel в доступных блоках JTextArea, вообще не отображаются

enter image description here

Это часть моего кода, которая касается пункта # 1

Информационная панель с информацией Отображение текстовой области и JScrollPane

    JPanel infoPanel = new JPanel();
    infoPanel.setLayout(new BorderLayout());
    JTextArea informationDisplay= new JTextArea();
    JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollbar1.setSize( 20, 20 );
    informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
    informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
    informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));

    infoPanel.add(scrollbar1);
    add(infoPanel, BorderLayout.SOUTH);

Это часть моего кода, которая касается пункта # 2

Добавление textPanel 3 строки, 1 столбец для инкапсуляции 3 TextAreas

    JPanel textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(3, 1, 4, 4));

TextArea1 "availableUnits" 2 строки и 2 столбца для инкапсуляции 4 кнопок

    JTextArea availableUnits = new JTextArea(2,2);
    //availableUnits.setPreferredSize(new Dimension(200, 200));


    JPanel unitButtonPanel = new JPanel();
    unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
    JButton ambulanceUnit = new JButton("Ambulance");
    JButton diseaseControlUnit = new JButton ("diseaseControlUnit");
    JButton gasControlUnit = new JButton("gasControlUnit");
    JButton fireTruck = new JButton("fireTruck");
    unitButtonPanel.add(ambulanceUnit);
    unitButtonPanel.add(diseaseControlUnit);
    unitButtonPanel.add(gasControlUnit);
    unitButtonPanel.add(fireTruck);
    availableUnits.add(unitButtonPanel);
    textPanel.add(availableUnits);

Это полный код, готовый для компиляции: -

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

public class GameView extends JFrame {
    private JPanel buttonPanel;
    private JPanel infoPanel;
    private JTextArea informationDisplay;
    private JTextArea availableUnits;
    private JPanel unitButtonPanel;
    private JPanel disasterPanel;
    private JTextArea disasterDisplay;
    private JButton ambulanceUnit;
    private JButton diseaseControlUnit;
    private JButton gasControlUnit;
    private JButton fireTruck;

    public static void main(String[] args) {
        new GameView();
    }

    public GameView() {
        setSize(1000, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension dim = tk.getScreenSize();
        int xPos = (dim.width/2) - (this.getWidth()/2);
        int yPos = (dim.height/2) - (this.getHeight()/2);
        this.setLocation(xPos,yPos);

        JPanel title = new JPanel();
        JLabel label1 = new JLabel("Invaded City");
        title.add(label1);
        add(title, BorderLayout.NORTH);

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(10, 10, 2, 2));
        for (int i = 0; i < 100; i++) {
            JButton mapButton = new JButton();
            addButton(mapButton);
        }
        add(buttonPanel, BorderLayout.CENTER);  

        JPanel textPanel = new JPanel();
        textPanel.setLayout(new GridLayout(3, 1, 4, 4));

        availableUnits = new JTextArea(2,2);
        //availableUnits.setPreferredSize(new Dimension(200, 200));

        unitButtonPanel = new JPanel();
        unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
        ambulanceUnit = new JButton("Ambulance");
        diseaseControlUnit = new JButton ("diseaseControlUnit");
        gasControlUnit = new JButton("gasControlUnit");
        fireTruck = new JButton("fireTruck");
        unitButtonPanel.add(ambulanceUnit);
        unitButtonPanel.add(diseaseControlUnit);
        unitButtonPanel.add(gasControlUnit);
        unitButtonPanel.add(fireTruck);
        availableUnits.add(unitButtonPanel);
        textPanel.add(availableUnits);

        JTextArea respondingUnits = new JTextArea(2,2);
        //respondingUnits.setPreferredSize(new Dimension(200, 200));
        textPanel.add(respondingUnits);

        JTextArea treatingUnits = new JTextArea(2,2);
        //treatingUnits.setPreferredSize(new Dimension(200, 200));
        textPanel.add(treatingUnits);

        add(textPanel, BorderLayout.EAST);

        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new BorderLayout());
        JTextArea informationDisplay= new JTextArea();
        JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollbar1.setSize( 20, 20 );
        informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
        informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
        informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));

        infoPanel.add(scrollbar1);
        add(infoPanel, BorderLayout.SOUTH);

        disasterPanel = new JPanel();
        disasterPanel.setLayout(new BorderLayout());
        disasterDisplay = new JTextArea();
        disasterDisplay.setPreferredSize(new Dimension(getWidth(), 200));

        disasterPanel.add(disasterDisplay);
        add(disasterPanel, BorderLayout.WEST);

        pack();
        setVisible(true);
    }
    public void addButton(JButton b) {
        buttonPanel.add(b);
    }
}

Выход: - enter image description here

...