Hello. I am busy writing a java applet, but I have encountered the following problems:
1. When I click on the Blue button, it is suppose to change the background colour to blue. When I click on the Black button, it is suppose to change the background colour to black.Butwhen I click on them, these two actions are not performed. My buttons are working, as I am able to change the caption of the buttons when I click on the buttons, but it does not want to change the background color.
2. When my applet loads, it only loads the west button.When I hover over the position where the second button is supposed to be, it suddenly appears. I have attached my .java and .class files with a screenshot.
I will REALY appreciate your help if you can help me asap as this is quite urgent. Thank you.
Please this is quite urgent as I am writing a semester test on this work on Monday.
Unfortunately your urgency isn't our problem.
However I have just had time to have a quick look at this:
The solution to part of the problem has already been given to you.
The reason you are seeing drawing issues is that you have overridden the paint() method and not called super.paint() and are not actually doing any painting yourself.
If you remove the paint() method (or add super.paint() to it) and change the setBackground() call as explained by xlarsx it should all work.
Thank you guys very very much.It is working now.Just one more question: I would like to add a JPanel inside the borders of those two buttons with a button on the JPanel,but I just cant seem to get it right.I've tried various different ways to do it but every time,my JPanel does not show up,or my whole applet just crashes.Your help will be greatly apreciated.
I would like to add a JPanel inside the borders of those two buttons with a button on the JPanel,but I just cant seem to get it right.
I was going to have a shot at this, but I couldn't make out what you were talking about.
Judge a man by his questions, rather than his answers...
Voltaire
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
I was going to have a shot at this, but I couldn't make out what you were talking about.
Sorry for not being clear. I have added a picture as attachment as how I want my applet to look. In the middle you will see a white retancle. That is the JPanel. On the JPanel I would like to add a circle and a button.
My problem is, I can't seem to figure out how to add the JPanel in the first place, and secondly how to add the circle and button. In every way I tried so far, my panel either take up the whole applet space or it doesn't appear at all or my button take up the whole space and take the left and right buttons away. I have set the appropriate sizes of everyhing, but I still can't seem to get it to work. I guess I'm going all wrong about this.
Hope you understand better now and please have a look at the pic. Thanx
OK, well if you're using a BorderLayout for the main panel, and you've set the preferred width of the two panels (buttons?) 'black' and 'blue' on the EAST and WEST areas, you need to set the central panel in the BorderLayout.CENTER area. This will automatically take up all space not occupied by the EAST and WEST components.
You can position your circle in the centre panel using another layout manager (for example, a GridBagLayout with contraints anchor set to GridBagLayout.CENTER and constraints fill set to GridBagLayout.NONE). You haven't shown where you want the other button to go, so I can't really help there.
There are many ways of doing these things, and you can use as many panels and layouts as you like to get things positioned the way you want.
The truth is, when all is said and done, one does not teach a subject, one teaches a student how to learn it. Teaching may look like administering a dose, but even a dose must be worked on by the body if it is to cure. Each individual must cure his or her own ignorance...
J. Barzun
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.