From: Nessuno on
Hi,

I am a newbie in Java. While learning from the Horstmann I have
modified a little example from the book. The problem is that the
buttons it creates are never shown.

Please, would someone explain what I am missing? The code follows.
Thanks in advance to everyone who will reply.

fabio

ButtonTest.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonTest
{
public static void main( String[] args )
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

@SuppressWarnings("serial")
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(300, 200);
buttonPanel = new JPanel();
makeButton("Red", Color.RED);
makeButton("Green", Color.GREEN);
makeButton("Blue", Color.BLUE);
}

private void makeButton(String name, final Color backgroundColor)
{
JButton button = new JButton(name);
buttonPanel.add(button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
}

});

}

private JPanel buttonPanel;
}
From: Joshua Cranmer on
On 02/21/2010 06:26 PM, Nessuno wrote:
> Please, would someone explain what I am missing? The code follows.
> Thanks in advance to everyone who will reply.
>
> buttonPanel = new JPanel();
> makeButton("Red", Color.RED);
> makeButton("Green", Color.GREEN);
> makeButton("Blue", Color.BLUE);

You make a buttonPanel and then... never add it to anything? So all of
your buttons are now on some panel which is never attached to anything else.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
From: Knute Johnson on
On 2/21/2010 3:26 PM, Nessuno wrote:
> Hi,
>
> I am a newbie in Java. While learning from the Horstmann I have
> modified a little example from the book. The problem is that the
> buttons it creates are never shown.
>
> Please, would someone explain what I am missing? The code follows.
> Thanks in advance to everyone who will reply.
>
> fabio
>
> ButtonTest.java
>
> import javax.swing.*;
> import java.awt.*;
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
>
> public class ButtonTest
> {
> public static void main( String[] args )
> {
> EventQueue.invokeLater(new Runnable()
> {
> public void run()
> {
> ButtonFrame frame = new ButtonFrame();
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> frame.setVisible(true);
> }
> });
> }
> }
>
> @SuppressWarnings("serial")
> class ButtonFrame extends JFrame
> {
> public ButtonFrame()
> {
> setTitle("ButtonTest");
> setSize(300, 200);
> buttonPanel = new JPanel();
> makeButton("Red", Color.RED);
> makeButton("Green", Color.GREEN);
> makeButton("Blue", Color.BLUE);

add(buttonPanel);

> }
>
> private void makeButton(String name, final Color backgroundColor)
> {
> JButton button = new JButton(name);
> buttonPanel.add(button);
> button.addActionListener(new ActionListener()
> {
> public void actionPerformed(ActionEvent event)
> {
> buttonPanel.setBackground(backgroundColor);
> }
>
> });
>
> }
>
> private JPanel buttonPanel;
> }

You need to add the buttonPanel to the JFrame.

--

Knute Johnson
email s/nospam/knute2010/

From: markspace on
Knute Johnson wrote:
>> public static void main( String[] args )
>> {
>> EventQueue.invokeLater(new Runnable()
>> {
>> public void run()
>> {
>> ButtonFrame frame = new ButtonFrame();
>> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

>> frame.setVisible(true);
>
> You need to add the buttonPanel to the JFrame.


The OP should also call pack() on the frame before it is displayed.
From: Knute Johnson on
On 2/21/2010 4:59 PM, markspace wrote:
> Knute Johnson wrote:
>>> public static void main( String[] args )
>>> {
>>> EventQueue.invokeLater(new Runnable()
>>> {
>>> public void run()
>>> {
>>> ButtonFrame frame = new ButtonFrame();
>>> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>
> frame.pack();
>
>>> frame.setVisible(true);
>>
>> You need to add the buttonPanel to the JFrame.
>
>
> The OP should also call pack() on the frame before it is displayed.

He calls setSize() in the JFrame constructor. Not the best method but
it does work in this instance.

--

Knute Johnson
email s/nospam/knute2010/

 |  Next  |  Last
Pages: 1 2
Prev: Java app installer
Next: ArrayList issue