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);
}
我正在尝试输入gui。我的问题是(J)标签的格式。我不知道是因为GridBagLayout
还是其他原因。每个y值的第一个Jlabel
(在本例中为g.gridy
)比其他任何一个都宽。