簡體   English   中英

將JLabel放置在JFrame的左上角

[英]Positioning a JLabel on the top left corner of JFrame

public class a5Frame2 extends JFrame implements KeyListener
{
    public a5Frame2()
    {
        super("TV");
        setLocation(450, 75);
        setFocusable( true );
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel panel = new JPanel();
        this.setLayout(new BorderLayout());

        panel.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        JLabel channelNumber = new JLabel("hello");
        gc.gridx = 0;
        gc.gridy = 0;
        gc.anchor = GridBagConstraints.NORTHWEST;
        gc.insets = new Insets(2, 0, 0, 2);
        panel.add(channelNumber, gc);

        mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
        mainPanel.add(panel, BorderLayout.NORTH);
        this.add(mainPanel);
        this.setVisible(true);

        this.addKeyListener(this); 
    }
}

JLabel hello當前位於JFrame的頂部北部。 我希望它位於JFrame的左上角。 我很確定gc.anchor = GridBagConstraints.NORTHWEST; 做這項工作,但在這種情況下不起作用。 有誰知道為什么它不起作用?

這是我當前的JFrame的圖像

gc.weightx = 1添加到您的約束中

在此處輸入圖片說明

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class a5Frame2 extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new a5Frame2();
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public a5Frame2() {
        super("TV");
        setLocation(450, 75);
        setFocusable(true);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel(new BorderLayout()){

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }

        };
        JPanel panel = new JPanel();
        this.setLayout(new BorderLayout());

        panel.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        JLabel channelNumber = new JLabel("hello");
        gc.gridx = 0;
        gc.gridy = 0;
        gc.weightx = 1;
        gc.anchor = GridBagConstraints.NORTHWEST;
        gc.insets = new Insets(2, 0, 0, 2);
        panel.add(channelNumber, gc);

        mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
        mainPanel.add(panel, BorderLayout.NORTH);
        this.add(mainPanel);
        this.setVisible(true);

    }
}

正如評論中已經指出的那樣, KeyListener並不是一個好主意,尤其是當直接附加到JFrame ,太多的事情可能會阻礙並且阻止KeyListener觸發事件。

看一看如何使用鍵綁定作為更好的選擇

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM