Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How can I incorporate images when I click a button

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    My Mood
    Scared
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I incorporate images when I click a button

    // Objective : When the user clicks the help button an image will pop up

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.*;import java.io.*;
    import javax.swing.*;import javax.imageio.*;
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class GridBags extends JFrame{
     
     
    	JPanel thePanel;
     
    	public GridBags(){
    		super();
     
     
    		//general settings
    		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		this.setSize(1000, 500);
     		this.setResizable(false);
     		this.setTitle("Biology Review Program");
     		this.setVisible(true);
     		this.setLayout(new GridBagLayout());
     
    		initComponents();
     
    	}
     
    	public void initComponents(){
     
     
     
     
     
    		//This variable is changed to allow us to place buttons
    		//and other components flexibly on the page.
    		GridBagConstraints c = new GridBagConstraints();
    		c.fill = GridBagConstraints.NONE;
     
    		JButton lesson = new JButton("Lessons");
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		c.gridx = 1;
    		c.gridy = 500;
    		this.add(lesson, c);
    		lesson.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e){
    				System.out.println("You clicked for Lesson?");
     
                }
            });  
     
    		//create the myCreature object
    		thePanel = new JPanel();
    		//set the size and background of the panel
    		thePanel.setPreferredSize(new Dimension(700, 500));
    		thePanel.setBackground(new java.awt.Color(0, 0, 0));
    		//add the panel to the pane
    		c.gridwidth = 2;  //six "buttons" wide
    		c.gridheight = 1;
    		c.gridx = 3;		//left most
    		c.gridy = 1;		//top most
    		this.add(thePanel, c);
     
    		JButton quiz = new JButton("Quiz");
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		c.gridx = 2;
    		c.gridy = 500;
    		this.add(quiz, c);
    		quiz.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e){
    				System.out.println("You clicked for Quiz?");
     
                }
            });  
     
     
    		JButton help = new JButton("Help");
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		c.gridx = 3;
    		c.gridy = 500;
    		this.add(help, c);
    		help.addActionListener(new ActionListener() {
      [COLOR="#FF0000"]	Image img=null;
        		try {img=ImageIO.read(new File("help.jpg"));}
      			catch(IOException e)
          	    {System.out.println("ok");System.exit(0);}
        		g.drawImage(img,200,0,this);[/COLOR]
     
     
                     public void actionPerformed(ActionEvent e){
    				System.out.println("You clicked for Help?");
     
                }
            });  
     
     
    	}
     
     
     
    	public static void main(String[] args){
     
    		GridBags example = new GridBags();
    		example.pack();
    		example.show();
    	}
     
     
     
     
     
    }
    Last edited by pbrockway2; May 18th, 2013 at 07:13 PM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How can I incorporate images when I click a button

    I've added code tags to your post. The idea is that you put [code] at the start of a section of code and [/code] at the end: that way the code will be formatted when it appears here on a web page.

    Does your code compile? If not and you can't understand the compiler's messages, post them.

Similar Threads

  1. How to close a frame on a button click ??
    By rk0887 in forum AWT / Java Swing
    Replies: 1
    Last Post: August 22nd, 2012, 08:58 AM
  2. add JTextField with a click of button
    By A4Andy in forum AWT / Java Swing
    Replies: 1
    Last Post: August 31st, 2011, 07:34 AM
  3. Lock up code to the click of a button!!!
    By Allen Walker in forum Java Theory & Questions
    Replies: 2
    Last Post: July 1st, 2011, 09:12 AM
  4. close JDialog on button click
    By Christophe in forum AWT / Java Swing
    Replies: 4
    Last Post: April 4th, 2010, 11:04 PM