【问题标题】:label.setVisible(true) in panel for images if correctlabel.setVisible(true) 如果正确,则在图像面板中
【发布时间】:2013-09-23 18:03:51
【问题描述】:
           for(i=0; i<16; i++){

                     images[i]=new ImageIcon(getClass()
    .getResource("/images/1 ("+i+").jpg"));

            }

 for( i=0; i<buttons.length; i++){
                for (j=0; j<buttons[i].length;j++){ 
                    n=i*buttons.length+buttons[i].length;
                    buttons[i][j]=new JButton();
                    label[n].setIcon((images[i*buttons.length+j]));
                    buttons[i][j].add(label[n]);
                    label[n].setVisible(false);


                    panel.add(buttons[i][j]);
                    buttons[i][j].addActionListener(this);

                }
            }




    public void actionPerformed(ActionEvent e) {
        if(e.getSource() instanceof JButton){
            JButton pressedButton = (JButton) e.getSource();
            opens[open]=(JButton) e.getSource(); //i want to put label[n] into array?
            if((pressedButton.getIcon() == null)){

                label[n].setVisible(true);

                open=open++;
            } else {   
                //pressedButton.setIcon(null);
            }

            }
        if (open==1){
            opens[0].setVisible(false);
            opens[1].setVisible(false);
        }
    }

你好朋友。我正在做一个记忆游戏,如果按钮有相同的图标,它们将保持打开状态。

我在它的面板中制作了框架,在它的按钮中,每个按钮都有标签。如果 face 为 true 并且用户点击,他们将通过 setvisible(true) 打开

但是在 void actionperformed 中,我该如何接受label[n]? Not button[][]?

label[n].setIcon((images[i*buttons.length+j]));

我认为是错误。它不正确吗?因为它不执行。

根据建议编辑:

 for(i=0; i<16; i++){

             images[i]=new ImageIcon(getClass().getResource("/images/1 ("+i+").jpg"));

    } //adding images to local variables

            for( i=0; i<buttons.length; i++){
                for (j=0; j<buttons[i].length;j++){ 
                    n=i*buttons.length+buttons[i].length;
                    buttons[i][j]=new JButton();
                //buttons[i][j].setIcon((images[i*buttons.length+j]));
    //if i make this code, all icons are displayed at first?

                    panel.add(buttons[i][j]);
                    buttons[i][j].addActionListener(this);

                }
            }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() instanceof JButton){
            JButton pressedButton = (JButton) e.getSource();
            if(pressedButton.getIcon() == null){
                pressedButton.setIcon((images[i*buttons.length+j]));
            } else {
                pressedButton.setIcon(null);
            }
        }
    }

我按照您的建议编写了此代码。但是现在图像不显示并且点击不起作用。

【问题讨论】:

  • 你有什么异常吗?到底发生了什么?
  • 我删除了我认为可疑的代码,它显示的图像没有标签。在这段代码中,当我运行时,什么也没有发生,没有错误,没有警告。只是进程中的一个jawa.exe,等待结束
  • 请再看一遍,我编辑了问题
  • @clarasampson:您需要创建并发布一个sscce,这是一个简单的程序,我们可以不加改动地编译和运行它并显示您的问题。

标签: java image swing jbutton


【解决方案1】:

我认为这篇文章与this question 有关。也许是一个任务?

几个提示:

label[n].setIcon((images[i*buttons.length+j]));

images 数组中有多少张图片?一个快速的数学运算会说,如果你有一个 4x4 数组,那么在最后一次迭代中你将需要 i*buttons.lenght+j == 4*4+4 == 20 图像。而且你只需要i*j/2 == 8 图像,假设是一对配对游戏。

如果按钮具有相同的图标,它们将保持打开状态。我在里面做了框架 面板,里面有按钮,每个按钮都有标签。

为什么需要标签?我认为如果两个按钮匹配,您可以禁用这些按钮,这样用户将无法再次单击它们并发送 actionPerformed 事件。

如果不是绝对必要使用数组,您可以改用ArrayList 并从其方法中获益。

更新

我发布此代码是因为我认为您仍然坚持使用数组。我只是想告诉你,没有必要使用它们来保持引用,你只需要在对象范式下多想一点,并将这个任务委托给适当的对象。

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;


public class Demo {    
    /* 
     * This variable will point to a pressed button to keep reference.
     */
    JButton _alreadyPressedButton = null;

