问题描述
所以我正在尝试为我正在开发的游戏制作菜单。
我想将图像作为背景放置在menuPanel
上,但是我不知道每次抬起窗口时如何让图像重新缩放。我做了一个JLabel
,并且从主要方法中导入了一个图像,当我启动游戏时,我可以看到该图像已正确导入,但是我想填满所有menuPanel
并拉伸为我将窗口增大到全屏或减小到我的框架的最小尺寸。
我该怎么办?
如您在屏幕截图中所见,我希望文本位于图像顶部,并且图像作为背景和全屏显示。
public class Window extends Canvas{
private static final long serialVersionUID = 6331412385749386309L;
private static final int WIDTH = 1024,HEIGHT = WIDTH / 16 * 9;
private JFrame frame;
private JPanel mainPanel;
private JPanel menuPanel;
private JPanel buttonsPanel;
private JPanel playPanel;
private JPanel optionsPanel;
private JButton playBtn;
private JButton optionsBtn;
private JButton quitBtn;
private int currWidth = WIDTH,currHeight = HEIGHT;
public Window(String title,Game game) {
frame = new JFrame(title);
frame.setSize(1024,576);
frame.setMinimumSize(new Dimension(WIDTH,HEIGHT));
frame.requestFocus();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
menu();
game.start();
}
private void menu() {
frame.getContentPane().setLayout(new BorderLayout(0,0));
mainPanel = new JPanel();
mainPanel.setBackground(new Color(255,255,255));
frame.getContentPane().add(mainPanel);
mainPanel.setLayout(new CardLayout(0,0));
// menuPanel config
menuPanel = new JPanel();
menuPanel.setForeground(new Color(0,0));
menuPanel.setBackground(new Color(0,0));
mainPanel.add(menuPanel,"menuPanel");
buttonsPanel = new JPanel();
buttonsPanel.setBorder(null);
buttonsPanel.setBackground(new Color(0,0));
// playBtn config
playBtn = new JButton("Play");
playBtn.setForeground(new Color(255,255));
playBtn.setFont(new Font("Segoe Script",Font.BOLD,40));
playBtn.setOpaque(false);
playBtn.setContentAreaFilled(false);
playBtn.setBorderPainted(false);
playBtn.setFocusPainted(false);
// optionsBtn config
optionsBtn = new JButton("Options");
optionsBtn.setForeground(new Color(255,255));
optionsBtn.setFont(new Font("Segoe Script",35));
optionsBtn.setOpaque(false);
optionsBtn.setContentAreaFilled(false);
optionsBtn.setBorderPainted(false);
optionsBtn.setFocusPainted(false);
//quitBtn config
quitBtn = new JButton("Quit");
quitBtn.setForeground(new Color(255,255));
quitBtn.setFont(new Font("Segoe Script",35));
quitBtn.setOpaque(false);
quitBtn.setContentAreaFilled(false);
quitBtn.setBorderPainted(false);
quitBtn.setFocusPainted(false);
GroupLayout gl_buttonsPanel = new GroupLayout(buttonsPanel);
gl_buttonsPanel.setHorizontalGroup(
gl_buttonsPanel.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_buttonsPanel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_buttonsPanel.createParallelGroup(Alignment.LEADING)
.addComponent(quitBtn,GroupLayout.DEFAULT_SIZE,175,Short.MAX_VALUE)
.addComponent(playBtn,Short.MAX_VALUE)
.addComponent(optionsBtn,Short.MAX_VALUE))
.addContainerGap())
);
gl_buttonsPanel.setVerticalGroup(
gl_buttonsPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_buttonsPanel.createSequentialGroup()
.addContainerGap()
.addComponent(playBtn)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(optionsBtn,GroupLayout.PREFERRED_SIZE,74,GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(quitBtn,71,GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))
);
buttonsPanel.setLayout(gl_buttonsPanel);
//
JLabel menuImageLabel = new JLabel(new ImageIcon(Game.menu_image.getScaledInstance(700,400,Image.SCALE_FAST)));
//
GroupLayout gl_menuPanel = new GroupLayout(menuPanel);
gl_menuPanel.setHorizontalGroup(
gl_menuPanel.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_menuPanel.createSequentialGroup()
.addComponent(menuImageLabel,762,GroupLayout.PREFERRED_SIZE)
.addGap(0)
.addComponent(buttonsPanel,195,Short.MAX_VALUE))
);
gl_menuPanel.setVerticalGroup(
gl_menuPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_menuPanel.createSequentialGroup()
.addGap(161)
.addComponent(buttonsPanel,Short.MAX_VALUE)
.addGap(124))
.addComponent(menuImageLabel,537,Short.MAX_VALUE)
);
menuPanel.setLayout(gl_menuPanel);
// playPanel config
playPanel = new JPanel();
playPanel.setBackground(new Color(0,255));
mainPanel.add(playPanel,"playPanel");
// optionsPanel config
optionsPanel = new JPanel();
optionsPanel.setBackground(new Color(255,0));
mainPanel.add(optionsPanel,"optionsPanel");
frame.setVisible(true);
setActions();
}
private void setActions() {
// playBtn action
playBtn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
playBtn.setForeground(new Color(200,210,10));
}
public void mouseExited(MouseEvent e) {
playBtn.setForeground(new Color(255,255));
}
public void mouseClicked(MouseEvent e) {
menuPanel.setVisible(false);
playPanel.setVisible(true);
optionsPanel.setVisible(false);
}
});
// optionsBtn action
optionsBtn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
optionsBtn.setForeground(new Color(200,10));
}
public void mouseExited(MouseEvent e) {
optionsBtn.setForeground(new Color(255,255));
}
public void mouseClicked(MouseEvent e) {
menuPanel.setVisible(false);
playPanel.setVisible(false);
optionsPanel.setVisible(true);
}
});
// quitBtn action
quitBtn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
quitBtn.setForeground(new Color(200,10));
}
public void mouseExited(MouseEvent e) {
quitBtn.setForeground(new Color(255,255));
}
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
}
public void tick() {
mainPanel.getSize(new Dimension(currWidth,currHeight));
System.out.println(currWidth + "," + currHeight);
}
public void render() {
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)