JLabel,如何设置坐标
JLabel, how to set coodinats
我想通过在特定位置使用坐标来放置 JLabel。
但是标签出现在我的 window 顶部的中间。
请告诉我我做错了什么。
class 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);
如果您想使用 "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);
}
}
这是评论问题的答案。
static JLabel A_label;
public static class MainPanel extends JPanel{
public MainPanel() {
this.setLayout(null);
A_label = new JLabel("B");
A_label.setBounds(100, 100, 10, 10);//set location & size
A_label.setForeground(Color.red);
this.add(A_label);
}
public void paintComponent(Graphics g) {
this.remove(A_label);
}
}
或者,如果您只想更改文本
static JLabel A_label;
public static class MainPanel extends JPanel{
public MainPanel() {
this.setLayout(null);
A_label = new JLabel("B");
A_label.setBounds(100, 100, 10, 10);//set location & size
A_label.setForeground(Color.red);
this.add(A_label);
}
public void paintComponent(Graphics g) {
A_label.setText("C");
}
}
我想通过在特定位置使用坐标来放置 JLabel。 但是标签出现在我的 window 顶部的中间。 请告诉我我做错了什么。
class 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);
如果您想使用 "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);
}
}
这是评论问题的答案。
static JLabel A_label;
public static class MainPanel extends JPanel{
public MainPanel() {
this.setLayout(null);
A_label = new JLabel("B");
A_label.setBounds(100, 100, 10, 10);//set location & size
A_label.setForeground(Color.red);
this.add(A_label);
}
public void paintComponent(Graphics g) {
this.remove(A_label);
}
}
或者,如果您只想更改文本
static JLabel A_label;
public static class MainPanel extends JPanel{
public MainPanel() {
this.setLayout(null);
A_label = new JLabel("B");
A_label.setBounds(100, 100, 10, 10);//set location & size
A_label.setForeground(Color.red);
this.add(A_label);
}
public void paintComponent(Graphics g) {
A_label.setText("C");
}
}