📄 mypicturedialog.java
字号:
package mydesktop;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.*;
import java.net.*;
public class MyPictureDialog extends JDialog{
JPanel panel =new JPanel();
JButton butUpload = new JButton("上传");
JButton butOk = new JButton("确定");
JButton butCanel = new JButton("取消");
JTextField urlText = new JTextField();
JPanel picturePanel = new JPanel();
JLabel pictureLabel = new JLabel();
JPanel uploadPanel = new JPanel();
JPanel southPanel = new JPanel();
private String [] FILE_OPEN_NAME={"JPG(*.jpg)","GIF(*.gif)"};
public MyPictureDialog(JFrame frame,String title ,boolean modal){
super(frame,title,modal);
try{
jbInit();
pack();
}
catch(Exception e){
}
}
public void jbInit()throws Exception{
uploadPanel.add(urlText);
uploadPanel.add(butUpload);
panel.setLayout(new BorderLayout());
panel.add(uploadPanel,BorderLayout.NORTH);
panel.add(picturePanel,BorderLayout.CENTER);
panel.add(southPanel,BorderLayout.SOUTH);
picturePanel.add(pictureLabel);
southPanel.add(butOk,null);
southPanel.add(butCanel,null);
getContentPane().add(panel);
butOk.addActionListener(new PictureOKActionListener(this));
butCanel.addActionListener(new PictureCanelActionListener(this));
butUpload.addActionListener(new UploadActionListener(this));
}
public void actionListener(){
this.dispose();
}
public void setPicture(String str){
URL url = getClass().getResource(str);
Image image = Toolkit.getDefaultToolkit().getImage(url);
image.getScaledInstance(20,20, Image.SCALE_SMOOTH);
ImageIcon ii = new ImageIcon(image);
pictureLabel.setIcon(ii);
this.urlText.setText(str);
}
public void uploadListener(){
JFileChooser chooser = new JFileChooser();
// FileFilter filter = new FileNameExtensionFilter ("JPG & GIF Images", "jpg", "gif");
// fd.setFilenameFilter(new );
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
urlText.setText("images/"+chooser.getSelectedFile().getName());
}
else return;
try{
URL url = getClass().getResource(urlText.getText());
Image image = Toolkit.getDefaultToolkit().getImage(url);
ImageIcon ii = new ImageIcon(image);
pictureLabel.setIcon(ii);
}
catch(Exception e){
}
}
}
class PictureOKActionListener implements java.awt.event.ActionListener{
MyPictureDialog d;
PictureOKActionListener(MyPictureDialog d){
this.d = d;
}
public void actionPerformed(ActionEvent e){
d.actionListener();
}
}
class PictureCanelActionListener implements ActionListener{
MyPictureDialog d;
PictureCanelActionListener(MyPictureDialog d){
this.d = d;
}
public void actionPerformed(ActionEvent e) {
d.actionListener();
}
}
class UploadActionListener implements ActionListener{
MyPictureDialog d;
UploadActionListener(MyPictureDialog d){
this.d =d ;
}
public void actionPerformed(ActionEvent ae){
d.uploadListener();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -