javadummy2011
asked on
Button Program
My program does not run correctly and I am stuck! I need the buttons to align left if click on the left button (not the whole window left like it displays), then the background color doesn't cover the entire background?? Please help. I attached the requirements and a copy of my program.
Thanks!!!
Buttons.java
Program-8.pdf
Thanks!!!
Buttons.java
Program-8.pdf
ASKER
As stated in my question, I need my background color to be the entire background.
Isn't the button panel the entire window?
There ahs to be an easier way to shift the buttons left than what your suggesting.....
Isn't the button panel the entire window?
There ahs to be an easier way to shift the buttons left than what your suggesting.....
On line 41 you typed
getContentPane().add(butto nPanel)
and so added the buttonPanel to the window.
getContentPane().setBackgr ound(yourC olor) should work better.
Color color = getContentPane().getBackgr ound() should help too.
My suggestion for shifting the buttons isn't that complicated. And why should there be a better way? There isn't much use for a real program to do that very often.
getContentPane().add(butto
and so added the buttonPanel to the window.
getContentPane().setBackgr
Color color = getContentPane().getBackgr
My suggestion for shifting the buttons isn't that complicated. And why should there be a better way? There isn't much use for a real program to do that very often.
YOu code does not compile because variable btn is not declare and not assifgned value in class eeventHandle
I don't have Java on this computer. What is the flow layout stuff doing when you align it? You could probably shift the buttonPanel instead of doing all the buttons one at a time too.
In order to change background of your original frame in class EventHandle
- you need to pass the reference to your class Buttons to class eventHandle
- you need to pass the reference to your class Buttons to class eventHandle
I suggest that you first modify the points above to make the program compilable and at least see the buttons.
Post your code, Then we'll be thinking how to shift them.
Post your code, Then we'll be thinking how to shift them.
ASKER
Im working on the color right now only. The buttons are viewable and in the right location, but they are not executing properly when clicked. Posting code in a few minutes....
OK, please, post code.
ASKER
post actual code or the file?
ASKER
I mean copy and paste code into comment or attach file?
It does not matter.
You know I also suggest to change - don't make this eventhandlee as seoparate class - this makes
it more difficult to do and there is no such requirement.
If your tecahers prefer, let;'s make anonymoous classes handing events - it swould be easier.
Then don' forget to call constructor of super class as the first operator of your constructor - this is also important
and not only to communicattee the header.
By the way, this assignemnet is more difficult than previous studd - I esecially don't
like moving thses buttons on GUI - these things when you do it not in constrctor are
kind of painful.
OK, so I suggest to move these event handlers and add constructor,
than either paste the code or attach it
You know I also suggest to change - don't make this eventhandlee as seoparate class - this makes
it more difficult to do and there is no such requirement.
If your tecahers prefer, let;'s make anonymoous classes handing events - it swould be easier.
Then don' forget to call constructor of super class as the first operator of your constructor - this is also important
and not only to communicattee the header.
By the way, this assignemnet is more difficult than previous studd - I esecially don't
like moving thses buttons on GUI - these things when you do it not in constrctor are
kind of painful.
OK, so I suggest to move these event handlers and add constructor,
than either paste the code or attach it
Attaching the file is fine.
You know better paste, because some of your attached files are strange they probable have some extra symbols or someting - I have wrong coloring agfer
I open them in the IDE
I open them in the IDE
ASKER
Ok, Don't be mad, but I started moving things one at a time to ensure I didn't jack up my code. Somewhere in between me cutting a piece out to move it and having to run upstairs to my kid crying, I got a little lost what I was doing..... Posting what I have for you to comment..... I promise I wont touch the code until I hear back from you..... As we both know I will mess it up further if I do mess with it.....
Buttons.java
Buttons.java
ASKER
/************************* ********** ********** ********** ********** ********
* Buttons.java
*
*
*
************************** ********** ********** ********** ********** *********/
import java.awt.*; // for FlowLayout
import javax.swing.*; // for JFrame, JLabel and JTextField
import java.awt.event.*; // for ActionListener and ActionEvent
public class Buttons extends JFrame // extends JFrame superclass
{
private JButton resetButton; // reset button
private JButton leftButton; // left button
private JButton colorButton; // color button
private JPanel buttonPanel; // windows panel
FlowLayout flt;
Color color;
Point point;
public Buttons()
{
super("Buttons Window");
{
setLayout(new FlowLayout (FlowLayout.CENTER)); // locates the window in center
setBounds(20, 120, 350, 250); // size of command window
setDefaultCloseOperation(E XIT_ON_CLO SE); // exit to main command window
createContents();
setVisible(true); // able to view, yes
buttonPanel = new JPanel(flt); // initialize the buttonPanel Within FlowLayout
leftButton = new JButton("Left"); // left button
colorButton = new JButton("Blue"); // color button
resetButton = new JButton("Reset"); // reset button
eventHandle = new EventHandle(evt);
point=new Point((Toolkit.getDefaultT oolkit().g etScreenSi ze().width - getWidth())/2, (Toolkit.getDefaultToolkit ().getScre enSize().h eight-getH eight())/2 );
buttonPanel.add(leftButton ); // add left button
buttonPanel.add(colorButto n); // add color button
buttonPanel.add(resetButto n); // add color button
leftButton.addActionListen er(evt); // add actionlisteners to all buttons
colorButton.addActionListe ner(evt);
resetButton.addActionListe ner(evt);
class buttons implements ActionListener // event handler that implements ActionListener interface
{
public void actionPerformed(ActionEven t e)
{
{
if (e.getSource() == leftButton) // if "left" button
{
setLayout (new FlowLayout(FlowLayout.LEFT )); // align buttons to left side
}
else if (e.getSource() == colorButton) // if "color" button
{
setBackground(color.BLUE); // sets the backgorund to Blue
}
else
{
setLayout(new FlowLayout (FlowLayout.CENTER)); // sets the buttons back to center alignment
setBackground(color); // returns background color to original color
}
}
}
}
}
public static void main(String[] args)
{
Buttons btnwindow = new Buttons();
}
} // end class Buttons
* Buttons.java
*
*
*
**************************
import java.awt.*; // for FlowLayout
import javax.swing.*; // for JFrame, JLabel and JTextField
import java.awt.event.*; // for ActionListener and ActionEvent
public class Buttons extends JFrame // extends JFrame superclass
{
private JButton resetButton; // reset button
private JButton leftButton; // left button
private JButton colorButton; // color button
private JPanel buttonPanel; // windows panel
FlowLayout flt;
Color color;
Point point;
public Buttons()
{
super("Buttons Window");
{
setLayout(new FlowLayout (FlowLayout.CENTER)); // locates the window in center
setBounds(20, 120, 350, 250); // size of command window
setDefaultCloseOperation(E
createContents();
setVisible(true); // able to view, yes
buttonPanel = new JPanel(flt); // initialize the buttonPanel Within FlowLayout
leftButton = new JButton("Left"); // left button
colorButton = new JButton("Blue"); // color button
resetButton = new JButton("Reset"); // reset button
eventHandle = new EventHandle(evt);
point=new Point((Toolkit.getDefaultT
buttonPanel.add(leftButton
buttonPanel.add(colorButto
buttonPanel.add(resetButto
leftButton.addActionListen
colorButton.addActionListe
resetButton.addActionListe
class buttons implements ActionListener // event handler that implements ActionListener interface
{
public void actionPerformed(ActionEven
{
{
if (e.getSource() == leftButton) // if "left" button
{
setLayout (new FlowLayout(FlowLayout.LEFT
}
else if (e.getSource() == colorButton) // if "color" button
{
setBackground(color.BLUE);
}
else
{
setLayout(new FlowLayout (FlowLayout.CENTER)); // sets the buttons back to center alignment
setBackground(color); // returns background color to original color
}
}
}
}
}
public static void main(String[] args)
{
Buttons btnwindow = new Buttons();
}
} // end class Buttons
No, this is not good.
I suggest that you don't rush but modify with some sense, make sure that it compiles .
This code of yours does not comipile, say there is no createContents, there is no eventHandle, and so on.
there is no rush now - so make sure you have something sensible.
Then, there is no need to have two classes - like Buttons, and buttons - no, you don't need - neither ebeventhandle as a separate class,
nor buttons.
Just have one class Buttons, add annnonymouse evente handlers as we used to
do in tye previouys assigment
and in this way assesmble everything say
up to the finctionality within the events.
And make sure it at eleast compiles - even better shows you the buttons and then
paste the code
I suggest that you don't rush but modify with some sense, make sure that it compiles .
This code of yours does not comipile, say there is no createContents, there is no eventHandle, and so on.
there is no rush now - so make sure you have something sensible.
Then, there is no need to have two classes - like Buttons, and buttons - no, you don't need - neither ebeventhandle as a separate class,
nor buttons.
Just have one class Buttons, add annnonymouse evente handlers as we used to
do in tye previouys assigment
and in this way assesmble everything say
up to the finctionality within the events.
And make sure it at eleast compiles - even better shows you the buttons and then
paste the code
ASKER
It compiles, but some of this code was suggested by a friend of mine, and I don't get why I can't use the ContentPane to replace setLocation((point.x -150), point.y);??? It compiles, but the entire ContentPane moves left instead of just the buttons..
See below....
/************************* ********** ********** ********** ********** ********
* Buttons.java
*
*
*
************************** ********** ********** ********** ********** *********/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Buttons extends JFrame
{
JButton resetButton;
JButton leftButton;
JButton colorButton;
JPanel buttonPanel;
eventHandle evt;
FlowLayout flt;
Point point;
Color color;
public Buttons()
{
super("Buttons Window");
flt = new FlowLayout();
buttonPanel = new JPanel(flt);
resetButton = new JButton("Reset");
leftButton = new JButton("Left");
colorButton = new JButton("Blue");
evt = new eventHandle();
buttonPanel.add(leftButton ); // add left button
buttonPanel.add(colorButto n); // add color button
buttonPanel.add(resetButto n); // add reset button
getContentPane().add(butto nPanel); // button panel
leftButton.addActionListen er(evt); // action listeners for all buttons
colorButton.addActionListe ner(evt);
resetButton.addActionListe ner(evt);
setBounds(20, 120, 320, 200);
point=new Point((Toolkit.getDefaultT oolkit().g etScreenSi ze().width - getWidth())/2, (Toolkit.getDefaultToolkit ().getScre enSize().h eight-getH eight())/2 );
setLocation(point);
color = buttonPanel.getBackground( );
setDefaultCloseOperation(E XIT_ON_CLO SE);
setVisible(true);
}
class eventHandle implements ActionListener
{
public void actionPerformed(ActionEven t e)
{
{
if (e.getSource() == leftButton)
{
setLocation((point.x -150), point.y);
}
else if (e.getSource() == colorButton)
{
buttonPanel.setBackground( color.BLUE );
} else
{
setLocation(point);
buttonPanel.setBackground( color);
}
}
}
}
public static void main(String[] args)
{
Buttons btnwindow = new Buttons();
}
} // end class Buttons
See below....
/*************************
* Buttons.java
*
*
*
**************************
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Buttons extends JFrame
{
JButton resetButton;
JButton leftButton;
JButton colorButton;
JPanel buttonPanel;
eventHandle evt;
FlowLayout flt;
Point point;
Color color;
public Buttons()
{
super("Buttons Window");
flt = new FlowLayout();
buttonPanel = new JPanel(flt);
resetButton = new JButton("Reset");
leftButton = new JButton("Left");
colorButton = new JButton("Blue");
evt = new eventHandle();
buttonPanel.add(leftButton
buttonPanel.add(colorButto
buttonPanel.add(resetButto
getContentPane().add(butto
leftButton.addActionListen
colorButton.addActionListe
resetButton.addActionListe
setBounds(20, 120, 320, 200);
point=new Point((Toolkit.getDefaultT
setLocation(point);
color = buttonPanel.getBackground(
setDefaultCloseOperation(E
setVisible(true);
}
class eventHandle implements ActionListener
{
public void actionPerformed(ActionEven
{
{
if (e.getSource() == leftButton)
{
setLocation((point.x -150), point.y);
}
else if (e.getSource() == colorButton)
{
buttonPanel.setBackground(
} else
{
setLocation(point);
buttonPanel.setBackground(
}
}
}
}
public static void main(String[] args)
{
Buttons btnwindow = new Buttons();
}
} // end class Buttons
Great! Your friend is good.
It really does a lot.
Of course setLocation() has nothing to do with it - setLocation moves top-level window
on the screen - and to move them around within the window - it is somehow different.
Let me try to do something about it.
It really does a lot.
Of course setLocation() has nothing to do with it - setLocation moves top-level window
on the screen - and to move them around within the window - it is somehow different.
Let me try to do something about it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If I may ask what the point of the following is:
point=new Point((Toolkit.getDefaultT oolkit().g etScreenSi ze().width - getWidth())/2, (Toolkit.getDefaultToolkit ().getScre enSize().h eight-getH eight())/2 );
That was one of the pieces I got from my friend too and I don't get how this means anything to the program?
point=new Point((Toolkit.getDefaultT
That was one of the pieces I got from my friend too and I don't get how this means anything to the program?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
should "private" be before the following too?
JButton resetButton;
JButton leftButton;
JButton colorButton;
JPanel buttonPanel;
JButton resetButton;
JButton leftButton;
JButton colorButton;
JPanel buttonPanel;
ASKER
And why is this important?
super("Buttons Window");
super("Buttons Window");
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again for all your help! Take care and good luck in your future endeavors! I am officially done with programming after Thurs! Ya!!!!
Good luck!
for_yan, you really shouldn't be posting full solutions to people's homework. Especially since you are adding things that are good, but might not have been taught yet.
Also, if the FlowLayout isn't working, why not just check the current left position of the left button and shift all the other buttons that far over? You could even store this value for shifting them back, much like you do with the color.