0

私は、プログラム割り当て用のGUIを構築しようとしています。このGUIには、基本的に、上部に2つのボタンがあり、左側から始まり、右側から再開し、パネルが下部にあります。しかし、それは私に言っています:

エラー:requestFocusInWindow(boolean)はJComponentのアクセスを保護しています

私は以前にこれに遭遇したことがありますが、それが何を意味するのか理解していないように感じます。私がグーグルで調べた良い説明があり、何も見つからないように見えるので、おそらく何かばかげていると思います。

GUIを構築するために使用しているコードは次のとおりです。

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

public class PendulumWindow {

    protected JFrame pendFrame;
    protected JPanel pendPanel;
    protected JButton resume;
    protected final int SIZE_X = 500;
    protected final int SIZE_Y = 450;
    protected Dimension pendPanSize = new Dimension(SIZE_X, SIZE_Y);

    public PendulumWindow() {

    }

    public PendulumWindow(String s) {
        makePanel();
        makeFrame();
    }

    public void makePanel() {
        pendPanel = new JPanel();

        pendPanel.setPreferredSize(pendPanSize);
        pendPanel.setFocusable(true);
        pendPanel.requestFocusInWindow(true);
        pendPanel.setBackground(Color.BLUE);
   }

   public void makeFrame() {
        pendFrame = new JFrame("Pendulum");
        start = new JButton("start");
        resume = new JButton("resume");

        //---------- FRAME PROPERTIES ----------//

        pendFrame.setSize(500,500);
        pendFrame.setVisible(true);
        pendFrame.setResizable(true);
        pendFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //---------- ADD ELEMENTS TO FRAME ----------//

        pendFrame.setLayout(new BorderLayout());
        pendFrame.add(start, BorderLayout.WEST);
        pendFrame.add(resume, BorderLayout.EAST);
        // pendFrame.add(pendPanel, BorderLayout.SOUTH);
   }

   public static void main(String[] args) {
        PendulumWindow window = new PendulumWindow("Pendulum");
   }
}
4

2 に答える 2

3

ドキュメントは、requestFocusInWindow(boolean)protectedそうなので、のサブクラスによってのみ呼び出すことができることを示していJComponentます。代わりに、公的にアクセス可能なrequestFocusInWindowを使用する必要があります。

于 2013-03-16T23:55:04.643 に答える
0

requestFocus()ではなく、使用してくださいrequestFocusInWindow()

于 2013-03-17T00:00:30.297 に答える