import javax.swing.*;
public class DialogTest {
public static void main(String[] args){
DialogFrame
frame = new DialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
// new class
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.*;
public class DialogFrame extends JFrame
{
public DialogFrame()
{
setTitle("DialogTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
DialogPanel
panel = new DialogPanel();
add(panel);
//construct a File Menu
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
//The About
item shows the About dialog
JMenuItem
aboutItem = new JMenuItem("About");
aboutItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent
event)
{
if (dialog == null) //first time
dialog = new AboutDialog(DialogFrame.this);
dialog.setVisible(true); //pop up
dialog
}
});
fileMenu.add(aboutItem);
//The Exit item exits the program
JMenuItem
exitItem = new JMenuItem("Exit");
exitItem.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent
event)
{
System.exit(0);
}
});
addComponentsToPane(panel);
}
public static void addComponentsToPane(Container pane)
{
final boolean shouldFill = true;
final boolean shouldWeightX = true;
final boolean RIGHT_TO_LEFT =
false;
// vsg -This is a method and it cannot be with a constructor.. not sure about this
if (RIGHT_TO_LEFT)
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill)
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
button = new JButton("Back");
if (shouldWeightX)
c.weightx =
1.0;
c.weighty =
1.0;
c.gridx =
0;
c.gridy =
4;
c.anchor = GridBagConstraints.PAGE_END;
pane.add(button, c);
button = new JButton("Home");
c.weighty =
1.0;
c.gridx =
1;
c.gridy =
4;
c.anchor = GridBagConstraints.PAGE_END;
pane.add(button, c);
button = new JButton("Exit");
c.weighty =
1.0;
c.gridx =
2;
c.gridy = 4;
c.anchor = GridBagConstraints.PAGE_END;
pane.add(button, c);
button = new JButton("Next");
c.ipady =
0; //reset to default
c.weighty =
1.0; //request any extra vertical
space
c.anchor = GridBagConstraints.PAGE_END;
//bottom
of space
c.gridx =
3; //aligned with button 2
c.gridwidth
= 1; //2 columns wide
c.gridy =
4; //fourth row
pane.add(button, c);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
private AboutDialog dialog;
// vsg missing closing bracket - I'll show you my trick
}
//New Class
// You were missing the Panel Class
import javax.swing.*;
import java.awt.*;
public class DialogPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
//Think about where drawingString could be helpful
g.drawString("Grammar
Gorilla", MESSAGE_X, MESSAGE_Y);
}
// Try different sizes both big and
small
public static final int MESSAGE_X = 75;
public static final int MESSAGE_Y = 100;
}
// New Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AboutDialog extends JDialog{
public AboutDialog(JFrame owner){
super(owner, "About DialogTest", true);
// add HTML label to center
add(new JLabel("<html><h1>Grammar
Gorillas</h1></html>"), BorderLayout.CENTER);
//OK button closes the dialog
JButton
ok = new JButton("OK");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent
event){
setVisible(false);
}
});
// add OK button to southern border
JPanel
panel = new JPanel();
panel.add(ok);
add(panel,BorderLayout.SOUTH);
setSize(250,150);
}
}