Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The key points are: Gui, Controls, Listeners, Appear, Three Step Gui, Swing Gui Overview, Swing Hierarchy, Listener Interfaces, Button Example, Textfield Example
Typology: Slides
1 / 57
private void makeMenuBar() // Create menu bar; add shortcuts. { int shortcut_mask = Toolkit.getDefaultToolkit(). getMenuShortcutKeyMask();
JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar);
// create the File menu JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); :
JMenuItem openItem = new JMenuItem("Open");
openItem. setAccelerator (KeyStroke.getKeyStroke( KeyEvent.VK_O, shortcut_mask)); // ctrl-o
openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { File f = chooseImage (); if (f != null) imagePanel. displayImage (f); pack(); // triggers resizing to fit image }
});
fileMenu.add(openItem);
:
private File chooseImage()
// open file chooser and let the user select // an image file
{
int returnVal = fileChooser.showOpenDialog(null); if (returnVal != JFileChooser.APPROVE_OPTION) return null; // cancelled
return fileChooser.getSelectedFile();
} // end of chooseImage()
public static void main(String[] args) { new ImageViewer(); }
} // end of ImageViewer class
public void displayImage(File f)
// load and set the image for this panel
{
BufferedImage image = loadImage (f); if (image != null) { width = image.getWidth(); height = image.getHeight(); panelImage = image;
invalidate(); repaint(); // triggers a panel redrawing // with the new image }
} // end of displayImage()
private BufferedImage loadImage(File imageFile) // load an image file and returns it as a // BufferedImage. { try { BufferedImage image = ImageIO.read(imageFile); if (image == null ||(image.getWidth() < 0)) // probably bad file format return null; return image; } catch (IOException e) { return null; } } // end of loadImage()