0

我正在尝试为程序分配构建 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)只能protectedJComponent. 相反,您应该使用可公开访问的requestFocusInWindow

于 2013-03-16T23:55:04.643 回答
0

使用requestFocus(),不使用requestFocusInWindow()

于 2013-03-17T00:00:30.297 回答