Files
G4G0-1/Semester 1/Programming 1/Java/examples/projects/chapter13/imageviewer0-3/ImageViewer.java
2024-01-15 20:14:10 +00:00

88 lines
2.1 KiB
Java
Executable File

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* ImageViewer is the main class of the image viewer application. It builds
* and displays the application GUI.
*
* To start the application, create an object of this class.
*
* @author Michael Kölling and David J. Barnes.
* @version 0.3
*/
public class ImageViewer
{
private JFrame frame;
/**
* Create an ImageViewer show it on screen.
*/
public ImageViewer()
{
makeFrame();
}
// ---- implementation of menu functions ----
/**
* Open function: open a file chooser to select a new image file.
*/
private void openFile()
{
// this is some test output, until we do this properly
System.out.println("open file");
}
/**
* Quit function: quit the application.
*/
private void quit()
{
System.exit(0);
}
// ---- swing stuff to build the frame and all its components ----
/**
* Create the Swing frame and its content.
*/
private void makeFrame()
{
frame = new JFrame("ImageViewer");
makeMenuBar(frame);
Container contentPane = frame.getContentPane();
JLabel label = new JLabel("I am a label. I can display some text.");
contentPane.add(label);
// building is done - arrange the components and show
frame.pack();
frame.setVisible(true);
}
/**
* Create the main frame's menu bar.
* @param frame The frame that the menu bar should be added to.
*/
private void makeMenuBar(JFrame frame)
{
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
// create the File menu
JMenu fileMenu = new JMenu("File");
menubar.add(fileMenu);
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(e -> openFile());
fileMenu.add(openItem);
JMenuItem quitItem = new JMenuItem("Quit");
quitItem.addActionListener(e -> quit());
fileMenu.add(quitItem);
}
}