当我将它添加到JPanel类时,我不确定为什么我的JButton没有显示:如果我的类扩展了JPanel,那不应该这样吗。添加是否足够?如果我从另一个Jpanel中删除这个Jpanel类,那么连接到这个面板的组件也会消失吗?谢谢!
public class Menu extends JPanel{
private static final long serialVersionUID = 1L;
static boolean startGame = false;
public static String user;
public Menu(final JFrame frame) {
JPanel userName = new JPanel(); // create new JPanel
final JTextField text = new JTextField();
text.setText("input your username here");
//adds userName input box to the panel
Action action = new AbstractAction(){
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
user = text.getText();
text.setVisible(false);
Menu.startGame = true;
}};
text.addActionListener(action);
userName.add(text);
frame.add(userName, BorderLayout.SOUTH);
frame.revalidate();
frame.repaint();
setFocusable(true);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
sndMenu newMenu = new sndMenu(frame);
newMenu.setVisible(true);
}
});
/**
* this does not display help!
*/
//add a button to the menu called "help"
JButton help = new JButton("get instructions");
this.add(help,BorderLayout.CENTER);
help.setVisible(true);
help.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
instructions instr = new instructions(frame);
frame.add(instr);
frame.revalidate();
frame.repaint();
}
});
}
public void paint (Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);
Font fnt0 = new Font("Manaspace", Font.BOLD, 40);
Font fnt1 = new Font("Manaspace", Font.BOLD, 30);
g.setFont(fnt0);
g.setColor(Color.white);
g.drawString("Welcome To Jumping Box!", 120, 300);
g.setFont(fnt1);
g.drawString("Click to play!", 260, 400);
}
}