package ch19;
import java.awt.BorderLayout;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import java.io.File;
import java.io.IOException;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Ch1904 extends JFrame
{
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel1 = null;
private Ch1904Panel jPanel2 = null;
private JLabel jLabel = null;
private JTextField jTextField = null;
private JButton jButton = null;
/**
* This is the default constructor
*/
public Ch1904()
{
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize()
{
this.setSize(573, 341);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane()
{
if (jContentPane == null)
{
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel1(), BorderLayout.NORTH);
jContentPane.add(getJPanel2(), BorderLayout.SOUTH);
}
return jContentPane;
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1()
{
if (jPanel1 == null)
{
jPanel1 = new JPanel();
jLabel = new JLabel();
jLabel.setText("JLabel");
jPanel1.setLayout(new FlowLayout());
jPanel1.add(jLabel, null);
jPanel1.add(getJTextField(), null);
jPanel1.add(getJButton(), null);
}
return jPanel1;
}
/**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private Ch1904Panel getJPanel2()
{
if (jPanel2 == null)
{
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
jPanel2 = new Ch1904Panel();
jPanel2.setLayout(new GridBagLayout());
}
return jPanel2;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField()
{
if (jTextField == null)
{
jTextField = new JTextField();
jTextField.setColumns(35);
}
return jTextField;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton()
{
if (jButton == null)
{
jButton = new JButton();
jButton.setText("열기");
jButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
// TODO Auto-generated Event stub actionPerformed()
JFileChooser j=new JFileChooser();
//파일넣으면 0, 취소는 1;
//int result = j.showDialog(null, "이미지화일열기");
//System.out.println(result);
j.showDialog(null, "이미지화일열기");
File file = j.getSelectedFile();
//파일이 null 일경우 에러남.
//System.out.println(file.getName());
if(file != null)
{
try
{
jPanel2.setImage(file);
} catch (IOException e1)
{
// TODO 자동 생성된 catch 블록
e1.printStackTrace();
}
}
}
});
}
return jButton;
}
} // @jve:decl-index=0:visual-constraint="10,10"
******** Ch1904Panel.java **********
package ch19;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Ch1904Panel extends JPanel
{
private BufferedImage image;
private static final long serialVersionUID = 1L;
public void setImage(File file) throws IOException
{
image = ImageIO.read(file);
repaint();
ImageIO.write(image, "gif", new File("D:\\study\\java\\Project\\sample\\src\\ch19\\button2.jpg"));
}
@Override
protected void paintComponent(Graphics g)
{
// TODO 자동 생성된 메소드 스텁
//Swing에서는 기존 그래픽내용 소거하려면
//상위 클래스의 paintConponent메서드를 재호출해야 한다.
super.paintComponent(g);
if(image != null)
{
g.drawImage(image, 0, 0, this);
}
}
/**
* This method initializes this
*
* @return void
*/
private void initialize()
{
this.setSize(300, 200);
this.setLayout(new GridBagLayout());
}
}