Results 1 to 4 of 4

Thread: GUI image program java

  1. #1
    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);
    }
    }

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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);
    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    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.

  4. #4
    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.

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,716,011.47921 seconds with 17 queries