GUI

from JAVA 2013. 5. 31. 15:45

윈도우 프로그램은 이벤트 드리븐 방식이라고 한다.

뭔가 명령을 주면 거기에 해당하는 처리 루틴을 수행하는것.

 

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class  WindowExample2
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame("Hello~~");
  frame.setPreferredSize(new Dimension(200, 70));
  frame.setLocation(500,400);
  Container contentPane = frame.getContentPane();
  JTextField text = new JTextField();
  JButton button = new JButton("확인");
  JLabel label = new JLabel("Hello");
  contentPane.add(text, BorderLayout.CENTER);
  contentPane.add(button, BorderLayout.EAST);
  contentPane.add(label, BorderLayout.SOUTH);
  ActionListener listener = new ConfirmButtonActionListener(text, label);
  button.addActionListener(listener);  
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);
 }
}

======================================================================================

import javax.swing.*;
import java.awt.event.*;
class  ConfirmButtonActionListener implements ActionListener
{
 JTextField text;
 JLabel label;
 ConfirmButtonActionListener(JTextField text, JLabel label){
  this.text = text;
  this.label = label;
 }
 public  void actionPerformed(ActionEvent e)
 {
  String name = text.getText();
  label.setText("Hello, " + name);
 }
}

 

 

 

그리드 레이아웃

import java.awt.*;
import javax.swing.*;
class  WindowExample3
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame("동물원");
  frame.setLocation(500,400);
  Container contentPane = frame.getContentPane();
  GridLayout layout = new GridLayout(3,2);
  contentPane.setLayout(layout);
  contentPane.add(new JButton("얼룩말"));
  contentPane.add(new JButton("사자"));
  contentPane.add(new JButton("코끼리"));
  contentPane.add(new JButton("코뿔소"));
  contentPane.add(new JButton("펭귄"));
  contentPane.add(new JButton("하이애나"));
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);
 }
}

 

 

그리드 레이아웃

import java.awt.*;
import javax.swing.*;
class  WindowExample4
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame("동물원");
  frame.setLocation(500,400);
  Container contentPane = frame.getContentPane();
  FlowLayout layout = new FlowLayout();
  contentPane.setLayout(layout);
  contentPane.add(new JButton("얼룩말"));
  contentPane.add(new JButton("사자"));
  contentPane.add(new JButton("코끼리"));
  contentPane.add(new JButton("코뿔소"));
  contentPane.add(new JButton("펭귄"));
  contentPane.add(new JButton("하이애나"));
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);
 }
}

 

 

박스 레이아웃

import java.awt.*;
import javax.swing.*;
class  WindowExample5
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame("동물원");
  frame.setLocation(500,400);
  Container contentPane = frame.getContentPane();
  BoxLayout layout = new BoxLayout(contentPane, BoxLayout.X_AXIS);
  contentPane.setLayout(layout);
  contentPane.add(new JButton("얼룩말"));
  contentPane.add(new JButton("사자"));
  contentPane.add(new JButton("코끼리"));
  contentPane.add(new JButton("코뿔소"));
  contentPane.add(new JButton("펭귄"));
  contentPane.add(new JButton("하이애나"));
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);
 }
}

 

 

테이블에 입력하면 커맨드창에 출력

import java.awt.*;
import javax.swing.*;
class  WindowExample7
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame("테이블 처리");
  frame.setPreferredSize(new Dimension(300,150));
  frame.setLocation(500,400);
  Container contentPane = frame.getContentPane();
  String colNames[] = {"이름", "나이", "성별"};
  Object data[][] = {{"김철수", 24, "남"},
       {"이순희", 21, "여"},
       {"박지영", 26, "여"}};
  JTable table = new JTable(data, colNames);
  JScrollPane scrollPane = new JScrollPane(table);
  contentPane.add(scrollPane, BorderLayout.CENTER);
  JButton button = new JButton("출력");
  button.addActionListener(new PrintActionListener(table));
  contentPane.add(button, BorderLayout.SOUTH);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);
 }
}
============================================================================

import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
class PrintActionListener implements ActionListener
{
 JTable table;
 PrintActionListener(JTable table){
  this.table=table;
 }

 public void actionPerformed(ActionEvent e){
  TableModel model = table.getModel();
  int rowNum = model.getRowCount();
  int colNum = model.getColumnCount();
  for(int col=0; col<colNum; col++){
   String colName = model.getColumnName(col);
   System.out.print(colName + "\t");
  }
  System.out.println();
  for(int row=0; row<rowNum; row++){
   for(int col=0; col<colNum; col++){
    Object obj = model.getValueAt(row,col);
    System.out.print(obj + "\t");
   }
   System.out.println();
  }
  System.out.println("------------------------------------------");
 }
}

GUI에서 입력하면 콘솔창에 결과값 나옴.

 

