使用JComboBox选择切换图像

时间:2015-07-21 18:03:59

标签: java swing user-interface

我是Java的新手,想要解决这个问题。我在这里放置测试代码。

我有两个JFrame“Frame 1”和“Settings”。第1帧有一个用于运行“设置”的按钮和一个用于预览图像的JLabel。

“设置”有一个JComboBox来选择Image_1,Image_2 ......如何通过使用放置在设置上的JCombobox执行的操作来更改第1帧上的图像?如果这是毫无意义的我很抱歉..谢谢。

我知道怎么用同一个班级来做。由于这些分开的类,我面临的问题。重复标记的问题要求在同一个JFrame中执行此操作。你能帮忙吗?

第1帧:

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Frame1 extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame1 frame = new Frame1();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Frame1() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        Image imgUniLogo = new ImageIcon(this.getClass().getResource("/Image1.png")).getImage();

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setBounds(280, 71, 79, 93);
        contentPane.add(lblNewLabel);
        lblNewLabel.setIcon(new ImageIcon(imgUniLogo));

        JButton btnNewButton = new JButton("Settings");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Frame2 frame = new Frame2();
                frame.setVisible(true);
            }
        });
        btnNewButton.setBounds(54, 209, 107, 23);
        contentPane.add(btnNewButton);
    }
}

设置:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;


public class Settings extends JFrame {

    private JPanel contentPane;

    public Settings() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"Image 1", "Image 2"}));
        comboBox.setBounds(140, 117, 151, 20);
        contentPane.add(comboBox);
    }
}

0 个答案:

没有答案