[英]JButtons not appearing on JPanel
I am working on a menu that should come up with 2 buttons, "resume" and "Exit to main menu". 我正在处理一个应该带有2个按钮的菜单,“恢复”和“退出主菜单”。 The problem is that the JPanel is showing without any problems but the JButtons are not there, even though I have added them. 问题是,即使我添加了JPanel,显示的JPanel也没有任何问题,但JButton却不存在。 The following part of code is the handling of the graphical side of the menu. 以下代码部分是菜单图形部分的处理。
if(secMenuFlag){
JPanel menu = new JPanel();
JButton resume = new JButton("Resume"), exit = new JButton("Exit to Main Menu");
menu.setLayout(null);
menu.setLocation((frame.getWidth() - menuSize[0]) / 2, (frame.getHeight() - menuSize[1]) / 2);
menu.setSize(menuSize[0], menuSize[1]);
menu.setBackground(new Color(0, 0, 0));
resume.addActionListener(this);
resume.setFont(new Font("Sans-serif", Font.BOLD, 18));
resume.setBackground(Color.white);
resume.setLocation(100, 100);
exit.addActionListener(this);
exit.setFont(new Font("Sans-serif", Font.BOLD, 18));
exit.setBackground(Color.white);
exit.setLocation(200, 100);
menu.add(resume);
menu.add(exit);
super.add(menu, 0);
}
set the bounds of the buttons. 设置按钮的边界。 I have done for a resume, please follow the same procedure for the exit. 我已经完成了简历,请按照相同的步骤退出。
JPanel menu = new JPanel();
JButton resume = new JButton("Resume"), exit = new JButton("Exit to Main Menu");
menu.setLayout(null);
JFrame frame;
frame = new JFrame("check");
frame.setLayout(null);
frame.setSize(300, 300);
int[] menuSize = new int[2];
menuSize[0] = 200;
menuSize[1] = 300;
menu.setLocation((frame.getWidth() - menuSize[0]) / 2, (frame.getHeight() - menuSize[1]) / 2);
menu.setSize(menuSize[0], menuSize[1]);
menu.setBackground(new Color(255, 255, 255));
// resume.addActionListener((ActionListener) this);
resume.setBounds(20, 20, 100, 100);
resume.setFont(new Font("Sans-serif", Font.BOLD, 18));
resume.setBackground(Color.BLACK);
resume.setLocation(100, 100);
resume.setVisible(true);
// exit.addActionListener((ActionListener) this);
exit.setFont(new Font("Sans-serif", Font.BOLD, 18));
exit.setBackground(Color.BLACK);
exit.setLocation(200, 100);
exit.setVisible(true);
menu.add(resume);
menu.add(exit);
frame.add(menu);
frame.setVisible(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.