java - 在全屏模式下运行应用程序时,Java Swing 无法找出 JPanel 的问题
问题描述
当我以全屏模式运行应用程序时,未访问MainPanel类的构造函数。只有当我单击菜单栏中的任何项目时才能访问它。
该应用程序在窗口模式下或当我手动设置框架的宽度和高度时运行良好。
应用程序以全屏模式运行,但未调用 MainPanel() 构造函数
单击任何菜单项后应用程序以全屏模式运行(调用的 MainPanel() 构造函数)
主.java
public class Main {
public static void main(String[] args) {
AppFrame mainFrame = new AppFrame("Algorithm Visualizer");
mainFrame.add(new MainPanel());
}
}
主面板.java
public class MainPanel extends JPanel {
MainPanel() {
this.setBackground(Color.BLACK);
}
}
AppFrame.java
class AppFrame extends JFrame implements ActionListener {
private JMenuBar menuBar;
private JMenu fileMenu, sortingAlgoMenu, searchingAlgoMenu;
private JMenuItem exitItem, bubbleSortItem;
// constructor with frame_title and auto app resolution
AppFrame(String frameTitle) {
// sets the app theme
setTheme();
// Frame Properties
this.setTitle(frameTitle);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
GraphicsDevice myDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
// this is the code that makes the frame full-screen
if(myDevice.isFullScreenSupported()) {
this.setUndecorated(true);
myDevice.setFullScreenWindow(this);
}
else {
// windowed mode (title bar present)
int deviceWidth = myDevice.getDisplayMode().getWidth();
int deviceHeight = myDevice.getDisplayMode().getHeight();
this.setSize(deviceWidth, deviceHeight);
this.setLocationRelativeTo(null);
}
// adds the MenuBar
addMenuBar();
// makes the JFrame visible
this.setVisible(true);
}
// constructor passed with app title, width and height
AppFrame(String frameTitle, int frameWidth, int frameHeight) {
// sets the app theme
setTheme();
// Frame Properties
this.setTitle(frameTitle);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(frameWidth, frameHeight);
this.setLocationRelativeTo(null);
// adds the MenuBar
addMenuBar();
// makes the JFrame visible
this.setVisible(true);
}
// sets the theme of the application
private static void setTheme() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
// setting the JMenuBar
private void addMenuBar() {
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
menuBar.add(fileMenu);
sortingAlgoMenu = new JMenu("Sorting Algorithms");
searchingAlgoMenu = new JMenu("Searching Algorithms");
menuBar.add(sortingAlgoMenu);
menuBar.add(searchingAlgoMenu);
exitItem = new JMenuItem("Exit");
bubbleSortItem = new JMenuItem("Bubble Sort");
fileMenu.add(exitItem);
sortingAlgoMenu.add(bubbleSortItem);
// on-click of "Exit"
exitItem.addActionListener(this);
this.setJMenuBar(menuBar);
}
// handle action events (on-click listeners)
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == exitItem)
System.exit(0);
}
}
解决方案
只需在 addMenuBar() 之前的应用程序框架中添加 MainPanel。无论如何都会调用构造函数,但 MainPanel 以其他方式不可见。
推荐阅读
- android - Android 材质范围日期选择器未正确放置
- python - Pandas groupby 折叠第一行组
- python - 有没有办法使用乌龟在“背景”中绘制?
- git - 你如何告诉 git 删除和添加确实是一个动作?
- jenkins - Maven 3.5.2 是否支持 Java11 构建
- google-cloud-platform - 使用部署管理器的未经身份验证的 CloudRun
- jquery - 导航栏(折叠)不起作用 Bootstrap 5
- r - 当我写入 Excel 工作表时,如何从数字单元格 R 中取出绿色三角形?
- java - 在 Page 类中使用 Test 类中的驱动程序时出现驱动程序 Null 指针异常
- bluetooth - 使用 NodeMCU (ESP32) 作为 HCI 上的蓝牙控制器