gpt4 book ai didi

java - JLabel,如何设置坐标

转载 作者:行者123 更新时间:2023-12-01 19:36:25 24 4
gpt4 key购买 nike

我想通过使用坐标将 JLabel 放置在特定位置。但标签却出现在我的窗口顶部的中间。请告诉我我做错了什么。

类 PaintPanel 扩展 JPanel {

private Color color = Color.RED;
private GeneralPath path;
Graphics2D graphics2D;
ArrayList<PathToDraw> generalPathToDraws;



public PaintPanel() {
path = new GeneralPath();
ArrayList<Object> generalPathToDraws = new ArrayList<>();
//path.moveTo(564, 278);
}

//add multiplay general pathes to drowing
public void addMultiplayPathesAndDrowThem(ArrayList<PathToDraw> ptd){

generalPathToDraws = ptd;
updateGraphics();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
graphics2D = (Graphics2D) g;


//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
JLabel A_label = new JLabel("B");
A_label.setLocation(300, 300);
A_label.setForeground(Color.red);
add(A_label);

how it looks

最佳答案

如果你想使用“setLocation”,你的面板布局应该是“null”

public static class MainPanel extends JPanel{
public MainPanel() {
this.setLayout(null);
JLabel A_label = new JLabel("B");

A_label.setBounds(100, 100, 10, 10);//set location & size
//Or
//A_label.setSize(10, 10);
//A_label.setLocation(100, 100);

A_label.setForeground(Color.red);
this.add(A_label);
}
}

关于java - JLabel,如何设置坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57288817/

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