基于组合框选项显示JLabel

2022-08-15 00:00:00 java swing jlabel jpanel mouselistener

如何根据组合框选项显示JLabel。当我运行代码时,所有的JLabel都会立即显示出来。

combo1.addActionListener(this);
panel.add(combo1);
panel.add(img1);
panel.add(img2);
panel.add(img3);

frame.add(panel);

    img1.setText("<html> Image : <a href="">Image1/</a></html>");   
    img2.setText("<html> Image : <a href="">Image2/</a></html>");
    img3.setText("<html> Image : <a href="">Image3/</a></html>");

    img1.setCursor(new Cursor(Cursor.HAND_CURSOR));
    img2.setCursor(new Cursor(Cursor.HAND_CURSOR));
    img3.setCursor(new Cursor(Cursor.HAND_CURSOR));

我使用MouseListener检查哪个标签被传递给goWebsite()函数,然后根据该标签添加一个超链接。

private void goWebsite(final JLabel website) {
        website.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(website == img1){
                try {
                    Desktop.getDesktop().browse(new URI("http://2.bp.blogspot.com/_2dxp9ORKKAM/TBZpViy7O1I/AAAAAAAABGY/zitq3ZLA8K4/s1600/red.png"));
                } catch (URISyntaxException | IOException ex) {
                    //It looks like there's a problem
                }
            }
                if(website == img2){
                    try {
                        Desktop.getDesktop().browse(new URI("http://www.pratikbagaria.com/wp-content/uploads/2011/04/BlueGreenPink.jpg"));
                    } catch (URISyntaxException | IOException ex) {
                        //It looks like there's a problem
                    }
                }
                if(website == img3){
                    try {
                        Desktop.getDesktop().browse(new URI("http://i.imgur.com/9OPnZNk.png"));
                    } catch (URISyntaxException | IOException ex) {
                        //It looks like there's a problem
                    }
                }
                if(website == img4){
                    try {
                        Desktop.getDesktop().browse(new URI("http://www.solidbackgrounds.com/images/three/2048x2048/2048x2048-fluorescent-orange-fluorescent-pink-fluorescent-yellow-three-color-background.jpg"));
                    } catch (URISyntaxException | IOException ex) {
                        //It looks like there's a problem
                    }
                }

操作执行检查用户从组合框中选择了哪个选项,然后将正确的img JLabel传递给goWebsite()函数。

@Override
    public void actionPerformed(ActionEvent e)
    {
        String color1 = (String)combo1.getSelectedItem();
        // Possibly check if either color is 'null' here
        if (color1.equals("red") )
        {

            goWebsite(img1);
        }
        if (color1.equals("blue") )
        {

            goWebsite(img2);
        }
        if (color1.equals("green") )
        {

            goWebsite(img3);
        }
}

解决方案

尝试将ItemListener添加到JComboBox,在调用itemStateChanged时,检查ItemEvent#getStateChanged是否等于ItemEvent.SELECTED

选中组合框中选定的内容(或使用ItemEvent#ItemSelected),然后使用所需信息更新单个JLabel或使用JLabel#setVisible(true)使与所选项目关联的当前标签可见,但也确保隐藏其他标签

例如...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ComboBoxUpdate {

    public static void main(String[] args) {
        new ComboBoxUpdate();
    }

    public ComboBoxUpdate() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JComboBox comboBox;
        private JLabel option1;
        private JLabel option2;
        private JLabel option3;

        public TestPane() {
            comboBox = new JComboBox(new String[]{"Choice 1", "Choice 2", "Choice 3"});
            option1 = new JLabel("Bananas");
            option2 = new JLabel("Appels");
            option3 = new JLabel("Grapes");
            option1.setVisible(false);
            option2.setVisible(false);
            option3.setVisible(false);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(comboBox, gbc);
            add(option1, gbc);
            add(option2, gbc);
            add(option3, gbc);

            comboBox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    switch (e.getStateChange()) {
                        case ItemEvent.SELECTED:
                            Object value = comboBox.getSelectedItem();
                            option1.setVisible(false);
                            option2.setVisible(false);
                            option3.setVisible(false);
                            if ("Choice 1".equals(value)) {
                                option1.setVisible(true);
                            } else if ("Choice 2".equals(value)) {
                                option2.setVisible(true);
                            } else if ("Choice 3".equals(value)) {
                                option3.setVisible(true);
                            }
                            break;
                    }
                }
            });

            comboBox.setSelectedItem(null);
        }
    }
}

相关文章