图像作为JLabel中的变量,用于if-else

时间:2013-11-06 08:06:25

标签: java swing if-statement

我是新手所以请耐心等待

我想要什么

我有一个JPanel,我有一个JButton,一个JLabel和一个JTextArea。按下JButton后,必须在JLabel内部打印图像(以及JTextArea中的某些文本)。这个特定的图像(和文本)由if-else语句决定。 if-else的条件基于整数变量R。

基本上它是一项调查,就像我正在尝试提出的问答方式。我用R来记录用户的答案。当用户单击某个选项时,R的值会更新。

它适用于文本,但不适用于图像。

对于文本,我使用字符串变量你的电话。如果最后R的值是120,那么你的手机会更新为一个字符串,例如。 Xperia Z。

我想要一个类似的变量我可以用于图像,这样当用户点击JButton时就会显示Xperia Z的图片。

R的总值用于if-else语句。

结构

我发起像这样的变量

int R=0;
String yourphone;
ImageIcon imageresult;

我的JPanel用于显示结果

final JPanel result = new JPanel();
    result.setBackground(Color.BLACK);
    getContentPane().add(result, "name_17130054294139");
    result.setLayout(null);


    final JTextArea txtrphoneresult = new JTextArea();
    txtrphoneresult.setBackground(Color.BLACK);
    txtrphoneresult.setForeground(Color.YELLOW);
    txtrphoneresult.setFont(new Font("Tahoma", Font.PLAIN, 14));
    txtrphoneresult.setBounds(448, 515, 469, 121);
    result.add(txtrphoneresult);


    JLabel resultlabel = new JLabel(imageresult);
    resultlabel.setBounds(292, 122, 782, 346);
    result.add(resultlabel);




    JButton btnShowResult = new JButton("Show Result");
    btnShowResult.setFont(new Font("Tahoma", Font.PLAIN, 10));
    btnShowResult.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if(R==1726)
            {
                yourphone = "Samsung Galaxy S4\r\nHTC One\r\nSony Xperia Z";
                ImageIcon imageresult = new ImageIcon("galaxy_one_XperiaZ.jpg");
            }
            else if(R==5002)
            {
                yourphone = "Sony Xperia Z1\r\nSamsung Galaxy Note 3";
                ImageIcon imageresult = new ImageIcon("Note3_sonyZ1.jpg");
            }
            else
            {
                yourphone = "No Results";
            }

            txtrphoneresult.setText(yourphone);

     }
     });
    btnShowResult.setBounds(618, 48, 130, 32);
    result.add(btnShowResult);

问题

根本不显示图像。 如果有任何其他可能的方法来实现这一点,请指导。

5 个答案:

答案 0 :(得分:1)

if语句之前声明ImageIcon imageresult并调用

resultlabel.setIcon(imageresult)
txtrphoneresult.setText(yourphone);

之后

BTW:不要使用null layout / setBounds()。阅读有关布局的内容并选择合适的布局。

答案 1 :(得分:1)

您在哪里为标签设置图标。 你把这条线搞定了

resultlabel.setIcon(imageresult);

答案 2 :(得分:1)

resultLabel成为最终并更改已执行的操作:

public void actionPerformed(ActionEvent e)
            {
                ImageIcon imageresult = null;
                if(R == 1726)
                {
                    yourphone = "Samsung Galaxy S4\r\nHTC One\r\nSony Xperia Z";
                    imageresult = new ImageIcon("galaxy_one_XperiaZ.jpg");
                }
                else if(R == 5002)
                {
                    yourphone = "Sony Xperia Z1\r\nSamsung Galaxy Note 3";
                    imageresult = new ImageIcon("Note3_sonyZ1.jpg");
                }
                else
                {
                    yourphone = "No Results";
                }

                txtrphoneresult.setText(yourphone);
                resultlabel.setIcon(imageresult);

            }

答案 3 :(得分:1)

首先,您在imageresult内创建if-else,并且所有方法都看不到它。并且您不会将图像添加到JLabel

将resultlabel设为clas成员或final变量。 以下一种方式更改您的代码:

 public void actionPerformed(ActionEvent e) {

        ImageIcon imageresult = null;
        if(R==1726)
        {
            yourphone = "Samsung Galaxy S4\r\nHTC One\r\nSony Xperia Z";
            imageresult = new ImageIcon("galaxy_one_XperiaZ.jpg");
        }
        else if(R==5002)
        {
            yourphone = "Sony Xperia Z1\r\nSamsung Galaxy Note 3";
            imageresult = new ImageIcon("Note3_sonyZ1.jpg");
        }
        else
        {
            yourphone = "No Results";
        }

        resultlabel.setIcon(imageresult)
        txtrphoneresult.setText(yourphone);

 }

答案 4 :(得分:1)

图片未显示,因为您没有“给予”JLabel。您可以使用setIcon()方法完成此操作。

您也有变量声明的问题。与PHP和其他一些编程语言不同,在Java中,变量仅存在于已声明的括号(和子括号){}中(尽管有一些例外)。例如:

public void myMethod() {
    float b = 5f;

    if (true) {
        int a = 6;

        if (true) {
            // a exists, you can do whatever you like with it
            // b exists, you can do whatever you like with it
        }
        // a exists you can do whatever you like with it
        // b exists, you can do whatever you like with it

    } // a is "destroyed"

    // a doesn't exists
    // b still exists
} // b is "destroyed"
// Neither a or b exists

与变量imageresult的声明完全相同。

相关问题