자바

자바 8주차 <SWING 2>

고양이오즈 2021. 12. 12. 20:21

2021년 7월 작성글

 

<요약>

이번 시간에는 awt에 없는 컴포넌트들에 대해 볼 것.

안드로이드에도 있는 화면을 유연하게 볼 수 있는 컴포넌트.

자료구조를 표현하기 위한 컴포넌트임.

 

JTextField, JTextArea, JEditorPane, JPassword, JTextPane

한 라인을 표현하는 컴포넌트

=>JTextField, JPassword(입력 된 값을 가려줌. 사용자 편의를 위하여!)

전체 텍스트를 표시하는 컴포넌트

=>JTextArea

스타일을 줄 수 있는 컴포넌트

=>JEditorPane(에디트가 가진 기능을 지원하는 펜), JTextPane

둘 다 텍스트 에리어에 패널 기능이 들어간 것. 무언가를 붙일 수 있음.

 

주요 메소드

copy(), cut(), paste() 등

 

constructs

JTextField

JTextField(String text, int columns) : 컬럼 수와 처음 들어갈 숫자 초기값을 지원해줌.

 

append(String text) 그 다음 자리부터 추가

insert(String text, int position) 그 자리부터 추가

 

JTabbedPane

탭 메뉴를 만드는 컴포넌트. 여러 개의 패널 객체가 한 헤드라인에 보임.

여러 항목을 만들고 탭 패널에 붙여줘야 함.

 

JTable

여러 튜플을 붙일 수 있음.

데이터베이스가 하나의 테이블이라는 단어와 똑같음.

튜플 : 테이블의 한 레코드. 한 줄에 여러 개의 속성이 모인 것.

컬럼의 수 : 한 튜플이 몇 개의 속성을 가지고 있느냐

로우의 수 : 튜플, 즉 레코드의 개수.

테이블을 지정해서 직접 만들 수도 있고 테이블 모델을 만들어서 사용할 수도 있음.

테이블 모델 : 자주 쓰는 레코드를 미리 만들어 놓는 것. 인터페이스임. 벡터를 사용하여 셀 값의 객체를 저장함. addColum(), addRow() 명령어로 쉽게 데이터를 추가 가능.

JTableModel은 JTable이 표 형식의 모델을 조회하는데 사용할 방법을 지정함.

 

JTree

루트, 노드, 리프로 구성되어 있는 계층적인 구조를 보여주는 레이아웃

모델이나 노드는 인터페이스(공통된 함수를 모아둔 것)임.

테이블과 비슷한 모델을 가지고 있음. 일반적으로 자주쓰는 함수들을 위해서 인터페이스와 클래스로 정의된 내용이 있음. 사용하면 편함.

 

JMeue

awt와 같음. 같은 메뉴바를 만들 수 있음.

 

JDialog

스윙셋에서는 다이얼로그 기능이 다양했음. 우리는 그렇게 사용하려면 여러 코드를 사용해 형태를 바꿔 사용할 수 있음.

추가적인 구현이 필요함. 버튼이 누르면 뜨고 사라지게 하는 이벤트와 연동을 시켜야 함.

 

JFileChooser

io때 사용이 되는 내용. 파일 인아웃이 되기 때문.

오픈 버튼을 누르면 파일을 고를 수 있는 프레임이 뜨는 것. 그것이 파일 chooser임.

파일을 불러들이기 위한 기능임.

<구현 예시>

이벤트를 연결하기 위해서 actionlistener랑 itemlistener를 implement로 받고, 각 이벤트를 지정해 주었음.

사진이 나오는 위치가 마음에 들지 않아서 기존에 사진 나오는 위치가 배열로 설정된 것을 그냥 이미지 아이콘으로 받아서 이미지를 바꿔 세팅하는 식으로 지정함.

또한 버튼 옆에 이미지가 나오면 좋을 것 같아서 전체 패널에 그리드레이아웃을 설정하고 버튼이 있는 패널, 이미지가 있는 패널을 더해줌.

<코드>

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

