Below is a picture of the basic interface I want created. Everything works fine except for the following:-

  1. The JScrollPane isn't appearing when I try inserting lots of text in the InformationDisplay JTextArea and I don't get why
  2. The buttons I inserted in the unitButtonPanel in the availableUnits JTextArea isn't appearing at all

enter image description here

This is the part in my code that tackles point #1

InfoPanel with informationDisplay TextArea and 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);

This is the part in my code that tackles point #2

Adding a textPanel 3 rows, 1 column to encapsulate 3 TextAreas

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

TextArea1 "availableUnits" 2 rows and 2 columns to encapsulate 4 buttons

    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);

This is the full code ready for compilation:-

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);
    }
}

Output:- enter image description here

0 Answers