import javax.swing.*; //JFrame, JButton, JPanel, JTextField
import javax.swing.SwingConstants; //Gets the constants, like VERTICAL and HORIZONTAL
import java.awt.*; //Color, GridBagLayout, GridBagConstraints, BorderLayout
import java.awt.event.*; //ActionListener and the rest
import java.awt.Dimension;
class Mainframe extends JFrame {
EditColorsDialog editColorsDiag = new EditColorsDialog(this);
public Mainframe(String title) {
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
buildMenuLine();
buildButtonLine();
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.gridheight = 6;
DrawingPanel dPanel = new DrawingPanel();
dPanel.setBorder(BorderFactory.createLineBorder(Color.black));
add(dPanel, c);
}
private void buildMenuLine() {
JMenu mainMenu = new JMenu("File");
JMenuItem menuItem = new JMenuItem("Save");
MenuListener listener = new MenuListener();
mainMenu.add(menuItem);
menuItem.addActionListener(listener);
menuItem = new JMenuItem("Load");
mainMenu.add(menuItem);
menuItem.addActionListener(listener);
menuItem = new JMenuItem("Close");
mainMenu.add(menuItem);
menuItem.addActionListener(listener);
menuItem = new JMenuItem("Exit");
mainMenu.add(menuItem);
menuItem.addActionListener(listener);
JMenu optionsMenu = new JMenu("Options");
menuItem = new JMenuItem("Edit Colors");
optionsMenu.add(menuItem);
menuItem.addActionListener(listener);
menuItem = new JMenuItem("About");
optionsMenu.add(menuItem);
menuItem.addActionListener(listener);
JMenuBar menuLine = new JMenuBar();
menuLine.add(mainMenu);
menuLine.add(optionsMenu);
setJMenuBar(menuLine);
}
private void buildButtonLine() {
JToolBar buttonRow = new JToolBar("Colors", 1); //VERTICAL doesn't work, using 1
ButtonListener listener = new ButtonListener();
int buttonx = 50;
int buttony = 50;
Dimension d = new Dimension(buttonx, buttony);
JButton button = new JButton("Yellow");
button.setPreferredSize(d);
buttonRow.add(button);
button.addActionListener(listener);
button = new JButton("Red");
button.setPreferredSize(d);
buttonRow.add(button);
button.addActionListener(listener);
button = new JButton("Blue");
button.setPreferredSize(d);
buttonRow.add(button);
button.addActionListener(listener);
button = new JButton("Orange");
button.setPreferredSize(d);
buttonRow.add(button);
button.addActionListener(listener);
button = new JButton("Green");
button.setPreferredSize(d);
buttonRow.add(button);
button.addActionListener(listener);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 0;
c.gridy = 1;
c.gridheight = 6;
add(buttonRow, c);
}
private class MenuListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String itemName = event.getActionCommand();
System.out.println("You pushed the menu item named \"" + itemName +"\"..");
if (itemName == "Exit") {
System.out.println("Exiting..");
dispose();
System.exit(0);
} else if (itemName == "Edit Colors") {
editColorsDiag.showDialog();
}
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String buttonName = event.getActionCommand();
System.out.println("You pushed the button named \"" + buttonName +"\"..");
}
}
private class DrawingPanel extends JPanel {
public DrawingPanel() {
add(new JTextField("Drawing Window"));
add(new JTextField("- Testing"));
}
}
}
class EditColorsDialog extends JDialog {
JButton exitButton = new JButton("Close");
ButtonListener listener = new ButtonListener();
public EditColorsDialog(JFrame parent) {
super(parent, "Edit Colors", true);
add(new JLabel("Pane for editing colors"), BorderLayout.NORTH);
add(new JLabel(),BorderLayout.CENTER);
add(exitButton,BorderLayout.SOUTH);
exitButton.addActionListener(listener);
setSize(200,300);
setResizable(false);
}
public void showDialog() {
setVisible(true);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String buttonName = event.getActionCommand();
if ( buttonName == "Close" ) {
setVisible(false);
}
}
}
}
class Colorizer {
public static void main(String[] args) {
Mainframe newFrame = new Mainframe("Colorizer");
newFrame.setVisible(true);
newFrame.setSize(300,400);
newFrame.setResizable(false);
}
}
|