We help IT Professionals succeed at work.

Button Program

javadummy2011
javadummy2011 asked
on
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
Comment
Watch Question

Awarded 2010
Top Expert 2013

Commented:
Shouldn't you be setting the background of the entire window, not just the buttonPanel?
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.

Author

Commented:
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.....
Awarded 2010
Top Expert 2013

Commented:
On line 41 you typed
getContentPane().add(buttonPanel)
and so added the buttonPanel to the window.
getContentPane().setBackground(yourColor) should work better.

Color color = getContentPane().getBackground() 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.
Awarded 2011
Awarded 2011

Commented:
YOu code does not compile because variable btn is not declare and not assifgned value in class eeventHandle
Awarded 2010
Top Expert 2013

Commented:
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.
Awarded 2011
Awarded 2011

Commented:
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
Awarded 2011
Awarded 2011

Commented:
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.

Author

Commented:
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....
Awarded 2011
Awarded 2011

Commented:
OK, please, post code.

Author

Commented:
post actual code or the file?

Author

Commented:
I mean copy and paste code into comment or attach file?
Awarded 2011
Awarded 2011

Commented:
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
Awarded 2010
Top Expert 2013

Commented:
Attaching the file is fine.
Awarded 2011
Awarded 2011

Commented:
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

Author

Commented:
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

Author

Commented:
/*************************************************************************
* 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(EXIT_ON_CLOSE);  // 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.getDefaultToolkit().getScreenSize().width- getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height-getHeight())/2);

buttonPanel.add(leftButton);  // add left button
buttonPanel.add(colorButton);  // add color button
buttonPanel.add(resetButton);  // add color button

leftButton.addActionListener(evt);  // add actionlisteners to all buttons
colorButton.addActionListener(evt);
resetButton.addActionListener(evt);

class buttons implements ActionListener  // event handler that implements ActionListener interface
{

public void actionPerformed(ActionEvent 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
Awarded 2011
Awarded 2011

Commented:
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

Author

Commented:
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(colorButton);  // add color button
buttonPanel.add(resetButton);  // add reset button
getContentPane().add(buttonPanel);  // button panel

leftButton.addActionListener(evt);  // action listeners for all buttons
colorButton.addActionListener(evt);
resetButton.addActionListener(evt);
setBounds(20, 120, 320, 200);

point=new Point((Toolkit.getDefaultToolkit().getScreenSize().width- getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height-getHeight())/2);

setLocation(point);

color = buttonPanel.getBackground();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

class eventHandle implements ActionListener
{

public void actionPerformed(ActionEvent 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

Awarded 2011
Awarded 2011

Commented:
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.
Awarded 2011
Awarded 2011
Commented:
This also shifts them and resets - both color and shift:

/*************************************************************************
* 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;
FlowLayout flt1;
Point point;
Color color;

public Buttons()
{
super("Buttons Window");

flt = new FlowLayout(FlowLayout.CENTER);
flt1 = new FlowLayout(FlowLayout.LEFT);

buttonPanel = new JPanel();
buttonPanel.setLayout(flt);    

resetButton = new JButton("Reset");
leftButton = new JButton("Left");
colorButton = new JButton("Blue");
evt = new eventHandle();

buttonPanel.add(leftButton);  // add left button
buttonPanel.add(colorButton);  // add color button
buttonPanel.add(resetButton);  // add reset button
getContentPane().add(buttonPanel);  // button panel

leftButton.addActionListener(evt);  // action listeners for all buttons
colorButton.addActionListener(evt);
resetButton.addActionListener(evt);
setBounds(20, 120, 320, 200);

point=new Point((Toolkit.getDefaultToolkit().getScreenSize().width- getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height-getHeight())/2);

setLocation(point);

color = buttonPanel.getBackground();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

class eventHandle implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
{
if (e.getSource() == leftButton)
{

buttonPanel.setLayout(flt1);
buttonPanel.validate();    
//setLocation((point.x -150), point.y);

}
else if (e.getSource() == colorButton)
{
buttonPanel.setBackground(color.BLUE);

} else  if (e.getSource() == resetButton)
{

//setLocation(point);
buttonPanel.setBackground(color);
buttonPanel.setLayout(flt);
buttonPanel.validate();     

}
}
}
}

public static void main(String[] args)
{

Buttons btnwindow = new Buttons();
}
}  // end class Buttons

Open in new window

Author

Commented:
If I may ask what the point of the following is:

point=new Point((Toolkit.getDefaultToolkit().getScreenSize().width- getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height-getHeight())/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?
Awarded 2011
Awarded 2011
Commented:
I also think that may be removed unless you have the requirement to have the whole frame
centered on the screen - I think there is no such requirement, and it can be achieved easier.

Other than that - I think your friend thought to use it in conjunction with setLocation()
but he/she was confused on this subject as I explained above.
setLocation() is moving the top window around your whole screen.
Shifting the buttons within the panel - that is the busines of layout.

This is without any "point"

setLocationRelativeTo(null); <--- this centers the whole window in the middle of your screen



/*************************************************************************
* 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;
FlowLayout flt1;
Point point;
Color color;

public Buttons()
{
super("Buttons Window");

flt = new FlowLayout(FlowLayout.CENTER);
flt1 = new FlowLayout(FlowLayout.LEFT);

buttonPanel = new JPanel();
buttonPanel.setLayout(flt);    

resetButton = new JButton("Reset");
leftButton = new JButton("Left");
colorButton = new JButton("Blue");
evt = new eventHandle();

buttonPanel.add(leftButton);  // add left button
buttonPanel.add(colorButton);  // add color button
buttonPanel.add(resetButton);  // add reset button
getContentPane().add(buttonPanel);  // button panel

leftButton.addActionListener(evt);  // action listeners for all buttons
colorButton.addActionListener(evt);
resetButton.addActionListener(evt);
setBounds(20, 120, 320, 200);

setLocationRelativeTo(null);


color = buttonPanel.getBackground();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

class eventHandle implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
{
if (e.getSource() == leftButton)
{

buttonPanel.setLayout(flt1);
buttonPanel.validate();    


}
else if (e.getSource() == colorButton)
{
buttonPanel.setBackground(Color.BLUE);

} else  if (e.getSource() == resetButton)
{


buttonPanel.setBackground(color);
buttonPanel.setLayout(flt);
buttonPanel.validate();     

}
}
}
}

public static void main(String[] args)
{

Buttons btnwindow = new Buttons();
}
}  // end class Buttons

Open in new window

Author

Commented:
should "private" be before the following too?

JButton resetButton;
JButton leftButton;
JButton colorButton;
JPanel buttonPanel;

Author

Commented:
And why is this important?

super("Buttons Window");
Awarded 2011
Awarded 2011
Commented:

i think you may
have al of this private if they want you to have as many private as possible

at least it works this way also:

private JButton resetButton;
private JButton leftButton;
private JButton colorButton;
private JPanel buttonPanel;

private eventHandle evt;
private FlowLayout flt;
private FlowLayout flt1;
//Point point;
private Color color;

Author

Commented:
Thanks again for all your help!  Take care and good luck in your future endeavors!  I am officially done with programming after Thurs!  Ya!!!!
Awarded 2011
Awarded 2011

Commented:
Good luck!
 
Awarded 2010
Top Expert 2013

Commented:
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.