gpt4 book ai didi

java - 图像堆叠在一起

转载 作者:太空宇宙 更新时间:2023-11-04 15:07:07 24 4
gpt4 key购买 nike

我正在完成我的 DiceRollGUI,当我运行它并按“滚动”彼此顶部的图像堆栈时,如何使其在再次按下按钮后消失?

我做了 MadProgrammer 告诉我的大部分内容,但遇到了以下错误:

--------------------Configuration: <Default>--------------------
G:\DiceRollGUI\src\DiceRollGUI.java:26: error: constructor RollButton in class RollButton cannot be applied to given types;
button.addActionListener(new RollButton(diceRoll));
^
required: JPanel
found: JLabel
reason: actual argument JLabel cannot be converted to JPanel by method invocation conversion
G:\DiceRollGUI\src\DiceRollGUI.java:42: error: cannot find symbol
contentPane.add(diceRoll);
^
symbol: variable contentPane
location: class RollButton
2 errors

Process completed.

Dice Stack

代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;

public class DiceRollGUI {
public static JLabel label;
public static JFrame frame;
public static JPanel panel;
private static JButton button;
private static JButton buttonRollDie;
private static JLabel diceRoll;

public static void main (String[] args) {
JFrame frame = new JFrame("Dice Roll GUI");
panel = new JPanel();
JPanel contentPanel = new JPanel(new GridLayout(0,2,5,10));
button = new JButton("Roll");;

frame.setVisible(true);
frame.setResizable(false);
frame.setSize(1000, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setActionCommand("Roll");
button.addActionListener(new RollButton(diceRoll));
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.setPreferredSize(new Dimension(750, 500));
frame.setContentPane(panel);
frame.pack();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(button);
}

private static class RollButton implements ActionListener {

private JPanel diceRoll;

public RollButton(JPanel diceRoll){
this.diceRoll = diceRoll;
contentPane.add(diceRoll);
}
public void actionPerformed(ActionEvent event){
int roll = (int) (Math.round((Math.random() * 5) + 1));
ImageIcon dice = null;

if(roll == 1){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_1.png");
}
else if(roll == 2){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_2.png");
}
else if(roll == 3){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_3.png");
}
else if(roll == 4){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_4.jpeg");
}
else if(roll == 5){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_5.png");
}
else if(roll == 6){
dice = new ImageIcon("DiceRollGUI Pictures/die_face_6.png");
}
JLabel diceRoll = new JLabel("",dice, JLabel.CENTER);
panel.add(diceRoll);
panel.revalidate();
}
}
}

最佳答案

1- 在构造函数中,创建 diceRoll JLabel 的实例并将其添加到 panel

diceRoll = new JLabel();
contentPane.add(diceRoll);

2- 不要将 contentPane 的引用传递给 RollButton ActionListener,而是传递对 diceRoll 的引用> 相反...

button.addActionListener(new RollButton(diceRoll));

3-更改您的 RollButton ActionListener 以支持使用 JLabel 而不是 JPanel...

private static class RollButton implements ActionListener {

private JLabel diceRoll;

public RollButton(JLabel diceRoll){
this.diceRoll= diceRoll;
}

4- 在您的 actionPerformed 方法中,只需更改 diceRoll 的图标属性...

public void actionPerformed(ActionEvent event){
int roll = (int) (Math.round((Math.random() * 5) + 1));
ImageIcon dice = null;
//...
diceRoll.setIcon(dice);
}

您可能会发现在 contentPane 上使用 BorderLayoutGridBagLayout 会产生很好的效果。您可能还想研究 horizo​​ntalAligmentverticalAligment 属性

更新了可编译示例

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DiceRollGUI {

public static JLabel label;
public static JFrame frame;
public static JPanel panel;
private static JButton button;
private static JButton buttonRollDie;
private static JLabel diceRoll;

public static void main(String[] args) {
JFrame frame = new JFrame("Dice Roll GUI");
panel = new JPanel();
JPanel contentPanel = new JPanel(new GridLayout(0, 2, 5, 10));
button = new JButton("Roll");

// !! Add this !! //
diceRoll = new JLabel();
contentPanel.add(diceRoll);

frame.setVisible(true);
frame.setResizable(false);
frame.setSize(1000, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setActionCommand("Roll");
button.addActionListener(new RollButton(diceRoll));
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.setPreferredSize(new Dimension(750, 500));
frame.setContentPane(panel);
frame.pack();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(button);
}

private static class RollButton implements ActionListener {

// !! Change This !! //
private JLabel diceRoll;

// !! Change This !! //
public RollButton(JLabel diceRoll) {
// !! Change This !! //
this.diceRoll = diceRoll;
}

public void actionPerformed(ActionEvent event) {
int roll = (int) (Math.round((Math.random() * 5) + 1));
ImageIcon dice = null;

if (roll == 1) {
dice = new ImageIcon("DiceRollGUI Pictures/die_face_1.png");
} else if (roll == 2) {
dice = new ImageIcon("DiceRollGUI Pictures/die_face_2.png");
} else if (roll == 3) {
dice = new ImageIcon("DiceRollGUI Pictures/die_face_3.png");
} else if (roll == 4) {
dice = new ImageIcon("DiceRollGUI Pictures/die_face_4.jpeg");
} else if (roll == 5) {
dice = new ImageIcon("DiceRollGUI Pictures/die_face_5.png");
} else if (roll == 6) {
dice = new ImageIcon("DiceRollGUI Pictures/die_face_6.png");
}
// !! Change This !! //
diceRoll.setIcon(dice);
}
}
}

5-您实际上从未将 contentPane 添加到任何内容...更改您的 main 使其看起来更像..

public static void main(String[] args) {
JFrame frame = new JFrame("Dice Roll GUI");
panel = new JPanel();
JPanel contentPanel = new JPanel(new GridLayout(0, 2, 5, 10));
button = new JButton("Roll");

// !! Add this !! //
diceRoll = new JLabel();
contentPanel.add(diceRoll);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setActionCommand("Roll");
button.addActionListener(new RollButton(diceRoll));
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.setPreferredSize(new Dimension(750, 500));
panel.setLayout(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(button, BorderLayout.NORTH);
panel.add(contentPanel);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}

关于java - 图像堆叠在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21841545/

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