K
KingKongBundy
Could anyone tell me how I would modify this code (in the
gridbaglayout if possible) so that when the JFrame pops up, there is
'white space' between the JLabel and JButton. Currently, when this
pops up, the JLabel and JButton are on top of one another. When I
comment the line 'dialog.pack();' out, I get the desired JFrame look
(in terms of white space), but the entire JLabel message does not
display without having to re-sizing the window.
Thanks!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class Question
{
public static void main(String[] args)
{
String greetMessage = "Hello world - Hopefully this will work";
GreetingMessage dialog = new GreetingMessage(greetMessage);
dialog.pack();
dialog.show();
}
} //Question
class GreetingMessage extends JFrame
{
public GreetingMessage(String message)
{
Toolkit theKit = getToolkit();
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
Container contentPane = getContentPane();
contentPane.setLayout(layout);
//Set constraints for JLabel
constraints.weightx = 0.0;
constraints.weighty = 1.0;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.CENTER;
JLabel newLabel = new JLabel(message);
layout.setConstraints(newLabel,constraints);
contentPane.add(newLabel);
constraints.fill = constraints.NONE;
constraints.weightx = 2.0;
constraints.weighty = 2.0;
constraints.gridx = 0;
constraints.gridy = 100;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.CENTER;
JButton ok = new JButton("Ok");
layout.setConstraints(ok,constraints);
contentPane.add(ok);
setSize(400,400);
} //GreetingMessage constructor
} //GreetingMessage
gridbaglayout if possible) so that when the JFrame pops up, there is
'white space' between the JLabel and JButton. Currently, when this
pops up, the JLabel and JButton are on top of one another. When I
comment the line 'dialog.pack();' out, I get the desired JFrame look
(in terms of white space), but the entire JLabel message does not
display without having to re-sizing the window.
Thanks!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class Question
{
public static void main(String[] args)
{
String greetMessage = "Hello world - Hopefully this will work";
GreetingMessage dialog = new GreetingMessage(greetMessage);
dialog.pack();
dialog.show();
}
} //Question
class GreetingMessage extends JFrame
{
public GreetingMessage(String message)
{
Toolkit theKit = getToolkit();
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
Container contentPane = getContentPane();
contentPane.setLayout(layout);
//Set constraints for JLabel
constraints.weightx = 0.0;
constraints.weighty = 1.0;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.CENTER;
JLabel newLabel = new JLabel(message);
layout.setConstraints(newLabel,constraints);
contentPane.add(newLabel);
constraints.fill = constraints.NONE;
constraints.weightx = 2.0;
constraints.weighty = 2.0;
constraints.gridx = 0;
constraints.gridy = 100;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.CENTER;
JButton ok = new JButton("Ok");
layout.setConstraints(ok,constraints);
contentPane.add(ok);
setSize(400,400);
} //GreetingMessage constructor
} //GreetingMessage