<a href="타겟URL"><img src="이미지경로"></a>를 만들어주는 간단한 swing 프로그래밍을 짜봤다.
작성된 내용을 다시 텍스트 파일로 저장하려면 어떻게 해야하나??
이걸 좀 연구해보고 싶다.
소스코드 :
import javax.swing.*;
import java.util.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.io.*;
class TagGenerator implements ActionListener
{
//public TagGenerator(){} 만약을 위해 만든 생성자
JFrame frame;
JTextField urlField;
JTextField ImagePathField;
JTextArea area;
JButton button;
/*****************************************************************************************************
이벤트 처리 : 두개의 텍스트필드에서 값을 입력받아 HTML태그로 만든뒤 텍스트 에리어에 뿌려줌.
******************************************************************************************************/
public void actionPerformed(ActionEvent e){
String url = urlField.getText();
String localPath = ImagePathField.getText();
String result = "<a href = \"" + url + "\">" + "<img src = \"" + localPath + "\"></a>\n" ;
String toSend = area.getText();
//System.out.print(result);
area.append(result);
urlField.setText("");
ImagePathField.setText("");
urlField.requestFocus();
/*************************************************************************************************
텍스트 에리어에 출력된 결과값을 텍스트 파일로 저장하고 싶음(근데 안됨ㅠㅠ)
**************************************************************************************************/
FileWriter toTxt = null;
try{
toTxt = new FileWriter("saved.txt");
toTxt.write(toSend);
}
catch(IOException ioe){}
}
/*public void actionPerformed2(ActionEvent e){
FileWriter toTxt = null;
try{
toTxt = new FileWriter("hahaha.txt");
toTxt.write(sendStr);
}
catch(IOException ioe){}
}
*/
/***************************************************************
기본적인 프레임 세팅
***************************************************************/
public void view(){
JFrame frame = new JFrame("A태그 생성기^^");
frame.add(layout());
frame.setSize(1024, 768);
frame.setVisible(true);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension f = frame.getSize();
frame.setLocation((int) (screen.getWidth() - f.getWidth()) / 2, (int) (screen.getHeight() - f.getHeight()) / 2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit toolkit = Toolkit.getDefaultToolkit(); // 이미지 아이콘 바꾸기
Image img = toolkit.getImage("icon.gif");
frame.setIconImage(img);
}
/***************************************************************
컨테이너 레이아웃
***************************************************************/
JPanel layout(){ //입력모드
JPanel hapPannel = new JPanel(); //upPannel + downPannel + savePannel
JPanel upPannel = new JPanel(); //upPannel1 + upPannel2
JPanel upPannel1 = new JPanel(); //upPannel1_1 + upPannel1_2
JPanel upPannel1_1 = new JPanel();
JPanel upPannel1_2 = new JPanel();
JPanel upPannel2 = new JPanel(); //upPannel2_1 + upPannel2_2
JPanel upPannel2_1 = new JPanel();
JPanel upPannel2_2 = new JPanel();
JPanel downPannel = new JPanel();
JPanel savePannel = new JPanel(); //저장버튼을 달기 위한 패널
hapPannel.setLayout(new BorderLayout());
upPannel.setLayout(new BorderLayout());
EnterKeyEvent_On_urlField();
EnterKeyEvent_On_localPathField(); //이벤트 리스너 붙인곳
upPannel1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
JLabel urlLabel = new JLabel("Target URL : ");
urlLabel.setPreferredSize(new Dimension(100,30));
upPannel1_1.add(urlLabel);
upPannel1_2.add(urlField);
upPannel1.add(upPannel1_1);
upPannel1.add(upPannel1_2);
upPannel2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
JLabel Pathlabel = new JLabel("Image Path : ");
upPannel2_1.add(Pathlabel);
Pathlabel.setPreferredSize(new Dimension(100,30));
upPannel2_2.add(ImagePathField);
upPannel2.add(upPannel2_1);
upPannel2.add(upPannel2_2);
upPannel.add(upPannel1,"North");
upPannel.add(upPannel2,"South");
area = new JTextArea();
area.setBorder(new LineBorder(new Color(179,179,179)));
area.setFont(new Font(null, Font.PLAIN, 15));
downPannel.add(area);
JScrollPane scroll = new JScrollPane(area);
int len= area.getText().length();
area.setCaretPosition(len); //자동 스크롤
downPannel.setBorder(new LineBorder(new Color(179,179,179)));
saveToTextFile(); /*************안됌...ㅠㅠ***********/
savePannel.add(button);
hapPannel.add(upPannel,"North");
hapPannel.add(downPannel,"Center");
hapPannel.add(savePannel, "South");
hapPannel.add(scroll);
return hapPannel;
}
void EnterKeyEvent_On_urlField(){
urlField = new JTextField();
urlField.setPreferredSize(new Dimension(850, 30));
//urlField.addActionListener(this);
}
void EnterKeyEvent_On_localPathField(){
ImagePathField = new JTextField();
ImagePathField.setPreferredSize(new Dimension(850, 30));
ImagePathField.addActionListener(this);
}
void saveToTextFile(){
button = new JButton("텍스트 파일로 저장");
button.setPreferredSize(new Dimension(190, 55));
button.setFont(new Font(null, Font.PLAIN, 12));
button.setBackground(new Color(255,255,238,238));
button.addActionListener(this);
}
public static void main(String[] args)
{
TagGenerator obj = new TagGenerator();
obj.view();
}
}