오늘의 코드
package edu.java.gui09;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuiMain09 {
private JFrame frame;
private JButton btnOutput;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain09 window = new GuiMain09();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain09() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(8, 35, 414, 216);
frame.getContentPane().add(textArea);
JCheckBox chckbxMusic = new JCheckBox("음악");
chckbxMusic.setBounds(8, 6, 59, 23);
frame.getContentPane().add(chckbxMusic);
JCheckBox chckbxMovie = new JCheckBox("영화");
chckbxMovie.setBounds(71, 6, 59, 23);
frame.getContentPane().add(chckbxMovie);
JCheckBox chckbxReading = new JCheckBox("독서");
chckbxReading.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chckbxReading.isSelected()) {
btnOutput.setEnabled(false);
} else {
btnOutput.setEnabled(true);
}
}
});
chckbxReading.setBounds(134, 6, 59, 23);
frame.getContentPane().add(chckbxReading);
btnOutput = new JButton("출력");
btnOutput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String result = "음악 : " + chckbxMusic.isSelected() + "\n" + "영화 : " + chckbxMovie.isSelected() + "\n" + "독서 : " +chckbxReading.isSelected() + "\n";
textArea.setText(result);
}
});
btnOutput.setBounds(325, 6, 97, 23);
frame.getContentPane().add(btnOutput);
}
}
package edu.java.gui08;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuiMain08 {
private JFrame frame;
private static final ImageIcon[] IMAGE_ICONS = {
new ImageIcon("res/dog1.jpg"),
new ImageIcon("res/dog2.jpg"),
new ImageIcon("res/dog3.jpg"),
new ImageIcon("res/dog4.jpg")
};
private int index = 0;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain08 window = new GuiMain08();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GuiMain08() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 465, 646);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblOutput = new JLabel();
lblOutput.setIcon(IMAGE_ICONS[0]);
lblOutput.setFont(new Font("굴림", Font.BOLD, 42));
lblOutput.setBounds(12, 10, 423, 216);
frame.getContentPane().add(lblOutput);
JButton btnPrev = new JButton("이전");
btnPrev.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (index > 0) {
index--;
} else {
index = IMAGE_ICONS.length - 1;
}
lblOutput.setIcon(IMAGE_ICONS[index]);
}
});
btnPrev.setBounds(12, 230, 204, 247);
frame.getContentPane().add(btnPrev);
JButton btnNext = new JButton("다음");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (index < IMAGE_ICONS.length - 1) {
index++;
} else { // 초기화
index = 0;
}
lblOutput.setIcon(IMAGE_ICONS[index]);
}
});
btnNext.setBounds(224, 230, 211, 247);
frame.getContentPane().add(btnNext);
JButton btnNewButton = new JButton("랜덤");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double rX = Math.random();
int x = (int) (IMAGE_ICONS.length * rX);
lblOutput.setIcon(IMAGE_ICONS[x]);
}
});
btnNewButton.setBounds(173, 518, 97, 23);
frame.getContentPane().add(btnNewButton);
}
}
package edu.java.gui09;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuiMain09 {
private JFrame frame;
private JButton btnOutput;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain09 window = new GuiMain09();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain09() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(8, 35, 414, 216);
frame.getContentPane().add(textArea);
JCheckBox chckbxMusic = new JCheckBox("음악");
chckbxMusic.setBounds(8, 6, 59, 23);
frame.getContentPane().add(chckbxMusic);
JCheckBox chckbxMovie = new JCheckBox("영화");
chckbxMovie.setBounds(71, 6, 59, 23);
frame.getContentPane().add(chckbxMovie);
JCheckBox chckbxReading = new JCheckBox("독서");
chckbxReading.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chckbxReading.isSelected()) {
btnOutput.setEnabled(false);
} else {
btnOutput.setEnabled(true);
}
}
});
chckbxReading.setBounds(134, 6, 59, 23);
frame.getContentPane().add(chckbxReading);
btnOutput = new JButton("출력");
btnOutput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String result = "음악 : " + chckbxMusic.isSelected() + "\n" + "영화 : " + chckbxMovie.isSelected() + "\n" + "독서 : " +chckbxReading.isSelected() + "\n";
textArea.setText(result);
}
});
btnOutput.setBounds(325, 6, 97, 23);
frame.getContentPane().add(btnOutput);
}
}
package edu.java.gui10;
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;
public class GuiMain10 {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain10 window = new GuiMain10();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain10() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmOpen = new JMenuItem("Open");
mntmOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// JFileChooser : 파일을 선택할 수 있는 팝업창
System.out.println("클릭!"); // 이해가 안되면 로그!
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(null);
System.out.println(result);
if(result == JFileChooser.APPROVE_OPTION) { // 확인 버튼 클릭시
System.out.println("파일 선택");
File seleced = chooser.getSelectedFile();
System.out.println(seleced.getAbsolutePath());
} else { // 취소 버튼 클릭시
System.out.println("취소");
}
}
});
mnFile.add(mntmOpen);
JMenuItem mntmSave = new JMenuItem("Save");
mntmSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
mnFile.add(mntmSave);
JSeparator separator = new JSeparator();
mnFile.add(separator);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// ConfirmDialog :
// Yes(확인) - No(아니요) - Cancel(취소) 버튼을 갖는 다이얼로그
int result = JOptionPane.showConfirmDialog(frame.getContentPane(), "종료하시겠습니까?"); // 컴퍼넌트의 자식들이다. (즉 다형성)
// parantComponent : 부모 컴퍼넌트를 설정.
// 현재 컴퍼넌트를 보여줄 위치 설정
System.out.println("선택 결과 : " + result);
if(result == JOptionPane.YES_OPTION) {
// 프로그램 동료 :
// System.exit(0); 정상 종료
// System.exit(0 이외의 숫자); 비정상 종료
System.exit(0);
} else {
System.out.println("취소");
}
}
});
mnFile.add(mntmExit);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
JMenuItem mntmAbout = new JMenuItem("About");
mntmAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 메시지와 Ok 버튼만 있는 다이얼로그 : MessageDialog
JOptionPane.showMessageDialog(frame, "버전 1.0");
}
});
mnHelp.add(mntmAbout);
frame.getContentPane().setLayout(null);
} // end initialize()
} //
package edu.java.gui11;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuiMain11 {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain11 window = new GuiMain11();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain11() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btn1 = new JButton("New Frame");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// JFrame myFrame = new JFrame();
// myFrame.setBounds(10, 10, 400, 400);
// myFrame.setVisible(true);
// 일반적으로 새로운 프레임이나, 다이얼로그를 생성할 때는
// 각 클래스들을 상속받는 자식 클래스를 만들어서 사용하는 것이
// 편의성 면에서 더 좋음
MyFrame myframe = new MyFrame(); // Myframe작업이 다끝나고 출력
myframe.setVisible(true);
// frame.setVisible(false); // 메인 프레임을 안보이게
}
});
btn1.setFont(new Font("굴림", Font.PLAIN, 30));
btn1.setBounds(12, 10, 410, 107);
frame.getContentPane().add(btn1);
JButton btn2 = new JButton("New Dialog");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MyDialog myDialog = new MyDialog();
myDialog.setVisible(true);
}
});
btn2.setFont(new Font("굴림", Font.PLAIN, 30));
btn2.setBounds(12, 127, 410, 107);
frame.getContentPane().add(btn2);
}
}
package edu.java.gui11;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class MyDialog extends JDialog {
private final JPanel contentPanel = new JPanel();
public MyDialog() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
package edu.java.gui11;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
public class MyFrame extends JFrame { // main 지울 것!
private JPanel contentPane; // getcontentpane과 같은 말이 된다.
public MyFrame() {
// JFrame.EXIT_ON_CLOSE : 프로그램 전체 종료
// JFrame.DISPOSE_ON_CLOSE : 현재 창만 종료
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setBounds(100, 100, 450, 300);
contentPane = new JPanel(); // frame.getContentPane() 동일
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnNewButton = new JButton("New button");
contentPane.add(btnNewButton, BorderLayout.CENTER);
}
}
package edu.java.gui12;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuiMain12 {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain12 window = new GuiMain12();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain12() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(158, 50, 116, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
MyFrame myframe = new MyFrame(text);
myframe.setVisible(true);
}
});
btnNewButton.setBounds(171, 112, 97, 23);
frame.getContentPane().add(btnNewButton);
}
}
package edu.java.gui12;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
public class MyFrame extends JFrame { // main 지울 것!
private JPanel contentPane; // getcontentpane과 같은 말이 된다.
public MyFrame(String text) {
// JFrame.EXIT_ON_CLOSE : 프로그램 전체 종료
// JFrame.DISPOSE_ON_CLOSE : 현재 창만 종료
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setBounds(100, 100, 450, 300);
contentPane = new JPanel(); // frame.getContentPane() 동일
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnNewButton = new JButton(text);
contentPane.add(btnNewButton, BorderLayout.CENTER);
}
}
package edu.java.gui13;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import java.awt.Component;
public class GuiMain13 {
private JFrame frame;
private JPanel topPanel; // 로그인 버튼, 로그아웃 버튼, 아이디 입력 텍스트필드 등을 포함하는 판넬
private JLabel lblInfo; // 정보 출력 레이블
private JTextField txtInputId; // 아이디 입력 텍스트 필드
private JButton btnLogin; // 로그인 버튼
private JButton btnLogout; // 로그아웃 버튼
private JPanel contentPanel; // 시작할 때 생성할 기본판넬
private JPanel loginPanel; // 로그인 했을 때 보이는 판넬
private JPanel logoutPanel; // 로그아웃 했을 때 보이는 판넬
private Component currentComponent; // 현재 가지고 있는 컴퍼넌트 확인 변수
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain13 window = new GuiMain13();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain13() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
topPanel = new JPanel();
topPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
frame.getContentPane().add(topPanel, BorderLayout.NORTH);
topPanel.setLayout(new GridLayout(1, 0, 0, 0));
lblInfo = new JLabel("안녕하세요");
topPanel.add(lblInfo);
txtInputId = new JTextField();
topPanel.add(txtInputId);
txtInputId.setColumns(10);
btnLogin = new JButton("로그인");
btnLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String id = txtInputId.getText();
// 입력받은 id가 "test"인 경우에만
if(id.equals("test")) {
lblInfo.setText(id + "님 환영합니다.");
btnLogin.setEnabled(false);
btnLogout.setVisible(true);
// 현재 판넬 제거
frame.getContentPane().remove(currentComponent);
frame.revalidate(); // 컴퍼넌트 재검토
frame.repaint(); // 컴퍼넌트 다시 그림
// 로그인 했을 때 보여줄 판넬 추가
loginPanel = new LoginPanel();
// 로그인 판넬을 프레임에 연결
frame.getContentPane().add(loginPanel, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
currentComponent = loginPanel;
}
}
});
topPanel.add(btnLogin);
btnLogout = new JButton("로그아웃");
btnLogout.setVisible(false);
btnLogout.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lblInfo.setText("잘가세요!");
btnLogin.setEnabled(true);
btnLogout.setVisible(false);
// 현재 판넬 제거
frame.getContentPane().remove(currentComponent);
frame.revalidate();
frame.repaint();
// 로그아웃 했을 때 보여줄 판넬 추가
logoutPanel = new JPanel();
JLabel lblLogout = new JLabel("로그아웃 되었습니다.");
logoutPanel.add(lblLogout);
// 로그아웃 판넬 연결
frame.getContentPane().add(logoutPanel, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
// 현재 컴퍼넌트 정보 변경
currentComponent = logoutPanel;
}
});
topPanel.add(btnLogout);
contentPanel = new JPanel();
contentPanel.setBackground(Color.PINK);
frame.getContentPane().add(contentPanel, BorderLayout.CENTER);
// 현재 컴퍼넌트에 기본 판넬 저장
// 이게 없으면 판단을 할 수가 없다.
currentComponent = contentPanel;
}
} // end GuiMain13
package edu.java.gui13;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
public class LoginPanel extends JPanel{
public LoginPanel() {
setBackground(Color.WHITE);
setLayout(null);
JLabel lblNewLabel = new JLabel("로그인 되었습니다");
lblNewLabel.setBounds(103, 98, 225, 60);
add(lblNewLabel);
}
}
package edu.java.gui14;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
public class GuiMain14 {
private JFrame frame;
private JLabel lblDate;
int count = 100;
private JProgressBar progressBar;
private JDialog dialog;
private Timer timer;
private int year, month, day, hour, minute, seconds;
private Calendar cal;
private JButton btnNewButton2;
private boolean isStop;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain14 window = new GuiMain14();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain14() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
lblDate = new JLabel();
lblDate.setBounds(12, 22, 410, 50);
lblDate.setFont(new Font("굴림", Font.BOLD, 30));
frame.getContentPane().add(lblDate);
currentDate();
progressBar = new JProgressBar();
progressBar.setBounds(12, 0, 410, 20);
progressBar.setValue(count);
frame.getContentPane().add(progressBar);
JButton btnNewButton = new JButton("시작");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startTimer();
}
});
btnNewButton.setBounds(216, 228, 97, 23);
frame.getContentPane().add(btnNewButton);
btnNewButton2 = new JButton("종료");
btnNewButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isStop = true;
}
});
btnNewButton2.setBounds(325, 228, 97, 23);
frame.getContentPane().add(btnNewButton2);
JLabel lblDate_1 = new JLabel();
lblDate_1.setText("6교시까지 일단 고고");
lblDate_1.setFont(new Font("굴림", Font.BOLD, 30));
lblDate_1.setBounds(12, 107, 410, 50);
frame.getContentPane().add(lblDate_1);
dialog = new MyDialog();
dialog.setVisible(false);
}
private void startTimer() {
isStop = false;
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
if(isStop) {
timer.cancel();
}
if (count > 0) {
count--;
progressBar.setValue(count);
} else {
dialog.setVisible(true);
timer.cancel();
}
}
};
timer = new Timer();
timer.schedule(timerTask, 1, 1000);
}
private void currentDate() {
cal = GregorianCalendar.getInstance();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH) + 1;
day = cal.get(Calendar.DAY_OF_MONTH);
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
seconds = cal.get(Calendar.SECOND);
String dateString = String.format("%02d/%02d/%02d %02d:%02d:%02d", year, month, day, hour, minute, seconds);
lblDate.setText(dateString);
cal.add(Calendar.SECOND, 1);
}
};
timer = new Timer();
timer.schedule(timerTask, 1, 1000);
}
}
package edu.java.gui14;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class MyDialog extends JDialog {
private final JPanel contentPanel = new JPanel();
/**
* Create the dialog.
*/
public MyDialog() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
JLabel lblNewLabel = new JLabel("타이머 종료");
contentPanel.add(lblNewLabel);
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}