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

Thread: help to checking the error!!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help to checking the error!!

    the question is You can change the size of a JFrame by using the setSize(int h, int v) method, where h and v give the horizontal and vertical dimensions of the applet’s window in pixels. Write a GUI application that contains two JButtons, labeled “Big” and “Small.” Whenever the user clicks on Small, set the applets dimensions to 200 x 100, and whenever the user clicks on Big, set the dimensions to 300 x 200.
    my program on below:

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

    public class tma2 extends JFrame implements ActionListener
    {

    public tma2()
    {
    JButton startButton = new JButton("BIG");
    JButton startButton = new JButton("SMALL");
    JPanel panel = new JPanel();
    panel.add(startButton);
    }
    public void actionPerformed(ActionEvent e)
    {
    Object src = evt.getSource();
    if (src == button1) {
    setSize(200,100);
    }
    else if (src == button2)
    {
    setSize(300,200);
    }
    }
    }

    please let me know the error of this question!!!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help to checking the error!!

    Quote Originally Posted by vitorloke View Post
    please let me know the error of this question!!!
    What error? Post it in its entirety, and while we want to help don't expect us to know what you are talking about - be precise and concise in your question(s)

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help to checking the error!!

    pls refer the image on below:
    11.jpg

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help to checking the error!!

    can you let me know why i just only change 1 time only!if i want to click each time to change the size then how to modify?below is my code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class tma2 extends JFrame implements ActionListener
    {
    private JButton m_btn1;
    private int index;
     
    public tma2()
    {
    initialize();
    }
     
    private void initialize()
    {
    index = 0;
     
    m_btn1 = new JButton("BIG");
    m_btn1.addActionListener(this);
    setLayout(new BorderLayout());
    add(m_btn1, BorderLayout.NORTH);
    }
     
    public void actionPerformed(ActionEvent e)
    {
    if(index < 0)
    {
    index++;
    }
    else
    {
    setSize(300, 100);
    }
    }
     
    public static void main(String[] p)
    {
    tma2 thisFrame = new tma2();
    thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thisFrame.setSize(200,200);
    thisFrame.setVisible(true);
    }
    }
    Last edited by vitorloke; April 22nd, 2012 at 01:47 AM.

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: help to checking the error!!

    Quote Originally Posted by vitorloke View Post
    can you let me know why i just only change 1 time only!if i want to click each time to change the size then how to modify?
    Hello vitorloke!
    You are not changing only one time the size. You are changing it every time you click the button. The problem is that every time you are setting it to the same size. setSize(300,200);
    To understand that it works every time you click the button, print a message when the else block executes.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help to checking the error!!

    Quote Originally Posted by andreas90 View Post
    Hello vitorloke!
    You are not changing only one time the size. You are changing it every time you click the button. The problem is that every time you are setting it to the same size. setSize(300,200);
    To understand that it works every time you click the button, print a message when the else block executes.
    this is my last modify code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class tma2 extends JFrame implements ActionListener
    {
    private JButton m_btn1;
    private int index;
     
    public tma2()
    {
    initialize();
    }
     
    private void initialize()
    {
    index = 0;
     
    m_btn1 = new JButton("BIG");
    m_btn1.addActionListener(this);
    setLayout(new BorderLayout());
    add(m_btn1, BorderLayout.NORTH);
    }
     
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == 0)
    {
    setSize(200, 100);
    }
    else
    {
    setSize(300, 100);
    }
    }
     
    public static void main(String[] p)
    {
    tma2 thisFrame = new tma2();
    thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thisFrame.setSize(200,200);
    thisFrame.setVisible(true);
    }
    }

  7. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: help to checking the error!!

    Quote Originally Posted by vitorloke View Post
    this is my last modify code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class tma2 extends JFrame implements ActionListener
    {
    private JButton m_btn1;
    private int index;
     
    public tma2()
    {
    initialize();
    }
     
    private void initialize()
    {
    index = 0;
     
    m_btn1 = new JButton("BIG");
    m_btn1.addActionListener(this);
    setLayout(new BorderLayout());
    add(m_btn1, BorderLayout.NORTH);
    }
     
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == 0)
    {
    setSize(200, 100);
    }
    else
    {
    setSize(300, 100);
    }
    }
     
    public static void main(String[] p)
    {
    tma2 thisFrame = new tma2();
    thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thisFrame.setSize(200,200);
    thisFrame.setVisible(true);
    }
    }
    Do you have a problem with it (error, exception, unexpected behaviour)?

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help to checking the error!!

    yup!when i run this program the frame size only chenge 1 time only,if i click again the frame size cannot changer to other!can you let me how to modify the statement!

  9. #9
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: help to checking the error!!

    Quote Originally Posted by vitorloke View Post
    yup!when i run this program the frame size only chenge 1 time only,if i click again the frame size cannot changer to other!can you let me how to modify the statement!
    Firstly, I don't think the last posted code will compile. To be more specific the following line will give you an error.
    if(e.getSource() == 0)
    e.getSource() should equal the button you have created.
    To your question now:
    did you try what i suggested in post #4? I tell you that because it is critical to understand why it isn't changing (it actually).
    You can use the index variable to the setSize() method and increment or decrement it every time you click the button to get what you want.

  10. #10
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help to checking the error!!

    how to use the index variable to the setSize() method to change the size!since i just want to change by 2 difference type!so how to create the statement?

  11. #11
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: help to checking the error!!

    Quote Originally Posted by vitorloke View Post
    how to use the index variable to the setSize() method to change the size!since i just want to change by 2 difference type!so how to create the statement?
    What do you mean by "i just want to change 2 difference type"?
    Can you post your current code and elaborate your requirements?

  12. #12
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help to checking the error!!

    i mean just want to set (200,100) and (300,200)

  13. #13
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: help to checking the error!!

    Quote Originally Posted by vitorloke View Post
    i mean just want to set (200,100) and (300,200)
    If I'm getting it right you want to set the size (200,100) the first time the button is clicked, (300,200) the second time so on.
    So if you want to do this with one button only you can use the index value in the actionPerformed() method you have with the following logic;

    if (index is even)*
    setSize(200, 100);
    else
    setSize (300, 100);
    increment index by one

    *You can either test if the index is odd instead of even.
    Hint: an int x is even when x % 2 == 0.

Similar Threads

  1. Checking Blurbs
    By 9erNumber16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2011, 07:29 PM
  2. Need help with checking passwords
    By kb1213 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 12th, 2011, 05:32 PM
  3. Error while creating Gym member database through Java programming
    By parvez07 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 26th, 2009, 02:17 AM
  4. Checking in.
    By Johannes in forum Member Introductions
    Replies: 7
    Last Post: August 13th, 2009, 06:11 AM
  5. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM