简体   繁体   English

JPanel显示在JFrame的右下角

[英]JPanel displaying in bottom right quarter of JFrame

I am trying to add a MessagePanel which extends JPanel into a JFrame so that it is simply in the center and takes up the whole thing, which I have done many times before but now for some reason it is appearing in the bottom right quarter of the JFrame and I can't figure out why. 我正在尝试添加一个MessagePanel,它将JPanel扩展到JFrame中,以便它简单地位于中心并占据整个内容,这是我之前做过很多次的,但是由于某种原因,它现在出现在该窗口的右下角JFrame和我不知道为什么。 I added a border around it so i could see where it is. 我在它周围添加了边框,这样我就可以看到它在哪里。 Anybody know what's wrong ? 有人知道怎么了吗?

import javax.swing.*;
import java.awt.*;

public class Book extends JFrame{

    public Book(){
        setLayout(new BorderLayout());
        add(new MessagePanel("My Message"), BorderLayout.CENTER);
    }

    public static void main(String[] args){
        Book f = new Book();
        f.setDefaultCloseOperation(Book.EXIT_ON_CLOSE);
        f.setTitle("Title");
        f.setSize(500, 500);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

// //

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;


public class MessagePanel extends JPanel{
    private String message = "";
    private boolean centered;
    private int x = 20;
    private int y = 20;
    private int interval = 10;

    public MessagePanel(){
    }

    public MessagePanel(String message){
        this.message = message;
        this.centered = true;
        setBorder(new LineBorder(Color.BLACK, 2));
    }

    public void setMessage(String message){
        this.message = message;
        repaint();
    }

    public void setCentered(boolean centered){
        this.centered = centered;
        repaint();
    }

    public void setX(int x){
        this.x = x;
        repaint();
    }

    public void setY(int y){
        this.y = y;
        repaint();
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public void setInterval(int interval){
        this.interval = interval;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);

        if(centered){
            FontMetrics fontMetrics = g.getFontMetrics();
            x = (getWidth() / 2) - (fontMetrics.stringWidth(message) / 2);
            y = (getHeight() / 2) + (fontMetrics.getAscent() / 2);

        }
        g.drawString(message, x, y);

    }

    public void moveUp(){
        this.y -= interval;
        repaint();
    }

    public void moveDown(){
        this.y += interval;
        repaint();
    }

    public void moveLeft(){
        this.x -= interval;
        repaint();
    }

    public void moveRight(){
        this.x += interval;
        repaint();
    }
}

Don't override getX(), setX(...), getY() and setY(...). 不要覆盖getX(),setX(...),getY()和setY(...)。

Those are methods of the Component class and are used to control the location of the component. 这些是Component类的方法,用于控制组件的位置。

If you want to control the location of the text then use different variables. 如果要控制文本的位置,请使用其他变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM