0

我正在尝试编写一个代码,该代码将使用 JButtons 移动一个红球(然后添加键盘绑定器)。编译时没有问题,当我跑步时我看到了球,但 JButtons 不会影响他。我认为问题可能是球只被抽了一次,然后一次又一次地被叫到没有被抽到新的位置,但我不知道如何解决这个问题。1)有人知道我该如何解决吗?2) 有没有办法将 JPanel 的形状更改为球?(这可能是移动他的更简单的方法)

package il.co.atlantis;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class KeyBinders implements ActionListener {

boolean right=true, left=false, up=false, down=false, inGame=true;
JPanel backgroundPanel, bannerPanel, scorePanel, applePanel;
JLabel currentScoreLabel, highestScoreLabel;
JButton upButton, downButton, rightButton, leftButton;
long millis =System.currentTimeMillis(), millisn =System.currentTimeMillis();
public static final int WID = 10, HEI = 10;
public static int x1 = 100, y1 = 100;
public class MyGraphics extends JComponent {

    private static final long serialVersionUID = 1L;

    MyGraphics() {
        setPreferredSize(new Dimension(700, 500));
    }

    public void moveRight(){
    ++x1;
    }

    public void moveLeft(){
    --x1;
    }

    public void moveUp(){
    --y1;
    }

    public void moveDown(){
    ++y1;
    }

    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.setColor(Color.red);
        g.fillOval(x1, y1, WID, HEI);
    }

}


public JPanel CreateContentPane (){
    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    backgroundPanel = new JPanel();
    backgroundPanel.setBackground(Color.black);
    backgroundPanel.setLocation(100, 10);
    backgroundPanel.setSize(700, 500);
    totalGUI.add(backgroundPanel);

    upButton = new JButton("up");
    upButton.setLocation(0,0);
    upButton.setSize(50,50);
    totalGUI.add(upButton);

    downButton = new JButton ("down");
    downButton.setLocation(0,50);
    downButton.setSize(50,50);
    totalGUI.add(downButton);

    rightButton = new JButton("right");
    rightButton.setLocation(0,100);
    rightButton.setSize(50,50);
    totalGUI.add(rightButton);

    leftButton = new JButton("left");
    leftButton.setLocation(0,150);
    leftButton.setSize(50,50);
    totalGUI.add(leftButton);


    MyGraphics tr = new MyGraphics();
    tr.setLocation(100, 100);
    backgroundPanel.add(tr);


    return totalGUI;
}

public void ActionPerformed(ActionEvent h){
    if(h.getSource() == upButton) {
        --y1;
    }
    else if(h.getSource() == downButton){
        ++y1;
    }
    else if(h.getSource() == leftButton){
        --x1;
    }
    else if(h.getSource() == rightButton){
        ++x1;
    }
}

private static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("[=] JButton Scores! [=]");

    //Create and set up the content pane.
    KeyBinders demo = new KeyBinders();
    frame.setContentPane(demo.CreateContentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(280, 190);
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            createAndShowGUI();

        }
    });
}

public KeyBinders() {
    // TODO Auto-generated constructor stub
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}


}
4

2 回答 2

2

当您调用动作事件时,actionPerformed()函数会像您一样被调用。您也更改了绘图位置。您需要调用Component.repaint()which 告诉 Swing 整个组件,无论您指定要重新绘制的哪个组件,都必须更新。所以在你的代码中添加这个函数调用。例如:

public void ActionPerformed(ActionEvent h){
    if(h.getSource() == upButton) {
        --y1;
    }
    else if(h.getSource() == downButton){
        ++y1;
    }
    else if(h.getSource() == leftButton){
        --x1;
    }
    else if(h.getSource() == rightButton){
        ++x1;
    }
   repaint();
}

查看教程:执行自定义绘画。

于 2013-10-24T12:13:30.593 回答
1

有一种方法叫repaint()你应该熟悉。

当在组件(例如 JFrame)上调用时,它将重新绘制其中的所有组件。如果您希望您的更改在屏幕上可见,您自然需要调用它。

至于自定义绘画,你根本不应该使用组件,而是使用 Graphics.fillRect/fillOval 等方法来绘制你想要的东西。

有关自定义绘画教程,请参见此处

于 2013-10-24T11:59:10.047 回答