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

1

repaint()知っておくべきメソッドがあります。

コンポーネント (JFrame など) で呼び出されると、内部のすべてのコンポーネントが再描画されます。当然のことながら、変更を画面に表示したい場合は、それを呼び出す必要があります。

カスタム ペイントに関しては、Component をまったく使用しないでください。Graphics.fillRect/fillOval などのメソッドを使用して、必要なものを描画するだけです。

カスタム ペイントのチュートリアルについては、こちらを参照してください。

于 2013-10-24T11:59:10.047 に答える