java - JFrame 组件未显示
问题描述
我正在尝试向 JFrame 添加组件,但是当我运行 GameMenu.java 时,唯一显示的是我的ImageIcon 。我已经实例化了setVisible(); 特别是在我设置框架或将组件添加到面板或菜单栏之后。所以我不确定为什么没有组件出现。我认为这可能与我的格式或主要方法有关。
这是我的两门课:
游戏菜单.java:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class GameMenu{
public static void main(String[] args) {
FrameCaller obj = new FrameCaller();
}
}
class FrameCaller extends JFrame {
public FrameCaller(){
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JLabel(new ImageIcon("logo.png")));
pack();
setLocationRelativeTo(null);
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("Game List");
JMenu m2 = new JMenu("Help");
JMenu m3 = new JMenu("Stats");
mb.add(m1);
mb.add(m2);
mb.add(m3);
JMenuItem showRulesButton = new JMenuItem("View game rules");
m2.add(showRulesButton);
JMenuItem m77 = new JMenuItem("View past game stats");
m3.add(m77);
mb.setVisible(true);
JPanel panel = new JPanel();
JButton newGameButton = new JButton("New Game");
newGameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new inGameFrame();
dispose();
}
});
panel.add(newGameButton);
panel.setVisible(true);
setVisible(true);
}
}
八关.java:
import javax.swing.*;
public class EightOff {
public static void main(String[] args)
{
inGameFrame obj = new inGameFrame();
}
}
class inGameFrame extends JFrame
{
public inGameFrame() {
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
任何提示都会很棒。谢谢。
解决方案
推荐阅读
- memory - 已提交与保留内存
- reactjs - 如何在同一个div中添加引导类和条件类?
- binary-search-tree - 二进制搜索以查找包含给定点的区间
- python - 正确的 JWT 身份验证架构和流程
- javascript - 【React-native】undefined is not an object (evalating '_this.props.navigation.navigate')
- angular - 角材料
未显示在自定义组件中 - python - 试图在屏幕上显示多个弹丸
- swift - 如何在用户默认值中存储日期?
- javascript - 如何修复 JavaScript 和 jQuery 中的“正确反馈问题”?我的警报有效,但正确的反馈无效
- import - 从 DB2 导入表后,它们不会出现在关系模型或设计中的任何地方吗?