Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35
Like Tree1Likes

Thread: help to checking the error!!

  1. #21
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    Quote Originally Posted by Fubarable View Post
    Yes you can, and that code is *much* better!



    Consider using only one JButton, and in your single JButton's ActionListener code, change the text of the JButton via setText. You'll need to check the text too to see what state the button is in (whether the text is "Big" or "Small"), and for this call getText() on the JButton. Note that when using an if block to check if two Strings are equal, don't compare the Strings with the == operator, but instead you'll want to use the equals(...) or better for you, the equalsIgnoreCase(...) method for Strings.
    can you explain more detail?

  2. #22
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    can you know how to modify the if statement to change the operation to when the JButton is labeled “Big,” clicking it should give the applet its big dimensions.

  3. #23
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    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:

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

  4. #24
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    my last modify code;
    Java 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);
    }
    }

  5. #25
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    Please post only formatted code. Your code is all left-justified and hard to read.

    Some suggestions:
    • First create two constant Strings, BIG = "Big", and SMALL = "Small". These will be declared public static final and will be at the top of your class. Doing this will ensure that all big strings will be spelled exactly the same, and will be in fact the same String object, and same for all small Strings.
    • When you create your JButton, pass in the BIG constant. This will set the JButton's text and its actionCommand to the BIG String constant.
    • In your actionPerformed(...) method, get the actionCommand String from your ActionEvent parameter, e, by calling e.getActionCommand().
    • Use an if block to test if the String returned equals the BIG String constant or the SMALL String constant.
    • If it equals BIG, then change your button's text to SMALL using its setText(...) method, and also change its actionCommand to SMALL using the setActionCommand(...) method. Change your gui's size as well.
    • Do the converse if the String equals SMALL.

  6. #26
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    Quote Originally Posted by Fubarable View Post
    Please post only formatted code. Your code is all left-justified and hard to read.

    Some suggestions:
    • First create two constant Strings, BIG = "Big", and SMALL = "Small". These will be declared public static final and will be at the top of your class. Doing this will ensure that all big strings will be spelled exactly the same, and will be in fact the same String object, and same for all small Strings.
    • When you create your JButton, pass in the BIG constant. This will set the JButton's text and its actionCommand to the BIG String constant.
    • In your actionPerformed(...) method, get the actionCommand String from your ActionEvent parameter, e, by calling e.getActionCommand().
    • Use an if block to test if the String returned equals the BIG String constant or the SMALL String constant.
    • If it equals BIG, then change your button's text to SMALL using its setText(...) method, and also change its actionCommand to SMALL using the setActionCommand(...) method. Change your gui's size as well.
    • Do the converse if the String equals SMALL.
    but the question asking only click single button to change the size.so i just confuser how to using if statement to control when i click the button then the result is change the size....can you let me know the statement basic on the top code !

  7. #27
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    Why not first try to code it yourself using my suggestions, suggestions which basically tell you what needs to be done.

  8. #28
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    basic on your suggestions is not a single button

  9. #29
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    Quote Originally Posted by vitorloke View Post
    basic on your suggestions is not a single button
    Yes it is -- please re-read it as it is based on your most recent code post.

  10. #30
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    can get me more tip and detail!because i not understanding !!!

  11. #31
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    my just get the idea for this question on below:

    Java Code:
    {
    ....
    setSize(BIG);
    fsize = TRUE;
    .....
    }
    
    on button click action handler
    {
    if(fsize = TRUE)
    {
    setsize(SMALL);
    fsize = FALSE;
    }
    else
    {
    setsize(BIG);
    fsize = TRUE;
    }
    }

    can you let me know how to Create a boolean variable, fsize to store the state/status of the current frame size. Assume that "Big" frame is TRUE, and "Small" frame size is FALSE.

  12. #32
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    You don't need boolean variables here, just check the actionCommand String that the ActionEvent returns. For instance, say you have two String constants in your program:

    Java Code:
       public static final String FOO = "Foo";
       public static final String BAR = "Bar";
    And you initialize your JButton with one of these Strings, then your JButton's ActionListener would check the button's actionCommand String and would then change this String and the button's text as needed. In semi-pseudocode

    Java Code:
          button.addActionListener(new ActionListener() {
             
             @Override
             public void actionPerformed(ActionEvent evt) {
                JButton btn = (JButton)evt.getSource();
                String actionCommand = evt.getActionCommand();
                
                check if the actionCommand String equals FOO. If so,
                   Set the button's text to BAR
                   Set the button's actionCommand to BAR
                   
                   do something (change size of GUI, or whatever...)
                else if actionCommand equals BAR
                   Set the button's text to FOO
                   Set the button's actionCommand to FOO
                   
                   do something (change size of GUI, or whatever...)
                }
             }
          });

  13. #33
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    how to do set the do something ???

  14. #34
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: help to checking the error!!

    Quote Originally Posted by vitorloke View Post
    how to do set the do something ???
    Don't you want to try to do something on your own here? Come on -- let's see your attempt.

  15. #35
    vitorloke is offline Member
    Join Date
    Apr 2012
    Posts
    22
    Rep Power
    0

    Default Re: help to checking the error!!

    how to do that???i not understand !

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Checking if a Bit is Set.
    By Hypnos in forum New To Java
    Replies: 11
    Last Post: 01-18-2012, 10:27 PM
  2. Checking DTD syntax...
    By xlomo in forum XML
    Replies: 0
    Last Post: 10-01-2011, 03:14 AM
  3. Error Checking not working correctly
    By RickAintree in forum New To Java
    Replies: 1
    Last Post: 12-15-2010, 02:54 PM
  4. how to do checking value
    By madhuks in forum Java Servlet
    Replies: 3
    Last Post: 07-21-2010, 10:35 AM
  5. Checking if a file name already has .txt in it
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 01:45 PM

Posting Permissions

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