Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 12-11-2009
Member
 
Join Date: Oct 2009
Posts: 61
GUI image program java

Hi
I am trying to write a GUI program to display three buttons with three different artists name on them. If one presses a button an image should appear on the button by the name and short audio clip comes up. I am posting my code, plz check it out. If you would help that would be great.
Code:
public class myGUI extends JFrame {
  private JButton myButton = new JButton("Vivaldi"); // Create a new button
  private JLabel labelWithText = new JLabel();
  private JButton my1Button = new JButton("Brubeck"); // Create a new button
  private JLabel label1WithText = new JLabel();
  private JButton my2Button = new JButton("dsf"); // Create a new button
  private JLabel label2WithText = new JLabel();
private java.net.URL speakerImage= getClass().getResource("speaker.jpg");
private int status = 1; // 1 = image on, 0 = image off
// Constructor
public myGUI() {
setLayout(new GridLayout(3, 0, 0, 0)); // Define layout as a grid
add(myButton); // Make button appear in window
add(labelWithText);
add(my1Button); // Make button appear in window
add(label1WithText);
add(my2Button); // Make button appear in window
add(label2WithText);
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
// Main method
public static void main(String[] args) {
myGUI myFrame = new HW9_11(); // Declare and create an instance of MyGUI
myFrame.setVisible(true); // Make the window visible on the screen
    Audio player = new Audio();
    player.play("hello.wav");
myFrame.setTitle("AA");
myFrame.setSize(300, 300);
}
}
Reply With Quote
  #2  
Old 12-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: GUI image program java

Hi
I have tried this program for you. Please check if it helps you. And if any problem in this program do post back.
Code:
public class HW9_11 extends JFrame {
  private JButton myButton = new JButton("Vivaldi"); // Create a new button
  private JLabel labelWithText = new JLabel();
  private JButton my1Button = new JButton("Brubeck"); // Create a new button
  private JLabel label1WithText = new JLabel();
  private JButton my2Button = new JButton("dsf"); // Create a new button
  private JLabel label2WithText = new JLabel();
private java.net.URL speakerImage= getClass().getResource("speaker.jpg");
private int status = 1; // 1 = image on, 0 = image off 
// Constructor
public HW9_11() {
setLayout(new GridLayout(3, 0, 0, 0)); // Define layout as a grid
add(myButton); // Make button appear in window
add(labelWithText);
add(my1Button); // Make button appear in window
add(label1WithText);
add(my2Button); // Make button appear in window
add(label2WithText);

myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
  if (status == 1) {
    myButton.setIcon(null);
    //button.setText("Image removed.");
    status = 0;
}
  else {
    myButton.setIcon(new ImageIcon(speakerImage));
    //button.setText(null);
    status = 1;
}
}
});
}
// Main method
public static void main(String[] args) {
HW9_11 myFrame = new HW9_11(); // Declare and create an instance of MyGUI
myFrame.setVisible(true); // Make the window visible on the screen
    Audio player = new Audio();
    player.play("hello.wav");
myFrame.setTitle("AA");
myFrame.setSize(300, 300);
}
}
Reply With Quote
  #3  
Old 12-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: GUI image program java

Hi
It seems that you have changed the image via setIcon. You can also get the icon via getIcon. If you want to toggle a button between having a picture and a blank, You can do this by using picture, which is its default icon property to see if a button has an image or not. So if the button has been clicked, and getIcon == null, then set the icon. If it's not null, then set it to null. This way you could possibly get rid of your "status" variable.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 12-11-2009
Member
 
Join Date: Oct 2009
Posts: 61
Re: GUI image program java

Hi
Thanks for replying with a code. But the code does not have an audio playing for the button. Never mind I will try it and if its working then I will post the code back. Thanks again for the help.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "GUI image program java"
Thread Thread Starter Forum Replies Last Post
'Program name.exe Bad Image' Error NewComer Windows Software 5 20-05-2010 09:11 AM
Link List Example in Java Sample program in Java trickson Software Development 2 04-08-2009 08:23 PM
Software or program to make thumbnails for image Bertus Windows Software 3 01-05-2009 06:41 PM
click on a GIF image to start a program Mr.Macroney Customize Desktop 3 21-01-2009 08:39 PM


All times are GMT +5.5. The time now is 11:47 PM.