简体   繁体   English

如何将JPanel添加到JFrame的中心?

[英]How to add a JPanel to the center of a JFrame?

I try to make a JFrame in which I have a JPanel (which contains a circle) and it is bordered by four buttons (north, south, east, west). 我尝试制作一个JFrame,其中有一个JPanel(包含一个圆圈),并且以四个按钮(北,南,东,西)为边界。 The circle will move in the direction indicated by the pressed button. 圆圈将按所按按钮指示的方向移动。
My problem is that I can't manage to put my JPanel in the center: 我的问题是我无法将JPanel放在中心:

https://i.stack.imgur.com/Cv12Q.png

The JFrame's class looks like this: JFrame的类如下所示:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Frame extends JFrame implements ActionListener {

JButton north, south, east, west;
int x = 10, y = 10;
MyPanel panel;

public Frame() {
    setLayout(new BorderLayout());
    panel = new MyPanel();
    panel.setBackground(Color.MAGENTA);

    north = new JButton("NORTH");
    south = new JButton("SOUTH");
    east = new JButton("EAST");
    west = new JButton("WEST");

    add(panel, BorderLayout.CENTER);
    add(north, BorderLayout.NORTH);
    add(south, BorderLayout.SOUTH);
    add(east, BorderLayout.EAST);
    add(west, BorderLayout.WEST);

    north.addActionListener(this);
    south.addActionListener(this);
    east.addActionListener(this);
    west.addActionListener(this);

    setBounds(100, 100, 300, 300);
    setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == north) {
        y -= 3;
        panel.setY(y);
        panel.repaint();
    }

    if (e.getSource() == south) {
        y += 3;
        panel.setY(y);
        panel.repaint();
    }

    if (e.getSource() == east) {
        x += 3;
        panel.setX(x);
        panel.repaint();
    }

    if (e.getSource() == west) {
        x -= 3;
        panel.setX(x);
        panel.repaint();
    }
}
}

And the MyPanel class looks like this: MyPanel类如下所示:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class MyPanel extends JPanel {
private Color color = Color.CYAN;
private int x = 10, y = 10;

public void paint(Graphics g) {
    super.paintComponent(g);
    g.setColor(color);
    g.fillOval(x, y, 20, 20);
}

public int getX() {
    return x;
}

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

public int getY() {
    return y;
}

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

Don't override getX() and getY() methods of your custom panel. 不要覆盖自定义面板的getX()getY()方法。 Those methods are used by Swing to determine the location of the component. Swing使用这些方法来确定组件的位置。

Instead you should have methods like setCirleX(...) , setCircleY(...) , getCircleX() and getCircleY() . 相反,您应该具有setCirleX(...)setCircleY(...)getCircleX()getCircleY()

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

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