import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class Project extends JFrame {
JTextField tf;
JButton btn;
JTextArea ta;
JPanel p;
JLabel imglbl = new JLabel();
Project() {
this.setTitle("1번 유형");
this.setSize(800, 500);
createMenu();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
p.setLayout(new FlowLayout());
tf = new JTextField(60);
btn = new JButton("확인");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.append(tf.getText() + "\n");
tf.setText("");
}
});
ta = new JTextArea(40, 60);
p.add(tf);
p.add(btn);
p.add(ta);
this.setContentPane(p);
this.setVisible(true);
}
void createMenu(){
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem[] menuItem = new JMenuItem[2];
String[] menuTitle = {"저장하기", "끝내기"};
for(int i=0; i
menuItem[i].addActionListener(new ShowAction());
fileMenu.add( menuItem[i] );
if(i==0 || i==3)
fileMenu.addSeparator();
}
mb.add(fileMenu);
this.setJMenuBar(mb);
}
class ShowAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
switch(cmd){
case "저장하기" :
SaveAs();
break;
case "끝내기" :
int result = JOptionPane.showConfirmDialog(null,"끝내시겠습니까?","종료", JOptionPane.YES_NO_OPTION);
if( result==JOptionPane.CLOSED_OPTION )
break;
else if( result==JOptionPane.YES_OPTION){
System.exit(0);
break;
}
else
break;
}
}
}
public void SaveAs() {
String text = ta.getText();
try
{
BufferedWriter out = new BufferedWriter(new FileWriter("c:\\Test.txt"));
out.write(text, 0, text.length());
out.flush();
out.close();
} catch(IOException except) {}
}
public static void main(String[] args) {
new Project();
}
}
'IT > JAVA' 카테고리의 다른 글
[JAVA/자바] 체크박스(CheckBox) 예제 - 2 (0) | 2017.05.30 |
---|---|
[JAVA/자바] 체크박스(CheckBox) 예제 - 1 (0) | 2017.05.30 |
[JAVA/자바] 텍스트 문자열 예제 (JTextField / JTextArea) (0) | 2017.05.30 |
[JAVA/자바] JMenuBar 를 이용한 간단한 예제 (0) | 2017.05.30 |
[JAVA/자바] 스캐너(Scanner)를 이용한 성적 처리 프로그램(학번, 이름, 과목명, 출석, 과제, 시험) (0) | 2017.05.30 |