[Java] [Exer3] Chương trình xem ảnh đơn giản


/*
* cusomized by: mathhoang
* Y!M: vietnam_hoangminhnguyen@yahoo.com
* addr: http://mathhoang.blogspot.com
*
*/

package ImageViewer;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class MyFile {
public static void main(String[] args) {
JFrame myFirstGUI = new MyFirstGUI();
myFirstGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFirstGUI.setVisible(true);
}

}

class MyFirstGUI extends JFrame
{
private static final int GUI_HEIGHT = 300;
private static final int GUI_WIDTH = 400;
private JLabel label;
private JFileChooser chooser;

public MyFirstGUI(){
setTitle("This is my first Java GUI Application");
setBackground(Color.blue);
setDefaultLookAndFeelDecorated(false);
setSize(GUI_HEIGHT, GUI_WIDTH);

// set up components
label = new JLabel();
add(label);
chooser = new JFileChooser(new File("."));

JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

JMenu menuItem = new JMenu("File");
menuBar.add(menuItem);

JMenuItem openItem = new JMenuItem("Open");
menuItem.add(openItem);
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION){
String path = chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(path));
}

}
});

JMenuItem exitItem = new JMenuItem("Exit");
menuItem.add(exitItem);
exitItem.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
}


---
name: nguyen minh hoang
Y!M : vietnam_hoangminhnguyen@yahoo.com
net name: mathhoang

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...