Results 1 to 20 of 35
Thread: help to checking the error!!
- 04-21-2012, 06:25 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 22
- Rep Power
- 0
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!!!
- 04-21-2012, 06:29 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
Re: help to checking the error!!
You reassign startButton in the constructor.
You are comparing src with a variable that has not been declared
- 04-21-2012, 07:14 PM #3
Re: help to checking the error!!
A JFrame isn't an Applet. Learn the difference.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-22-2012, 03:26 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 22
- Rep Power
- 0
Re: help to checking the error!!
so how to modify?
-
Re: help to checking the error!!
As as_Marshy suggests, don't declare your JButton variables inside of the constructor, since those variables are only visible within the constructor. For instance:
Java Code:public class MyFoo { private MyBar myBar1; // is visible throughout the class public MyFoo() { myBar1 = new myBar(); // again, this is visible throughout the class MyBar myBar2 = new MyBar(); // this is visible only inside of this constructor } }
- 04-22-2012, 03:50 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 22
- Rep Power
- 0
-
Re: help to checking the error!!
Yes, your code is having the same situation since your JButton variables are declared in the constructor and thus are only visible within the constructor, a situation you don't want. But that code snippet won't compile since you appear to be declaring the same variable, startButton twice.
Last edited by Fubarable; 04-22-2012 at 04:34 AM.
- 04-22-2012, 04:44 AM #8
Member
- Join Date
- Apr 2012
- Posts
- 22
- Rep Power
- 0
Re: help to checking the error!!
can you show how to comply the program?
-
Re: help to checking the error!!
It's hard to know where exactly you're stuck right now. Based on our advice so far, what have you tried, and how hasn't it worked?
- 04-22-2012, 05:24 AM #10
Member
- Join Date
- Apr 2012
- Posts
- 22
- Rep Power
- 0
-
Re: help to checking the error!!
- 04-22-2012, 05:30 AM #12
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
Re: help to checking the error!!
What is your exact problem. Programming is not an opinion. Like Poetry or language. In order to get help describe your errors. If not errors describe your output compared with your expected output. Don't assume anything. Assumption is like garlic to a programmers vampire certain problems lol
.
- 04-22-2012, 06:03 AM #13
Member
- Join Date
- Apr 2012
- Posts
- 22
- Rep Power
- 0
- 04-22-2012, 06:07 AM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
Re: help to checking the error!!
You posted this code hours ago and were told its problems. Do you mean to tell me you have not changed anything yet expect different results?
- 04-22-2012, 06:12 AM #15
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
Re: help to checking the error!!
12 hours ago!!! Have you changed any code based on advice in twelve hours? Einstein said insanity is doing the same thing over and over and expecting new results
.
-
Re: help to checking the error!!
There are many many of mistakes in that code, some of which have been pointed out, but others as well, including
- adding a semicolon `;` after your if statement -- this short-circuits the if statement. Check the tutorials to see how they are written and try to emulate this.
- same for your else statement -- get rid of the trailing semicolon.
- Re-declaring src as a String. It isn't a String but rather a JButton.
- Trying to compare src to a non-existing variable. button1 doesn't exist in the code above.
- You still declare your JButton variables in the constructor despite our recommending against this.
- You still are using the same variable name for both JButtons despite our recommending against this.
- Code indentation that appears randomly produced making your code very difficult to read, understand and debug. Indent 3 spaces for each block and be consistent and precise with your indentation.
I hate to suggest this, but I think that you should start over, that you should compile your code often and correct any and all compilation errors before adding any new code.
- 04-22-2012, 06:56 AM #17
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
Re: help to checking the error!!
Fubarable is being way too nice so I'm going to tell you out straight you need to learn the basics of java I.e how to write a class. I don't mean to be cruel but you are trying to sprint before you can crawl. And don't be discouraged we all started with the basics.,
Last edited by al_Marshy_1981; 04-22-2012 at 07:08 AM.
- 04-22-2012, 07:14 AM #18
Member
- Join Date
- Apr 2012
- Posts
- 22
- Rep Power
- 0
Re: help to checking the error!!
can do like this?
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class tma2 extends JFrame { private JButton myFirstButton; private JButton mySecondButton; // Constructor for a new frame public tma2() { super("4a"); myFirstButton = new JButton("BIG"); // myFirstButton.setFont(new Font( "Arial", Font.BOLD, 18)); // myFirstButton.setBackground(Color.red); mySecondButton = new JButton("Small"); // mySecondButton.setFont(new Font( "Arial", Font.BOLD, 18)); // mySecondButton.setBackground(Color.green); Container c = getContentPane(); FlowLayout fl = new FlowLayout(FlowLayout.LEFT); c.setLayout(fl); c.add (myFirstButton); c.add (mySecondButton); ButtonHandler handler = new ButtonHandler(); //creation of a new Object myFirstButton.addActionListener(handler); // Attach/register handler to myFirstButton mySecondButton.addActionListener(handler); //Attach/register handler to mySecondButton setSize(200, 100); show(); } public static void main(String [] args) { // Make frame tma2 f = new tma2(); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { // This closes the window and terminates the // Java Virtual Machine in the event that the // Frame is closed by clicking on X. System.out.println("Exit via windowClosing."); System.exit(0); } } ); } // end of main // inner class for button event handling private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == myFirstButton) { setSize(300, 200);//System.out.println("Left Button has been pressed."); } if (e.getSource() == mySecondButton) { setSize(200, 100);// System.out.println("Right Button has been pressed."); } } } // end of inner class } // end of outer class
- 04-22-2012, 07:19 AM #19
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
Re: help to checking the error!!
Well done. If that is your code and you understand it. I'm not sure about your language so can't be sure what you mean by hide.
-
Re: help to checking the error!!
Yes you can, and that code is *much* better!
if i want to change when i click the button hide the big and small button ,how to do?
Similar Threads
-
Checking if a Bit is Set.
By Hypnos in forum New To JavaReplies: 11Last Post: 01-18-2012, 10:27 PM -
Checking DTD syntax...
By xlomo in forum XMLReplies: 0Last Post: 10-01-2011, 03:14 AM -
Error Checking not working correctly
By RickAintree in forum New To JavaReplies: 1Last Post: 12-15-2010, 02:54 PM -
how to do checking value
By madhuks in forum Java ServletReplies: 3Last Post: 07-21-2010, 10:35 AM -
Checking if a file name already has .txt in it
By AJArmstron@aol.com in forum New To JavaReplies: 2Last Post: 04-17-2010, 01:45 PM
Bookmarks