    /**
     * Initializes the GUI
     */
    private void initGUI(){
        /*
         * Create needed icons - As this example uses 6 buttons, then I need 3 icons 
         */
        ImageIcon icon1 = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
        ImageIcon icon2 = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
        ImageIcon icon3 = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");
        /*
         * Make a list with 6 icons (add each icon twice)
         */
        List<ImageIcon> iconsList = new ArrayList<>();
        iconsList.add(icon1);
        iconsList.add(icon1);
        iconsList.add(icon2);
        iconsList.add(icon2);
        iconsList.add(icon3);
        iconsList.add(icon3);
        Collections.shuffle(iconsList); /* Shuffle the list */

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if(e.getSource() instanceof JButton){                    
                    final JButton pressedButton = (JButton) e.getSource();              
                    /* 
                     * Execute this code in a SwingWorker to prevent block EDT at "Thread.sleep(500)" line
                     * Google for EDT (Event Dispatch Thread)
                     */
                    SwingWorker sw = new SwingWorker() {
                        @Override
                        protected Object doInBackground() throws Exception {
                            if(_alreadyPressedButton != pressedButton){
                                pressedButton.setIcon(pressedButton.getPressedIcon());

                                if(_alreadyPressedButton != null){ 
                                    Thread.sleep(500);
                                    if(_alreadyPressedButton.getIcon() == pressedButton.getIcon()){
                                        _alreadyPressedButton.setEnabled(false);
                                        pressedButton.setEnabled(false);
                                    } else {
                                        _alreadyPressedButton.setIcon(null);
                                        pressedButton.setIcon(null);
                                    }
                                    _alreadyPressedButton = null;
                                } else {
                                    _alreadyPressedButton = pressedButton;
                                }

                            }
                            return null;
                        }

                    };

                    sw.execute();
                }
            }
        };

        JPanel panel = new JPanel(new GridLayout(3, 2));
        panel.setPreferredSize(new Dimension(200, 200));

        for(ImageIcon icon : iconsList){
            JButton button = new JButton();
            button.setPressedIcon(icon);
            button.addActionListener(actionListener);
            panel.add(button);
        }

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                 new Demo().initGUI();
            }
        });        

    }
}

【讨论】:

  • 但是如何隐藏和显示图像,我认为使用 jlabels 可以很容易地在该问题中显示没有 jbutton 的图像 pressButton.setIcon(null);但是当再次点击时,如何进入索引?
  • 您可以使用JButton.setIcon() 方法。例如:我单击一个按钮,设置了图标以便我可以看到图像。然后我单击另一个按钮。如果两个图像匹配,则禁用两个按钮。如果没有,则将两个按钮的图标设置为 null
  • 做null后怎么找到那个按钮的索引图?没有去掉?
  • @clarasampson 我的错。我在想你可以一起使用JButton.setPressedIconJButton.setIcon。当我点击一个按钮然后button.setIcon(button.getPressedIcon())。现在,当我单击另一个按钮时,如果两者都匹配则禁用它们。如果没有设置图标为null
【解决方案2】:

您似乎正在将 JLabel 添加到 JButton?如果这实际上是您正在做的事情,请不要这样做。相反,当您希望显示或更改按钮显示的图像(如果有)时,为什么不简单地设置 JButton 的 ImageIcon。您只需通过myButton.setIcon(fooIcon) 即可完成此操作。


编辑
您在评论中声明:

它将如何显示来自 jbutton 的图像并隐藏它?

您可以简单地交换图标。如果要显示图像,请将其 ImageIcon 通过setIcon(myIcon) 设置为 JButton 的图标。完成后,通过setIcon(otherIcon)setIcon(null) 将其换掉。

您提供的链接并未将 JLabels 放在您尝试执行的 JButton 之上,这就是我告诉您此信息的原因。

【讨论】:

  • 因为当点击jbutton时,它会显示图像。如果2个图像不相同,它们将再次关闭。我认为它会通过 jlabel 的 setvisible(true) 显示图像。
  • @clarasampson:你想错了。同样,不要将 JLabels 与 JButton 混为一谈。同样,只需设置 JButton 的图标。
  • 它将如何显示来自 jbutton 的图像并隐藏它?我从这里看到stackoverflow.com/a/9722205/2807930
  • @clara sampson please are you team, or ???,因为我找不到代码和描述之间的任何差异...,如果没有,那么真正的问题在哪里
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 2014-06-18
  • 2015-05-28
  • 2020-06-25
  • 1970-01-01
  • 2017-04-24
相关资源
最近更新 更多