Hi Guys
Im having a problem.
Im very new to Java and am using blueJ.
I would like to displya a picture on a label when I click a button. At the moment all I can get it to do is to print a message when a button is clicked.
Ive tried numerous things and am getting frustrated. If anyone has any solutions/ pointers i would be very grateful. My code is below:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class clsScreen implements ActionListener //actionlistener is needed to make buttons work
{
// instance variables - replace the example below with your own
private static final String PATH = "I:/More Quiz Pictures/"; // Path to where pictures are placed
private JLabel screenLabel; // creates a label so i can put images onto it
private JLabel screenLabel1;
public clsScreen()
{
}
public void makescreen()
{
JFrame frame = new JFrame("Television"); //Creates a new frame called television
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
JLabel screenLabel = new JLabel(new ImageIcon(PATH+"TVOFF.JPG")); // Sets the screen label to a blank screen which will
//simulate a blank screen
contentPane.add(screenLabel, BorderLayout.CENTER);// CENTERS THE lABEL TO THE MIDDLE OF THE FRAME
JPanel southPanel = new JPanel();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
// ***********************************************
southPanel.add(buttonPanel, BorderLayout.SOUTH);
contentPane.add(southPanel, BorderLayout.SOUTH);
//***Start of button creation*************
JButton channelButton1 = new JButton("1"); //Create the button
channelButton1.addActionListener(this); // adds a actionlistener so the button responds to a click
buttonPanel.add(channelButton1); // add the button to the button panel
//****Creating button 2******
JButton channelButton2 = new JButton("2");
channelButton2.addActionListener(this);
buttonPanel.add(channelButton2);
//****Creating button 3******
JButton channelButton3 = new JButton("3");
channelButton3.addActionListener(this);
buttonPanel.add(channelButton3);
//****Creating Button 4******
JButton channelButton4 = new JButton("4");
channelButton4.addActionListener(this);
buttonPanel.add(channelButton4);
//****Creating Guide Button******
JButton channelButtonGuide = new JButton("T.V Guide");
channelButtonGuide.addActionListener(this);
buttonPanel.add(channelButtonGuide);
//*****Creating the standby Button******
JButton channelButtonStandby = new JButton("Standby");
channelButtonStandby.addActionListener(this);
buttonPanel.add(channelButtonStandby);
//*****End of button creation***//
frame.pack(); //will pack the border down to fit the content
frame.setVisible(true); // shows the frame when method is called
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand(); //Command = button that is clicked
if(command.equals("1")) //if 1 button is clicked then display 1.
{
System.out.println("1");
screenLabel1.setIcon(new ImageIcon(PATH + "Pic01.JPG"));
}
else if (command.equals("2"))
{
System.out.println("2");
}
else if(command.equals("3"))
{
System.out.println("3");
}
else if(command.equals("4"))
{
System.out.println("4");
}
else if(command.equals("T.V Guide"))
{
System.out.println("Guide");
}
else if(command.equals("Standby"))
{
System.out.println("Standby");
}
}
}//End