我正在制作游戏的小菜单。我已经完成了游戏本身,但我的菜单遇到了一些问题。当我点击按钮&#34;规则和控制&#34;,&#34;选项&#34;和&#34;关于&#34;时,附在他们身上的JLabel不会显示。< / p>
我做错了什么..? 提前谢谢。
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class Menu extends JFrame
{
private static final long serialVersionUID = 1L;
public Menu()
{
// Creating a new JFrame and setting stuff
JFrame frame = new JFrame("BREAK THE BRICKS - MENU");
frame.setResizable(false);
frame.setBounds(43, 10, 1280, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Creating a menu panel
JPanel menupanel = new JPanel();
menupanel.setLayout(null);
setContentPane(menupanel);
frame.add(menupanel);
//PRINCIPAL BUTTONS OF THE MENU
//Creating buttons
JButton buttonrules = new JButton();
JButton buttonoptions = new JButton();
JButton buttonabout = new JButton();
JButton buttonplay = new JButton("PLAY");
//Setting their bounds
buttonrules.setBounds(56, 224, 400, 83);
buttonoptions.setBounds(56, 302, 400, 82);
buttonabout.setBounds(56, 379, 400, 83);
buttonplay.setBounds(56, 486, 400, 110);
//Setting their border's color
buttonrules.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5));
buttonoptions.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5));
buttonabout.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5));
buttonplay.setBorder(BorderFactory.createLineBorder(Color.GRAY, 5));
//Setting their content and font
buttonrules.setContentAreaFilled(false);
buttonoptions.setContentAreaFilled(false);
buttonabout.setContentAreaFilled(false);
buttonplay.setFont(new Font("Mesquite Std", Font.PLAIN, 99));
//Adding them to the principal panel
menupanel.add(buttonrules);
menupanel.add(buttonoptions);
menupanel.add(buttonabout);
menupanel.add(buttonplay);
//BACKGROUND MENU'S IMAGE
//Attaching the principal background image to the principal panel
JLabel labelbackground = new JLabel();
menupanel.add(labelbackground);
labelbackground.setBounds(0, 0, 1280, 720);
Image background = new ImageIcon(this.getClass().getResource("/Menu_Principal.jpg")).getImage();
labelbackground.setIcon(new ImageIcon(background));
//BOXES ON THE RIGHT-HAND SIDE OF THE SCREEN
//RULES AND CONTROLS
//Creating a JLabel and setting stuff
JLabel labelboxrules = new JLabel();
labelboxrules.setForeground(Color.WHITE);
labelboxrules.setBounds(475, 159, 754, 500);
//Importing rules and controls' image and setting it to its label
Image rulandconimg = new ImageIcon(this.getClass().getResource("/Rules_And_Controls.jpg")).getImage();
labelboxrules.setIcon(new ImageIcon(rulandconimg));
//OPTIONS
//Creating a JLabel and setting stuff
JLabel labelboxoptions = new JLabel();
labelboxoptions.setForeground(Color.WHITE);
labelboxoptions.setBounds(475, 159, 754, 500);
//Importing options' image and setting it to its label
Image optionsimg = new ImageIcon(this.getClass().getResource("/Options.jpg")).getImage();
labelboxoptions.setIcon(new ImageIcon(optionsimg));
//ABOUT
//Creating a JLabel and setting stuff
JLabel labelboxabout = new JLabel();
labelboxabout.setForeground(Color.WHITE);
labelboxabout.setBounds(475, 159, 754, 500);
//Importing about's image and setting it to its label
Image aboutimg = new ImageIcon(this.getClass().getResource("/About.jpg")).getImage();
labelboxabout.setIcon(new ImageIcon(aboutimg));
//THEIR FUTURE BORDER
Border boxborder = BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5);
//ASSOCIATING ACTIONS WITH MENU'S BUTTONS
//Rules and Controls
buttonrules.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent a)
{
labelboxrules.setBorder(boxborder);
labelbackground.add(labelboxrules);
labelboxrules.setVisible(true);
}
});
//Options
buttonoptions.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent a)
{
labelboxoptions.setBorder(boxborder);
labelbackground.add(labelboxoptions);
labelboxoptions.setVisible(true);
}
});
//About
buttonabout.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent a)
{
labelboxabout.setBorder(boxborder);
labelbackground.add(labelboxabout);
labelboxabout.setVisible(true);
}
});
//Play
buttonplay.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent c)
{
Game.myGame();
}
});
}
}
答案 0 :(得分:1)
添加组件后,您必须revalidate
和repaint
面板。但是当不使用布局管理器时,camicker和madprogrammer指出revalidate
毫无意义。如果您使用布局管理器,那么你需要在重新绘制之前调用revalidate
。
与jframe相比,默认的jlables也是可见的,因此调用labelboxoptions.setVisible(true);
是多余的。
例如
buttonoptions.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
labelboxoptions.setBorder(boxborder);
labelbackground.add(labelboxoptions);
labelboxoptions.setVisible(true);
menupanel.repaint();
}
});
请注意: 不使用null布局。使用布局管理器。。
答案 1 :(得分:-1)
正如Andrew和MadProgrammer所建议的
不要使用setLayout(null)
,
更新并删除了不需要的声明