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 3 of 3

Thread: Button tryout

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Location
    Almere, Nederlands, Earth, Sol, Milkyway
    Posts
    4
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Button tryout

    Hi, (first post)

    i am trying to learn and understand java and have run into a problem.

    i am trying to get a new 3rd button working "image"
    the button image is shut open a new windows with a image in it i weill post the main class "ButtonX" aswel as the window frames "BaseFrame1" and "ImageFrame1"

    if you can fix it please tell my how so i can learn from it.

    (ButtonX)

    **************************************************

    import java.awt.Button;
    import java.awt.Event;
    import java.awt.Frame;

    public class ButtonX extends java.applet.Applet {
    Frame window;
    Button open, image, close;

    public void init() {
    open = new Button("Open Window");
    add(open);
    image = new Button("image");
    add(image);
    close = new Button("Close Window");
    add(close);


    window = new BaseFrame1("A Pop Up Window");
    window.resize(250,200);
    window = new ImageFrame1("A image");
    window.resize(350,300);
    }

    public boolean action(Event evt, Object arg) {
    if (evt.target instanceof Button) {
    String label = (String)arg;
    if (label.equals("Open Window")) {
    if (!window.isShowing())
    window.show();
    } else {
    if (window.isShowing())
    window.hide();
    }
    return true;
    } else
    return false;
    if (evt.target instanceof Button) {
    String label = (String)arg;
    if (label.equals("image")) {
    if (!window.isShowing())
    window.show();
    } else {
    if (window.isShowing())
    window.hide();
    }
    return true;
    } else
    return false;
    }
    }

    ************************************************** ******

    BaseFrame1

    ************************************************** ******

    import java.awt.*;

    class BaseFrame1 extends Frame {
    String message = "This is a Window";
    Label l;

    BaseFrame1(String title) {
    super(title);
    setLayout(new BorderLayout());

    l = new Label(message, Label.CENTER);
    l.setFont(new Font("Helvetica", Font.PLAIN, 12));
    add("Center", l);
    }

    public Insets getInsets() {
    return new Insets(20,0,25,0);
    }
    }

    ************************************************** ****************

    ImageFrame1

    ************************************************** ****************


    import java.awt.*;
    import javax.swing.*;

    public class ImageFrame1 extends JPanel{
    Image image;
    public ImageFrame1(){
    super();

    image = Toolkit.getDefaultToolkit().getImage("ncis.jpeg");
    }


    public void paintComponent(Graphics g){


    g.drawImage(image,50,10,200,300, this);
    }

    public static void main(String arg[]){
    JFrame frame = new JFrame("ShowImage");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(600,400);

    ShowImage panel = new ShowImage();
    frame.setContentPane(panel);
    frame.setVisible(true);
    }
    }

    ************************************************** *****************


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Button tryout

    I see one problem, in your ButtonX class, in the action function:
    	public boolean action(Event evt, Object arg) {
    		if (evt.target instanceof Button) {
    			String label = (String) arg;
    			if (label.equals("Open Window")) {
    				if (!window.isShowing())
    					window.show();
    			} else {
    				if (window.isShowing())
    					window.hide();
    			}
    			return true;
    		} else
    			return false;
    		if (evt.target instanceof Button) {
    			String label = (String) arg;
    			if (label.equals("image")) {
    				if (!window.isShowing())
    					window.show();
    			} else {
    				if (window.isShowing())
    					window.hide();
    			}
    			return true;
    		} else
    			return false;
    	}
    especially here:
    		} else
    			return false;
    		if (evt.target instanceof Button) {
    The if-statement is dead code. It will never get executed, since right before that the function will return false. And thus quit the function.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Location
    Almere, Nederlands, Earth, Sol, Milkyway
    Posts
    4
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Button tryout

    How do i fix it?

Similar Threads

  1. Can't use button.addActionListener(this); ?
    By xdega in forum AWT / Java Swing
    Replies: 2
    Last Post: April 23rd, 2012, 08:44 AM
  2. Help with button actionlistener
    By umerahmad in forum Java Theory & Questions
    Replies: 9
    Last Post: August 25th, 2011, 07:21 AM
  3. disable a button ?
    By dime111 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 28th, 2011, 02:58 PM
  4. Incrementing button by 1
    By joshft91 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 1st, 2011, 10:45 AM
  5. Trying to add a close button
    By coyboss in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2011, 03:28 PM