grid = 가로,세로 나눌 수 있음
layout = 컨테이너에 (바탕되는 것 : contentPane)
adapt -> 인터페이스
void setForeground(color) //전경색 설정
void setBackground(color) //배경색 설정
void setOpaque(boolean) // 불투명성 설정
void setFont(Font) // 폰트 설정
Font getFont() // 폰트 리턴
void setEnabled(boolean) //컴포넌트 활성화/비활성화
void setVisible(boolean) //컴포넌트 보이기 /숨기기
boolean isVisible() // 컴포넌트의 보이는 상태 리턴
int getWidth() //폭리턴
int getHeight() //높이리턴
int getX() // x좌표 리턴
int getY() // y좌표 리턴
Point getLocationOnScreen() //스크린 좌표상에서의 컴포넌트 좌표
void setLocation(int, int) //위치지정
void setSize(int,int) //크기지정
Component add(Component) // 자식 컴포넌트 추가
void remove(Component) // 자식 컴포넌트 제거
void removeAll() // 모든 자식 컴포넌트 제거
Component[] getComponents() // 자식 컴포넌트 배열 리턴
Container getParent() // 부모 컨테이너 리턴
Container getTopLevelAncestor() // 최상위 부모 컨테이너 리턴
package experience;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.temporal.JulianFields;
public class JComponentEx extends JFrame {
public JComponentEx(){
super("JComponent의 공통 메소드 예제");
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton b1 = new JButton("Magenta/Yellow Button");
JButton b2 = new JButton("Disabled Button");
JButton b3 = new JButton("getX(), getY()");
b1.setBackground(Color.YELLOW);
b1.setForeground(Color.MAGENTA);
b1.setFont(new Font("Arial", Font.ITALIC, 20)); // Arial 폰트,20픽셀
b2.setEnabled(false); //버튼 비활성화
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource(); //가져온 소스를 버튼형으로 바꿔줌. change
JComponentEx frame = (JComponentEx) b.getTopLevelAncestor();
frame.setTitle(b.getX()+","+b.getY()); //타이틀에 버튼 좌표 출력
}
});
c.add(b1);
c.add(b2);
c.add(b3);
setSize(260,200);
setVisible(true);
}
public static void main(String[] args) {
new JComponentEx();
}
}
JLabel() //빈레이블
JLabel(Icon image) // 이미지 레이블
JLabel(String text) // 문자열 레이블
JLabel(String text, Icon image, int hAlign) // 문자열과 이미지를 모두 가진 레이블
- hAlign : 수평 정렬 값으로 SwingConstants.LEFT,
SwingConstants.RiGHT, SwingConstants.CENTER 중 하나
JLabel textLabel = new JLable("사랑합니다");
imageIcon image = new ImageIcon("images/sunset.jpg");
JLabel imageLabel = newJLabel(image);
ImageIcon image = new ImageIcon("images/sunset.jpg");
JLabel label = new JLabel("사랑합니다", image, SwingConstants.CENTER);
package experience;
import javax.swing.*;
import java.awt.*;
public class JLabelEx2 extends JFrame {
public JLabelEx2(){
setTitle("레이블 예쩨");
setSize(600,300);
setVisible(true);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel textLabel = new JLabel("사랑합니다."); // 맨위에 나타나는 표현
ImageIcon beauty = new ImageIcon("images/beauty.jpg"); //이미지 경로 설정 및 중간에 그림 삽입
JLabel imageLabel = new JLabel(beauty);
ImageIcon normalIcon = new ImageIcon("images/normalIcon.gif"); //이미지 경로 설정 미 하단에 그림삽입
JLabel label = new JLabel("보고싶으면 전화하세요", normalIcon, SwingConstants.CENTER); // 하단 중간정렬
c.add(textLabel);
c.add(imageLabel);
c.add(label);
}
public static void main(String[] args) {
new JLabelEx2();
}
}
JButton() // 빈버튼
JButton(Icon image) //이미지 버튼
JButton(String text) //문자열 버튼
JButton(String text, Icon image) // 문자열과 이미지를 가진 버튼
JButton btn = new JButton("hello");
package experience;
import javax.swing.*;
import java.awt.*;
public class ButtonEx extends JFrame {
public ButtonEx() {
setTitle("이미지 버튼 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
ImageIcon normalIcon = new ImageIcon("image/~~.gif"); //이미지경로
ImageIcon rolloverIcon = new ImageIcon("image/~~.gif"); //이미지경로
ImageIcon pressedIcon = new ImageIcon("image/~~.gif"); //이미지경로
JButton btn = new JButton("call~~", normalIcon);
btn.setPressedIcon(pressedIcon);
btn.setRolloverIcon(rolloverIcon);
c.add(btn);
setSize(250, 150);
setVisible(true);
}
public static void main(String[] args) {
new ButtonEx();
}
}
void setHorizontalAlignment (int align)
align
: 정렬의 기준을 지정하는 값으로 다음과 같다.SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT
void setVerticalAligment(int align)
SwingConstants.TOP, SwingConstants.CENTER, SwingConstants.BoTTOM