.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
public class MyFrame extends JFrame {
JTextArea ta = new JTextArea(7,20);
MyFrame() {
setTitle("파일 저장");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(ta));
createMenu();
setSize(300, 220);
setVisible(true);
}
void createMenu() {
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("파일");
JMenuItem save = new JMenuItem("저장");
file.add(save);
mb.add(file);
setJMenuBar(mb);
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (ta != null) {
String fileName = JOptionPane.showInputDialog("저장할 파일명을 입력하세요.");
JTextField field = new JTextField();
getContentPane().add(field);
try {
FileWriter fout = new FileWriter(fileName);
String t = ta.getText();
StringTokenizer st = new StringTokenizer(ta.getText(), "\n");
while (st.hasMoreTokens()) {
fout.write(st.nextToken());
fout.write("\r\n");
}
fout.close();
} catch (IOException e1) { }
} else {
JOptionPane.showMessageDialog(null, "아무것도 입력하지 않았습니다."
, "주의", JOptionPane.ERROR_MESSAGE);
}
}
});
}
public static void main(String[] args) {
new MyFrame();
}
}
※ 텍스트 영역에 아무것도 입력하지 않을 때 경고 메세지가 뜨지 않음.
.
.
.
.
.
.
.
.
.
.
public class MyFrame extends JFrame {
Container contentPane = getContentPane();
ImageIcon icon = new ImageIcon("\"C:\\Users\\신승미\\Pictures\\배경화면\\꽃.jpeg\"");
JLabel label = new JLabel(icon);
MyFrame() {
setTitle("이미지 드래깅");
setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane.setLayout(null);
MyListener listener = new MyListener();
label.addMouseListener(listener);
label.addMouseMotionListener(listener);
setSize(300,300);
setVisible(true);
}
class MyListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
int x = getX();
int y = getY();
label.setLocation(x,y);
label.setSize(50,50);
contentPane.add(label);
}
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
int now_x = getX();
int now_y = getY();
}
}
public static void main (String[] args){
new MyFrame();
}
}
.
.
.
.
.
public class MyFrame extends JFrame {
MyFrame() {
setTitle("Ten 레이블 클릭");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JLabel[] num = new JLabel[10];
for(int i = 0; i<10; i++) {
num[i] = (JLabel) Integer.toString(i);
contentPane.add(num[i]);
num[i].setLocation((int)(Math.random()*50 +1), (int)(Math.random()*50 +1));
}
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
}
}