테이블의 행을 추가, 삭제

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
class  WindowExample8
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame("테이블 수정하기");
  frame.setPreferredSize(new Dimension(400,200));
  frame.setLocation(500,400);
  Container contentPane = frame.getContentPane();
  String colNames[] = {"이름","나이","성별"};
  DefaultTableModel model = new DefaultTableModel(colNames,0);
  JTable table = new JTable(model);
  contentPane.add(new JScrollPane(table), BorderLayout.CENTER);
  JPanel panel = new JPanel();
  JTextField text1 = new JTextField(6);
  JTextField text2 = new JTextField(3);
  JTextField text3 = new JTextField(2);
  JButton button1 = new JButton("추가");
  JButton button2 = new JButton("삭제");
  panel.add(new JLabel("이름"));
  panel.add(text1);
  panel.add(new JLabel("나이"));
  panel.add(text2);
  panel.add(new JLabel("성별"));
  panel.add(text3);
  panel.add(button1);
  panel.add(button2);
  contentPane.add(panel,BorderLayout.SOUTH);
  button1.addActionListener(
   new AddActionListener(table, text1, text2, text3));
  button2.addActionListener(new RemoveActionListener(table));
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);
 }
}
=================================================================================

import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
class  AddActionListener implements ActionListener
{
 JTable table;
 JTextField text1, text2, text3;
 AddActionListener(JTable table, JTextField text1, JTextField text2, JTextField text3){
  this.table = table;
  this.text1 = text1;
  this.text2 = text2;
  this.text3 = text3;
 }
 public void actionPerformed(ActionEvent e){
  String arr[] = new String[3];
  arr[0]=text1.getText();
  arr[1]=text2.getText();
  arr[2]=text3.getText();
  DefaultTableModel model = (DefaultTableModel)table.getModel();
  model.addRow(arr);
 }
}
===========================================================================

import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
class RemoveActionListener implements ActionListener
{
 JTable table;
 RemoveActionListener(JTable table){
  this.table = table;
 }
 public void actionPerformed(ActionEvent e){
  int row = table.getSelectedRow();
  if(row==-1) return;
  DefaultTableModel model = (DefaultTableModel)table.getModel();
  model.removeRow(row);
 }
}

 


 

 

자동차 그리기^^

import javax.swing.*;
import java.awt.*;
class  CarDrawingPanel extends JPanel
{
 public  void paint(Graphics g)
 {
  g.drawRect(100,110,200,40);
  g.drawRect(150,70,100,40);
  g.drawOval(125,150,30,30);
  g.drawOval(245,150,30,30);
  g.drawLine(50,180,350,180);
 }
}
==========================================

import java.awt.*;
import javax.swing.*;
class GraphicExample1
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame("자동차 그리기");
  frame.setLocation(500,700);
  frame.setPreferredSize(new Dimension(400,300));
  Container contentPane = frame.getContentPane();
  CarDrawingPanel drawingPanel = new CarDrawingPanel();
  contentPane.add(drawingPanel);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);
 }
}

 

이미지 로드하는 프로그램

import java.awt.*;
import javax.swing.*;
class  ImageExample1
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame("이미지 로딩~");
  frame.setLocation(500,200);
  frame.setPreferredSize(new Dimension(500,400));
  Container contentPane = frame.getContentPane();
  ImagePanel imagePanel = new ImagePanel();
  contentPane.add(imagePanel, BorderLayout.CENTER);
  JPanel controlPanel = new JPanel();
  JTextField text = new JTextField(30);
  JButton button = new JButton("이미지 로드");
  controlPanel.add(text);
  controlPanel.add(button);
  contentPane.add(controlPanel, BorderLayout.SOUTH);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  button.addActionListener(
   new LoadActionListener(text, imagePanel));
  frame.pack();
  frame.setVisible(true);
 }
}
=======================================================================

import java.awt.event.*;
import javax.swing.*;
class  LoadActionListener implements ActionListener
{
 JTextField text;
 ImagePanel imagePanel;
 LoadActionListener(JTextField text, ImagePanel imagePanel){
  this.text = text;
  this.imagePanel = imagePanel;
 }
 public void actionPerformed(ActionEvent e){
  imagePanel.setPath(text.getText());
  imagePanel.repaint();
 }
}
=====================================================================

import java.awt.*;
import javax.swing.*;
class ImagePanel extends JPanel
{
 Image image;
 Toolkit toolkit = getToolkit();
 void setPath(String path){
  image = toolkit.getImage(path);
 }
 public void paint(Graphics g){
  g.clearRect(0,0,getWidth(), getHeight());
  if(image!=null) g.drawImage(image,0,0,this);
 }

}

 


 

 

'JAVA' 카테고리의 다른 글

애플릿  (0) 2013.06.14
네트워크 통신  (0) 2013.06.07
A태그 생성기 보완한 코드. 텍스트 파일로 저장하는거 연구중..  (0) 2013.05.25
JDBC 과제. 단순히 학사정보 시스템을 출력하는 예제  (0) 2013.05.25
JDBC  (0) 2013.05.24
,