컴파일> javac -d . HelloSwingApp.java
실행> java HelloSwingApp


/*
 * HelloSwingApp.java
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HelloSwingApp extends JFrame implements ActionListener {
 
   JButton button1;
   JLabel label1;

   public static void main(String[] args) {
      HelloSwingApp app = new HelloSwingApp();
      app.setTitle("Hello Win");
      app.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent we) {
              System.exit(0);
          }
      });
      app.setLocation(50, 50);
      app.setSize(200, 140);
      app.setVisible(true);
   }

   public HelloSwingApp() {
      Container pane1 = this.getContentPane();
      pane1.setLayout(null);
      button1 = new JButton("클릭하세요!");
      button1.addActionListener(this);
      button1.setBounds(40, 70, 120, 30);
      label1 = new JLabel("");
      label1.setBounds(30, 20, 120, 20);
      pane1.setBackground(new Color(0xFF, 0xFF, 0xEE));
      pane1.add(label1);
      pane1.add(button1);
   }

   private JDialog dialog1;
   private JPanel panDiag;

   public void actionPerformed(ActionEvent e) {
      Object src = e.getSource();
      if (src instanceof JButton) {
          if ((JButton) src == button1) {
              dialog1 = new JDialog(this, "메시지");
              dialog1.setSize(180, 120);
              dialog1.setLocation(300, 200);
              dialog1.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent we) {
                      dialog1.dispose();
                  }
              });
         
              panDiag = new JPanel() {
                  public void paint(Graphics g) {
                      g.drawString( "안녕하세요?", 30, 30 );
                  }
              };

              dialog1.add(panDiag);
              dialog1.setVisible(true);
              label1.setText("Hello, world!");
          }
      }
   }
}






Creative Commons License 
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다
Posted by Scripter
,