當我在全屏模式下運行應用程式時,未訪問MainPanel類的建構式。只有當我單擊選單欄中的任何專案時才能訪問它。
該應用程式在視窗模式下或當我手動設定框架的寬度和高度時運行良好。
在視窗模式下運行的應用程式或當我設定寬度和高度時(作業正常)
應用程式以全屏模式運行,但未呼叫 MainPanel() 建構式
單擊任何選單項后以全屏模式運行的應用程式(呼叫的 MainPanel() 建構式)
主程式
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);
}
}
應用框架
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);
}
}
uj5u.com熱心網友回復:
只需在 addMenuBar() 之前在應用程式框架中添加 MainPanel。無論如何都會呼叫建構式,但 MainPanel 以其他方式不可見。
uj5u.com熱心網友回復:
我發現我做錯了什么。在添加 JPanel 之前,我使 JFrame 可見。所以,我只是從 AppFrame 類中洗掉了框架可見性,并在添加 JPanel 后從 Main.class 中設定了它的可見性(true)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/323144.html
