GridBagLayout
Author |
Message |
Witchcraft
|
Posted: Sat May 16, 2009 5:41 pm Post subject: GridBagLayout |
|
|
I'm fairly experienced with Java however quite new to its GUI components. Anyway, for the final project in my course, I need to design who wants to be a millionaire using GUI. I want to do well, so I'm trying to use the GridBagLayout which gives me more control at a cost of increased difficulty. So there are no errors currently in my program, however im trying to get my first button to display and only a blank window appears. If someone could tell me whats wrong with my program, that would be great.
Thanks
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Millionaire
{
static JButton answerA, answerB, answerC, answerD, finalAnswer;
static JLabel lbltitle, lblquestion;
public static void guiApp ()
{
JPanel panel = new JPanel (new GridBagLayout ());
GridBagConstraints gbc = new GridBagConstraints ();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 3;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 2;
answerA = new JButton ("A");
panel.add (answerA, gbc);
ButtonHandler onClick = new ButtonHandler ();
answerA.addActionListener (onClick);
JFrame frame = new JFrame ("Who Wants To Be A Millionaire");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.pack ();
frame.setVisible (true);
}
private static class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
}
}
public static void main (String[] args)
{
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
guiApp ();
}
}
);
}
}
[/list] |
|
|
|
|
|
Sponsor Sponsor
|
|
|
apomb
|
Posted: Sat May 16, 2009 6:39 pm Post subject: RE:GridBagLayout |
|
|
You're missing a paren in this chunk.
code: | public static void main (String[] args)
{
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{ |
other than that, i'd suggest using code tags instead of list.. |
|
|
|
|
|
Witchcraft
|
Posted: Sat May 16, 2009 7:54 pm Post subject: RE:GridBagLayout |
|
|
Are you sure, becuase I have another GUI program that works perfectly (but isnt a GridBagLayout) and this section in it is identicle. Just to make sure where should the parenthesis be exactly? |
|
|
|
|
|
DemonWasp
|
Posted: Sat May 16, 2009 9:18 pm Post subject: RE:GridBagLayout |
|
|
apomb: That's the way it's supposed to be. The end-paren is on a lower line. This is how Java does anonymous functions.
@witchcraft: Take a look at this example from SUN. They use frame.getContentPane() to get the JPanel that contains the contents of the JFrame. You make your own JPanel, but never add it to the JFrame. You have two options here:
1. Use frame.setContentPane( panel ); to put your JPanel into the frame
2. Do as they do - instead of creating your own panel, just use the existing one from the JFrame. |
|
|
|
|
|
Witchcraft
|
Posted: Sun May 17, 2009 6:18 pm Post subject: RE:GridBagLayout |
|
|
Thanks DemonWasp, that really clears things up |
|
|
|
|
|
|
|