大家好,我会尽力解释这一点,因为这很难解释。我正在进行一个项目,该项目要求用户选择他们希望多次出现的动物图像。但是我只能在同一JLabel(ImageBlock)上执行一次。这是我的代码,您可以理解。
主要动物
public static void main(String[] args) {
JFrame application = new JFrame("Animal Project");
GUI graphicalInterface = new GUI();
application.add(graphicalInterface);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setLocation(200, 200);
application.pack();
application.setVisible(true);
application.setResizable(false);
}
子类GUI
public class GUI extends JPanel implements ActionListener {
private JButton animalOption = new JButton();
private JPanel buttonPanel;
private JPanel imagePanel;
private ImageIcon bear;
private ImageIcon tiger;
private ImageIcon lion;
private JLabel imageBlock1;
private JLabel imageBlock2;
private JLabel imageBlock3;
GUI() {
Border blackline = BorderFactory.createLineBorder(Color.black);
//create button panel
buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(100, 70));
buttonPanel.setOpaque(true);
buttonPanel.setBackground(Color.white);
buttonPanel.setBorder(blackline);
imagePanel = new JPanel(new GridLayout(3, 3));
imagePanel.setPreferredSize(new Dimension(500, 500));
imagePanel.setOpaque(true);
imagePanel.setBackground(Color.white);
imagePanel.setBorder(blackline);
imageBlock1 = new JLabel();
imageBlock1.setPreferredSize(new Dimension(100, 100));
imageBlock1.setOpaque(true);
imageBlock1.setBackground(Color.white);
imageBlock1.setBorder(blackline);
imageBlock2 = new JLabel();
imageBlock2.setPreferredSize(new Dimension(100, 100));
imageBlock2.setOpaque(true);
imageBlock2.setBackground(Color.white);
imageBlock2.setBorder(blackline);
imageBlock3 = new JLabel();
imageBlock3.setPreferredSize(new Dimension(100, 100));
imageBlock3.setOpaque(true);
imageBlock3.setBackground(Color.white);
imageBlock3.setBorder(blackline);
bear = new ImageIcon("Bear.png");
tiger = new ImageIcon("Tiger.png");
lion = new ImageIcon("Lion.png");
animalOption = new JButton();
//add action listener to each button
animalOption.addActionListener(this);
//set button size
animalOption.setPreferredSize(new Dimension(100, 50));
//set text for each button
animalOption.setText("Animal");
animalOption.setToolTipText("press to select your animal");
//add buttons to gui
buttonPanel.add(animalOption);
this.add(buttonPanel);
this.add(imagePanel);
imagePanel.add(imageBlock1);
imagePanel.add(imageBlock2);
imagePanel.add(imageBlock3);
}
public void actionPerformed(ActionEvent e) {
int choice;
if (e.getSource().equals(animalOption)) { //add DVD Button
choice = selectAnimal();
if (choice == 1) {
imageBlock1.setIcon(bear);
} else if (choice == 2) {
imageBlock1.setIcon(tiger);
} else if (choice == 3) {
imageBlock1.setIcon(lion);
}
}
}
static int selectAnimal() {
int animal = 0;
String DVDYears = JOptionPane.showInputDialog("Please enter animal, type 1 for bear, type 2 for tiger, type 3 for lion");
animal = Integer.parseInt(DVDYears);
return animal;
}
}
运行代码后,按一下按钮,系统会提示我输入对话框,如果我输入“ 1”代表熊,我会得到这个,这正是我想要的!
]
但是那是问题所在,一旦我再次单击该按钮,它将不会进入下一个imageBlock,它将仅覆盖Bear所在的相同图像。显然,这是因为我没有放入代码,但是我不知道该怎么做,并且我在努力转移到下一个图像块。
是否需要多线程处理?我想用不同的动物和更多的imageBlock JLabels扩展这个项目,但是我现在开始变小,因此一旦知道如何进行扩展就可以扩展。任何人都可以帮助我,我已经在这个问题上困扰了好几天,我无法解决这个问题,我将不胜感激任何帮助和提示。
答案 0 :(得分:0)
忘记我的旧帖子了-我误会了您的要求。我想我现在明白了。我认为您想要的只是一个变量,用于存储要更改的图像块。试试这个:
将其与其他实例变量一起放在班级顶部:
private JLabel currImageBlock = null;
然后将您的actionPerformed()
方法更改为此
public void actionPerformed(ActionEvent e) {
int choice;
if (currImageBlock == null) {
currImageBlock = imageBlock1;
} else if (currImageBlock == imageBlock1) {
currImageBlock = imageBlock2;
} else if (currImageBlock == imageBlock2) {
currImageBlock = imageBlock3;
} else if (currImageBlock == imageBlock3) {
currImageBlock = imageBlock1;
}
if (e.getSource().equals(animalOption)) { //add DVD Button
choice = selectAnimal();
if (choice == 1) {
currImageBlock.setIcon(bear);
} else if (choice == 2) {
currImageBlock.setIcon(tiger);
} else if (choice == 3) {
currImageBlock.setIcon(lion);
}
}
}
这有点混乱,可能可以使用JLabel
个图像块数组来使其更加优雅,但是它确实满足您的要求(我认为。)它将循环显示将图像放置在每个图像块中。