Consider what we did back in our discussion of events, when we created a
number of JFrame with a number of JButtons.
Here's an
example:

and here's the program that produced the above frame:
package swingExamples;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class FlowButtons1Panel extends JPanel
{
public FlowButtons1Panel()
{
setBackground(Color.lightGray);
add(m_yellow);
add(m_blue);
add(m_red);
}
////////////// Data //////////////////
private JButton m_yellow = new JButton("Yellow");
private JButton m_blue = new JButton("Blue");
private JButton m_red = new JButton("Red");
}
class FlowButtons1Frame extends JFrame
{
public FlowButtons1Frame()
{
setTitle("FlowButtons");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.add( new FlowButtons1Panel() );
}
}
public class FlowButtons1
{
public static void main(String[] args)
{
FlowButtons1Frame frame = new FlowButtons1Frame();
frame.setVisible(true);
}
}
|
Notice that the buttons are nicely centered, horizontally.
|