gpt4 book ai didi

java - 将 JLabel 放置在 JFrame 的左上角

转载 作者:行者123 更新时间:2023-12-01 22:33:44 24 4
gpt4 key购买 nike

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; 可以完成这项工作,但在这种情况下它不起作用。有谁知道为什么它不起作用?

Here is an image of my current JFrame

最佳答案

gc.weightx = 1 添加到您的约束

enter image description here

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 触发事件。

看看How to Use Key Bindings寻找更好的选择

关于java - 将 JLabel 放置在 JFrame 的左上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27182946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com