在JPanels中居中JLabel

在JPanels中居中JLabel

本文介绍了在JPanels中居中JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个记分计划,但我遇到了一个问题。我试图做的是在顶部有一个包含两个JPanel的JPanel,而JPanel又包含两个团队名称。我很困惑为什么程序顶部的两个JLabel没有集中在它们所包含的JPanel中。

I'm making a score-keeping program, but I'm running into a problem. What I've tried to do is have a JPanel at the top that contains two JPanels, which, in turn, contains two team names. I'm confused as to why the two JLabels at the top of the program aren't centered inside of the JPanels they're contained in.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ScoreFrame extends JFrame {

    private static final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
    private static final int WIDTH = SCREEN_SIZE.width;
    private static final int HEIGHT = SCREEN_SIZE.height;
    private final JTextField[] nameField = new JTextField[] { new JTextField(), new JTextField() };
    private final JLabel[] nameLabel = new JLabel[] { new JLabel("Team 1"), new JLabel("Team 2") };
    private final GridBagLayout gridBag = new GridBagLayout();
    private final GridBagConstraints constraints = new GridBagConstraints();
    private final JPanel topPanel = new JPanel();

    public ScoreFrame() {
    super();
    setResizable(false);
    setSize(SCREEN_SIZE);
    setLayout(gridBag);
    setUndecorated(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addKeyListener(new EscapeListener());
    addComponents();
    }

    private void addComponents() {
    addToTopPanel();
    constraints.insets = new Insets(0, 0, (int) (HEIGHT * (double) 4 / 5), 0);
    gridBag.setConstraints(topPanel, constraints);

    add(topPanel);
    }

    private void addToTopPanel() {
    final JPanel[] teamPanel = new JPanel[] { new JPanel(), new JPanel() };
    topPanel.setLayout(gridBag);
    topPanel.setSize(new Dimension(WIDTH, HEIGHT / 5));

    Dimension teamPanelSize = new Dimension(WIDTH / 2, HEIGHT / 5);
    teamPanel[0].setSize(teamPanelSize);
    teamPanel[1].setSize(teamPanelSize);

    Font nameFont = new Font("Times New Roman", Font.PLAIN, 50);
    nameLabel[0].setFont(nameFont);
    nameLabel[1].setFont(nameFont);

    teamPanel[0].add(nameLabel[0]);
    teamPanel[1].add(nameLabel[1]);

    gridBag.setConstraints(teamPanel[0], constraints);

    constraints.gridx = 1;
    gridBag.setConstraints(teamPanel[1], constraints);

    topPanel.add(teamPanel[0]);
    topPanel.add(teamPanel[1]);
    }

    public void paint(Graphics g) {
    super.paint(g);
    int strokeSize = ((WIDTH + HEIGHT) / 2) / 300;
    if (strokeSize < 1) {
        strokeSize = 1;
    }

    final int fontSize = (int) (strokeSize * 12.5);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setStroke(new BasicStroke(strokeSize));
    g.drawLine(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 5);
    g.drawLine(WIDTH / 2, (int) (HEIGHT * (double) 105 / 400), WIDTH / 2, HEIGHT);
    g.drawLine(0, HEIGHT / 5, WIDTH, HEIGHT / 5);
    g.drawRect((int) (WIDTH * (double) 45 / 100), HEIGHT / 5, WIDTH / 10, (int) (HEIGHT * (double) 3 / 20));

    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
    g.drawString("Errors", (int) (WIDTH * (double) 101 / 220), HEIGHT / 4);
    }

    private JFrame getFrame() {
    return this;
    }

    public static void main(final String args[]) {
    new ScoreFrame().setVisible(true);
    }

    public class EscapeListener implements KeyListener {

    public void keyPressed(final KeyEvent event) {
        if (event.getKeyCode() == 27) {
        final int choice = JOptionPane.showConfirmDialog(getFrame(), "Do you want to exit the program?");

        if (choice == 0) {
            System.exit(0);
        }
        }
    }

    public void keyReleased(final KeyEvent event) {
    }

    public void keyTyped(final KeyEvent event) {
    }
    }
}


推荐答案

调用 pack()是使用布局的关键步骤。此示例使用 JLabel.CENTER GridLayout 在调整帧大小时使标签居中。为简单起见,中心面板只是一个占位符。这个稍微复杂的使用了类似的方法以及 java.text.MessageFormat

Invoking pack() is a critical step in using layouts. This example uses JLabel.CENTER and GridLayout to center the labels equally as the frame is resized. For simplicity, the center panel is simply a placeholder. This somewhat more complex example uses a similar approach along with java.text.MessageFormat.

附录:但我将如何申请 pack() 到我的代码?

只需调用 pack(),如图所示引用的例子。我没有看到一种简单的方法来挽救你当前设定尺寸的方法。相反,在 getPreferredSize() > JPanel 代表您的主要内容。无论屏幕大小如何,您实施的,() 应适应当前大小。

Simply invoke pack() as shown in the examples cited. I don't see an easy way to salvage your current approach of setting sizes extrinsically. Instead, override getPreferredSize() in a JPanel for your main content. No matter the screen size, your implementation of paintComponent() should adapt to the current size, for example.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/a/14422016/230513 */
public class Scores {

    private final JLabel[] nameLabel = new JLabel[]{
        new JLabel("Team 1", JLabel.CENTER),
        new JLabel("Team 2", JLabel.CENTER)};

    private void display() {
        JFrame f = new JFrame("Scores");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel teamPanel = new JPanel(new GridLayout(1, 0));
        teamPanel.add(nameLabel[0]);
        teamPanel.add(nameLabel[1]);
        f.add(teamPanel, BorderLayout.NORTH);
        f.add(new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        }, BorderLayout.CENTER);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Scores().display();
            }
        });
    }
}

这篇关于在JPanels中居中JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:14