I need to display an image when pressing on the button ; this is what i wrote bu
ID: 1924285 • Letter: I
Question
I need to display an image when pressing on the button ; this is what i wrote but it doesnt work ; i have 6 buttons and i only tried with one button . help pleaseimport java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Noura extends JFrame
{
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 200;
private JLabel imageLabel;
public Noura()
{
super("GridLayout");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,3));
JButton button1 = new JButton("HSS_105");
button1.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent event3 )
{
ImageIcon eeer = new ImageIcon("noura.jpg");
imageLabel.setIcon(eeer);
pack();
}
}
);
JButton button2 = new JButton("GENG_1103");
JButton button3 = new JButton("ESPU_107");
JButton button4 = new JButton("GENG_220");
JButton button5 = new JButton("GENG_310");
JButton button6 = new JButton("MATH_1094");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
panel1.add(button1);
panel2.add(button2);
panel3.add(button3);
panel4.add(button4);
panel5.add(button5);
panel6.add(button6);
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
add(panel6);
setVisible(true);
}
public static void main(String[] args)
{
new Noura();
}
}
Explanation / Answer
public void actionPerformed( ActionEvent event3 ) { ImageIcon eeer = new ImageIcon("noura.jpg"); imageLabel.setIcon(eeer); pack(); } } ); JButton button2 = new JButton("GENG_1103"); JButton button3 = new JButton("ESPU_107"); JButton button4 = new JButton("GENG_220"); JButton button5 = new JButton("GENG_310"); JButton button6 = new JButton("MATH_1094"); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); JPanel panel4 = new JPanel(); JPanel panel5 = new JPanel(); JPanel panel6 = new JPanel(); panel1.add(button1); panel2.add(button2); panel3.add(button3); panel4.add(button4); panel5.add(button5); panel6.add(button6); add(panel1); add(panel2); add(panel3); add(panel4); add(panel5); add(panel6); setVisible(true); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.