class TabTest implements ItemListener, ActionListener{

JFrame jf;

JTabbedPane tabpane;

JLabel text;

JRadioButton small, medium, large;

ButtonGroup size;

JCheckBox[] buttons = new JCheckBox[3];

String[] fruits = { "cryinggreen", "green", "sadgreen" };

JLabel pictureLabel = new JLabel();

ImageIcon[] icon = new ImageIcon[3];

public TabTest(String msg){

jf = new JFrame(msg);

tabpane = new JTabbedPane();

JPanel one = new JPanel();

JPanel two = new JPanel();

JPanel three = new JPanel();

JPanel four = new JPanel();

JPanel fourbuttom = new JPanel();

JPanel fourtop = new JPanel();

JPanel buttompanel = new JPanel();

JPanel threechoice = new JPanel(new GridLayout(1, 0));

JPanel checkPanel = new JPanel(new GridLayout(0, 1));

one.setBackground(Color.red);//패널의 색을 지정함

two.setBackground(Color.green);

three.setBackground(Color.blue);

four.setBackground(Color.white );

fourtop.setBackground(Color.white );

fourbuttom.setBackground(Color.white );

ImageIcon logo = new ImageIcon("../Images/"+"sm_logo.gif");

JLabel label=new JLabel("SMU Logo", logo, JLabel.RIGHT);//패널에 글자와 이미지를 붙임

label.setFont(new Font(" 굴림 ", Font.BOLD, 30));

one.add(label);

ImageIcon logo1 = new ImageIcon("../Images/"+"plus.png");

JLabel label1=new JLabel("plus Logo", logo1, JLabel.RIGHT);

two.add(label1);

 

for (int i = 0; i < 3; i++) {

buttons[i] = new JCheckBox(fruits[i]);

buttons[i].addItemListener(this);

pictureLabel= new JLabel();

icon[i] = new ImageIcon("../Images/" +fruits[i]+ ".png");}

for (int i = 0; i < 3; i++)

checkPanel.add(buttons[i]);

threechoice.add(checkPanel);

threechoice.add(pictureLabel);

three.add(threechoice);

JPanel mediumPanel = new JPanel();

small = new JRadioButton("Small Size");

medium = new JRadioButton("Medium Size");

large = new JRadioButton("Large Size");

size = new ButtonGroup();

size.add(small);

size.add(medium);

size.add(large);

small.addActionListener(this);

medium.addActionListener(this);

large.addActionListener(this);

mediumPanel.add(small);

mediumPanel.add(medium);

mediumPanel.add(large);

buttompanel = new JPanel();

text = new JLabel("Did not select the size");

text.setForeground(Color.red);

buttompanel.add(text);

JLabel sizelabel = new JLabel("What size of coffee do you like to order?");

fourtop.add(mediumPanel);

fourbuttom.add(sizelabel);

four.setLayout(new BorderLayout());

four.add(fourtop, BorderLayout.NORTH);

four.add(fourbuttom, BorderLayout.CENTER);

four.add(buttompanel, BorderLayout.SOUTH);

tabpane.addTab("SMU",one);//각 패널을 탭펜에 붙인다.

tabpane.addTab("TAN", two );

tabpane.addTab("미정 2", three );

tabpane.addTab("미정 3", four);

jf.getContentPane().add(tabpane,BorderLayout.CENTER);//탭페인을 프레임에 붙이고 가운데 처리한다.

jf.setSize(500,400);

jf.setVisible(true);

}

public void actionPerformed(ActionEvent e){

if(e.getSource() == small){

text.setText("Ordered Small Size");

}

if(e.getSource() == medium){

text.setText("Ordered Medium Size");

}

if(e.getSource() == large){

text.setText("Ordered large Size");

}

}

public void itemStateChanged(ItemEvent e) {

ImageIcon image = null;

Object source = e.getItemSelectable();//이벤트 발생한 내용이 소스임

for (int i = 0; i < 3; i++)

if(source == buttons[i]){

if(e.getStateChange() == ItemEvent.DESELECTED)//셀렉트되지 않으면

pictureLabel.setIcon(null);

else

pictureLabel.setIcon(icon[i]);}

}

}