| CIT
597 Assignment 8: GUI Generator Fall 2007, David Matuszek |
Read in an XML file that describes a GUI, and generate a Java program that implements that GUI.
Your program will implement only a very limited set of GUI features. Specifically,
it will create a class that extends JFrame, and within that class you may
have:
JMenuBar, containing one or more JMenus, each containing
one or more JMenuItems.JPanels.JFrame and the JPanels may each use one of three layout managers:
FlowLayout, BorderLayout, and GridLayout. JPanel may contain one or more JPanels, JButtons
and JTextAreas.JTextArea is embedded in a JScrollPane. All of the above will be in the constructor for the class. In addition, your
program should create a main method to create the GUI, make it visible, and
provide a means to quit the program. The purpose of this main method is to
simplify testing.
Use this DTD. I believe it is correct; let me know if you find any problems with it.
<!ELEMENT JFrame (JMenuBar?, JPanel+)> |
In the above, a JPanel will use a position of NORTH, SOUTH, EAST, WEST,
or
CENTER if it is inside a JFrame or JPanel that uses a
BorderLayout manager. If a JFrame or JPanel specifies grid as
a layout,
it will use the rows and
columns attributes, but not otherwise. These have to be optional
(#IMPLIED) because DTDs are too weak to require them in some places and disallow
them in others.
Here is an example XML file for creating a GUI. Your program may be tested with a different XML file.
<?xml version="1.0" encoding="UTF-8"?> |
Important note: The following is not a result of running my version of this program (because I haven't written it yet). Instead, it's my impression of the kind of result I expect. Your generated program may vary considerably from this, but it should behave the same.
import java.awt.*;
import javax.swing.*;
public class SimpleTranslatorGui extends JFrame {
public SimpleTranslatorGui() {
setLayout(new BorderLayout());
JMenuBar myMenuBar = new JMenuBar();
setJMenuBar(myMenuBar);
JMenu menu1 = new JMenu("File");
myMenuBar.add(menu1);
JMenuItem menuItem1 = new JMenuItem("Load...");
menu1.add(menuItem1);
JMenuItem menuItem2 = new JMenuItem("Save As...");
menu1.add(menuItem2);
JPanel mainPanel = new JPanel();
add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(new GridLayout(2, 1));
JTextArea inputArea = new JTextArea(10, 30);
mainPanel.add(new JScrollPane(inputArea));
JTextArea outputArea = new JTextArea(10, 30);
mainPanel.add(new JScrollPane(outputArea));
JPanel controlPanel = new JPanel();
add(controlPanel, BorderLayout.SOUTH);
controlPanel.setLayout(new FlowLayout());
JButton translateButton = new JButton("Translate");
controlPanel.add(translateButton);
JButton quitButton = new JButton("Quit");
controlPanel.add(quitButton);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SimpleTranslatorGui gui = new SimpleTranslatorGui();
gui.pack();
gui.setVisible(true);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
|
Use Swing's JFileChooser to select the input file to be read
and the output file to be written.
This type of program can be very long, or you can come up with some
good methods that do most of the work for you. For example, you could easily
write a method that generates (that is, writes all the code for) the main method;
all it needs is a String parameter
that tells the name given to the JFrame.