Re: JButton question
- From: IchBin <weconsultants@xxxxxxxxx>
- Date: Sun, 26 Aug 2007 23:24:04 -0400
On Mon, 27 Aug 2007 02:06:43 GMT, "Z.K." <nospam@xxxxxxxxxx> wrote:
I have a simple java gui that I wrote which adds 3 buttons and some
text. It seems to work, but the buttons seem like they are all
different sizes. How to I get them to be all the same size. I tried
setSize(), but that did not seem to work. My code is below.
Z.K.
code:
import javax.swing.*;
import java.awt.*;
public class NotHelloWorld
{
public static void main(String[] args)
{
NotHelloWorldFrame frame = new NotHelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class NotHelloWorldFrame extends JFrame
{
public NotHelloWorldFrame()
{
setTitle("NotHelloWorld");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
NotHelloWorldPanel panel = new NotHelloWorldPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
class NotHelloWorldPanel extends JPanel
{
public NotHelloWorldPanel()
{
NotHelloWorldButtonPanel bpanel = new NotHelloWorldButtonPanel();
add(bpanel);
}
public void paintComponent(Graphics g)
{
g.drawString("Not a Hello World program", MESSAGE_X, MESSAGE_Y);
}
public static final int MESSAGE_X = 75;
public static final int MESSAGE_Y = 100;
}
class NotHelloWorldButtonPanel extends JPanel
{
public NotHelloWorldButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
//redButton.setSize(5,10);
//yellowButton.setSize(5,10);
//blueButton.setSize(5,10);
add(yellowButton);
add(blueButton);
add(redButton);
}
}
You need to use a layout manager. Look at Sun's tutorial here:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
--
Thanks in Advance... http://weconsulting.org
IchBin, Philadelphia, Pa, USA http://ichbinquotations.weconsulting.org
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
.
- References:
- JButton question
- From: Z.K.
- JButton question
- Prev by Date: JButton question
- Previous by thread: JButton question
- Index(es):