JPanelフォームの主な静的メソッドのrepaint()メソッドにアクセスしようとしたときにエラーが発生しました:"Cannot make a static reference to the non-static method repaint() from the type Component"
。メインメソッドを"public void main"
に変更しようとしたとき、または静的を削除しようとするとコンパイルできません。この問題を解決する適切なアプローチは何ですか?どのように正しく静的なrepaint()メソッドのフォーム静的メインにアクセスするには?
public class mull extends JPanel { ...
....
public static void main(String[] args) {
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
f.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
&& (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
ringPoints += 100;
allowedToDrawRing = false;
}
if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
&& (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
squarePoints += 100;
allowedToDrawSquare = false;
}
if(!allowedToDrawSquare && !allowedToDrawRing){
// stop timer
// display points
ringTimer.stop();
squareTimer.stop();
showScores = true;
repaint(); // Cannot make a static reference to the non-static method repaint() from the type Component
}
}
});
f.add(p);
f.add(new mull());
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
私はすべてのコード形式がメイン置き換え、私はどのように対処する見ることができない1つの詳細があります。私の主な方法では、JFrameを作成し、それに時間を追加しました(Swingタイマー)。すべてのコードをコンストラクタにパースすると、内部で(コンストラクタ自体をJFrameタイマーに追加するために)呼び出すのは意味がありません。どのようにそれを解決するには? コンストラクタ:
public Mull() {
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
f.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
&& (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
ringPoints += 100;
allowedToDrawRing = false;
}
if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
&& (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
squarePoints += 100;
allowedToDrawSquare = false;
}
if(!allowedToDrawSquare && !allowedToDrawRing){
// stop timer
// display points
ringTimer.stop();
squareTimer.stop();
showScores = true;
repaint();
}
}
});
f.add(p);
f.add(new Mull());
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setVisible(true);
ActionListener actionListenerRing = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
if(ringCounter < 3){
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
while(!(
(xSquare + squareSize) < (xRing)
|| (xSquare) > (xRing + ringSize)
|| (ySquare + squareSize) < (yRing)
|| (ySquare) > (yRing + ringSize)
)
){
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
}
setParamsRing(xRing, yRing, ringSize);
ringCounter++;
repaint();
}
}
};
ActionListener actionListenerSquare = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
if(squareCounter < 3){
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
while(!(
(xSquare + squareSize) < (xRing)
|| (xSquare) > (xRing + ringSize)
|| (ySquare + squareSize) < (yRing)
|| (ySquare) > (yRing + ringSize)
)
){
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
}
setParamsSquare(xSquare, ySquare, squareSize);
squareCounter++;
repaint();
}
}
};
ringTimer = new Timer(700, actionListenerRing);
ringTimer.setInitialDelay(1000);
ringTimer.start();
squareTimer = new Timer(500, actionListenerSquare);
squareTimer.setInitialDelay(1000);
squareTimer.start();
}
おそらく、私は別のクラスにタイマ・メソッドを移動し、f.add(new setTimers());
のようなものを呼び出す必要があります....
タイマーはコンポーネントではないため、フレームに追加することはできません(f)。タイマーの問題を説明してください。なぜなら、ここで行われているようにOKであるはずだからです。 –