1

enter image description here

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Type2 extends JFrame {
    private final JLabel q;
    private final JLabel w;
    private final JLabel text;
    private final JTextArea text2;

    public Type2(){

    setLayout(new GridBagLayout());
    GridBagConstraints g = new GridBagConstraints();
    g.fill = GridBagConstraints.HORIZONTAL;
    g.weightx = 1d;

    Color customColor = new Color(225, 250, 250);
    Color customColor2 = new Color(169, 169, 169);

    text = new JLabel("\t\t\t\t\tthe quick brown fox jumps over the lazy dog\t\t\t\t");
    text.setFont(new Font("Ariel", Font.BOLD, 19));
    add(text, g);

    g.gridy = 1;   g.gridx = 0;
    q = new JLabel("q");
    q.setOpaque(true);
    q.setBackground(customColor2);
    q.setFont(new Font("Calibiri", Font.BOLD, 16));
    add(q, g);

    w = new JLabel("w");
    w.setOpaque(true);
    w.setBackground(customColor2);
    w.setFont(new Font("Calibiri", Font.BOLD, 16));
    g.gridx = 1;
    add(w, g);

    text2 = new JTextArea("          ");
    text2.setBackground(customColor);
    text2.setFont(new Font("Ariel", Font.BOLD, 16));
    g.gridx = 0;
    g.gridy = 9;
    add(text2, g);
    }

I am trying to make a Typing gui. My problem is with the formatting of (J)labels. I can't figure out if it's because of GridBagLayout or something else. First Jlabel for every value of y (i.e g.gridy in this case) is wider than any other.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mr.Robot
  • 37
  • 1
  • 5
  • 2
    The grid cell dimensions are affected by other elements in the grid, so if you have a JTextField that expands the width of a grid cell, and especially if you define its width to 1, this will affect others at the same y position. Please show an image of what you're getting and what you want. I'm guessing that you will want to nest a GridLayout JPanel that holds your labels in other JPanels. – Hovercraft Full Of Eels Nov 23 '19 at 21:43
  • 1
    *First Jlabel for every value of y (i.e g.gridy in this case) is wider than any other.* - in the basic example you posted you would need to set the `gridwidth=2` for the "text" and "text2" components. Then these components won't be included in the width calculation of the first column. – camickr Nov 23 '19 at 21:54
  • @HovercraftFullOfEels I have added the image. – Mr.Robot Nov 23 '19 at 22:30
  • @camickr it doesn't work. – Mr.Robot Nov 23 '19 at 22:31
  • 1
    Again, use the right tool for the job -- those labels should go into a JPanel that uses GridLayout. You can nest that same JPanel into your GridBagLayout JPanel if you desire, but if you need regular spacing, use the best layout for the job. – Hovercraft Full Of Eels Nov 23 '19 at 22:45
  • 2
    I said in your basic example you would use gridwidth=2. In your complete example you actually have 10 labels on the second row, so the gridwith should be 10. And you need to reset the gridwidth to 1 for all the other components. Read the Swing tutorial on [How to Use GridBagLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) to understand how the constraints work and for a working example. – camickr Nov 23 '19 at 23:06
  • 2
    As has already been suggested, using a panel with a GridLayout would be easier since you don't need to worry about setting all the constraints. You just add components to the panel and they will automatically wrap (although you will need a few empty labels to fill the empty cells on the 2nd and 3rd rows. Then you would add the label to the BorderLayout.PAGE_START, the grid panel to BorderLayout.CENTER and the text area to BorderLayout.PAGE_END. But first get the GridBagLayout working so you understand its basics. – camickr Nov 23 '19 at 23:11
  • 1
    1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Most IDEs have a keyboard shortcut specifically for formatting code. 2) `Type2 extends JFrame` See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 3) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 4) `"Ariel"` is a Disney character. ITYM `Arial` (2 x 'a', no 'e') But better to use `Font.SANS_SERIF` for compile time checking & .. – Andrew Thompson Nov 24 '19 at 04:49
  • .. cross platform robustness. (Few, is any, OS X machines will have `Arial` installed.) `Calibiri` .. you seem to be guessing how these font names are spelled. It is `Calibri`(2 x 'i' as opposed to 3). But the code should also check for the existence of the font rather than presume it exists. – Andrew Thompson Nov 24 '19 at 04:52
  • **BTW** to expand on the 3rd comment above (which can no longer be edited) The source code requires a `main(String[] args)` method before it could be an MRE / SSCCE. – Andrew Thompson Nov 24 '19 at 05:00
  • BTW - the image seems to be related to making an on-screen keyboard. See [this answer](https://stackoverflow.com/a/57463596/418556) by @camickr (to one of my questions). – Andrew Thompson Nov 24 '19 at 05:14

0 Answers0