gpt4 book ai didi

java - 刽子手图片减半

转载 作者:行者123 更新时间:2023-12-03 07:54:29 25 4
gpt4 key购买 nike

public Hangman(){

Container content =getContentPane();
content.setLayout(new GridLayout(0,1));

btnAddWord.addActionListener(this);
btnRestart.addActionListener(this);
btnHelp.addActionListener(this);
btnExit.addActionListener(this);

panel2.add(label);
panel3.add(word);
panel4.add(btnAddWord);
panel4.add(btnRestart);
panel4.add(btnHelp);
panel4.add(btnExit);

public static void main (String [] args){
Hangman frame = new Hangman();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 600);
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.CENTER);
frame.add(panel3, BorderLayout.SOUTH);
frame.add(panel4, BorderLayout.SOUTH);
}

好吧,我想出了如何在单击错误的按钮时更改图片,但问题是图片是一半,所以我的布局有问题...

enter image description here

最佳答案

好的,所以我很快就把它擦掉了。

public class HangPane extends JPanel {

private JLabel lblImage;
private JButton btnAction;
private int guess = 0;

public HangPane() {

setLayout(new BorderLayout());
lblImage = new JLabel();
lblImage.setPreferredSize(new Dimension(46, 172));
lblImage.setHorizontalAlignment(JLabel.CENTER);
lblImage.setVerticalAlignment(JLabel.CENTER);
btnAction = new JButton("Guess");

String imageName = "HG0.png";
lblImage.setIcon(new ImageIcon(imageName));


btnAction.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

String imageName = "HG0.png";

guess++;
switch (guess) {
case 1:
imageName = "HG1.png";
break;
case 2:
imageName = "HG2.png";
break;
case 3:
imageName = "HG3.png";
break;
case 4:
imageName = "HG4.png";
break;
case 5:
imageName = "HG5.png";
break;
case 6:
imageName = "HG6.png";
break;
default:
guess = 0;
break;
}

lblImage.setIcon(new ImageIcon(imageName));

}
});

add(lblImage, BorderLayout.CENTER);
add(btnAction, BorderLayout.SOUTH);

}
}

Guess00 Guess01 Guess02 Guess03 Guess04 Guess05 Guess06

这只会让您点击并单独查看每个步骤。

一些想法。

您可能需要设置标签的首选大小(尽管老实说,我认为将标签放在 CENTER 位置会解决这个问题)。

我用的尺寸只适合我自己的图片,你需要自己想办法。

此外,尝试使用类似 LineBorder 的方式设置组件的边框。这会告诉您这是布局问题还是图像/读取问题。

关于java - 刽子手图片减半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12255030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com