有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing如何在java面板上移动图形元素?

我目前正在做一个简单的类似乒乓球的游戏,但我被矩形的定位绊住了。 我想把它的Y位置改为实际面板高度的一半

package com.game;
import javax.swing.*;
import java.awt.*;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame("gEngine");
        Player playerOne = new Player(frame);
        Player playerTwo = new Player(frame);
    }
}


package com.game;

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

public class Player {
    public Player(JFrame frame) {
        MyPanel panel = new MyPanel();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.BLACK);
        frame.setSize(1280, 720);
        frame.setVisible(true);
    }
}
class MyPanel extends JPanel {
    public void paintComponent(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(50, 60, 20, 120);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    你的代码有很多“问题”。我建议你找些教程什么的。你可以使用frame.setVisible(true)Player的类构造函数中的内容。您是否意识到,每次创建Player对象时,所有这些都将应用于JFrame?这有必要吗?也许你应该只做一次。此外,为了根据组件的位置和大小来paint组件,您可以执行g.fillRect(50, getHeight() / 2, 20, 120);

    public class Test {
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("gEngine");
            Player playerOne = new Player();
            Player playerTwo = new Player();
    
            // Set the proper layout manager
            frame.setLayout(new GridLayout());
    
            frame.add(playerOne.getMyPanel());
            frame.add(playerTwo.getMyPanel());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setBackground(Color.BLACK);
            frame.setSize(1280, 720);
            frame.setVisible(true);
        }
    
        public static class Player {
            private JPanel myPanel;
    
            public Player() {
                this.myPanel = new MyPanel();
            }
    
            public JPanel getMyPanel() {
                return myPanel;
            }
    
        }
    
        static class MyPanel extends JPanel {
            @Override
            public void paintComponent(Graphics g) {
                // let the component be painted "natural"
                super.paintComponent(g);
                // Do custom painting
                g.setColor(Color.WHITE);
                g.fillRect(50, getHeight() / 2, 20, 120);
            }
        }
    
    }
    

    编辑(基于评论):

    背景是默认的,因为GridLayout将屏幕分割为两个面板(在画面中间)。即使框架有BLACK背景,这两个面板也覆盖了它。所以你看到的背景是来自面板,而不是框架。要更改它,您必须更改面板的背景(并使其不透明):

    static class MyPanel extends JPanel {
        public MyPanel() {
            super();
            setOpaque(true);
            setBackground(Color.BLACK);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            // let the component be painted "natural"
            super.paintComponent(g);
            // Do custom painting
            g.setColor(Color.WHITE);
            g.fillRect(50, getHeight() / 2, 20, 120);
        }
    }