Results 1 to 3 of 3

Thread: GUI's

  1. #1
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default GUI's

    Hello,

    The program I'm writing is using a GUI to display three buttons with three different artists name on them. When you press a button an image appears on the button by the name and a short audio clip comes up. So far Ive created the three buttons and I think I need an if statement to get the button to appear. If you could help that would be great.

    Heres the code:

    Java Code:
    import javax.swing.*; // Required library
    import java.awt.*; // Required library
    import java.awt.event.*; // Required library
    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
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    hey, i updated the code so now if i press the first button the image pops up. But how do i do that for the other two buttons and can someone help on how to add the audio when i click on the button?
    Thanks

    Java Code:
    import javax.swing.*; // Required library
    import java.awt.*; // Required library
    import java.awt.event.*; // Required library
    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
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You seem to know that you change the JButton's image by setting its icon via setIcon. You can also get the icon via getIcon. If you want to toggle a button between having a picture vs being blank, I can picture using 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.

    Also, you will want to give your variables more descriptive names than say myButton, my1Button and my2Button.

Similar Threads

  1. GUI's and inputting doubles or ints
    By lopder1 in forum New To Java
    Replies: 19
    Last Post: 11-05-2009, 08:50 PM
  2. GUI's
    By ngc0202 in forum New To Java
    Replies: 7
    Last Post: 07-26-2009, 03:38 AM
  3. Swing themes & custom GUI's
    By Mr.Beans in forum AWT / Swing
    Replies: 3
    Last Post: 04-28-2009, 02:10 AM
  4. GUI's
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-22-2008, 09:19 PM
  5. Creating GUI's for simulators.
    By Modifier in forum New To Java
    Replies: 3
    Last Post: 11-20-2008, 12:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •