gpt4 book ai didi

java - requestFocusInWindow(boolean) 在 JComponent 中具有 protected 访问权限?

转载 作者:行者123 更新时间:2023-12-01 23:56:52 25 4
gpt4 key购买 nike

我正在尝试为程序分配构建 GUI,该程序分配基本上在顶部有 2 个按钮,从左侧开始,在右侧继续,面板位于底部。但它告诉我:

Error: requestFocusInWindow(boolean) has protected access in 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");
}
}

最佳答案

文档显示 requestFocusInWindow(boolean)保护,因此只能由JComponent 的子类调用。相反,您应该使用可公开访问的 requestFocusInWindow .

关于java - requestFocusInWindow(boolean) 在 JComponent 中具有 protected 访问权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15456056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com