Component를 가지고 구성하는 도화지.
GUI 레이아웃을 구성할 때 필수적
JFrame : main 창. 이 아래 panel(⊃ component) 배치JDialog : 팝업창. 경고, 알림용 JApplet : 보안 이슈로 잘 사용하지 않음JPanel : component끼리 모아두는 곳
java.swing.JComponent 클래스를 상속 받음
JButton : 클릭될 수 있는 버튼JLabel : 텍스트나 이미지를 보여줌 (이후 수정 불가)JTextField : 한 줄짜리 유저가 입력 가능한 입력 공간JPasswordField : 입력한 값이 보이지 않는 입력 공간import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class BasicJTreeExample extends JFrame {
public BasicJTreeExample() {
setTitle("Basic JTree Example");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a root node
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
// Ad first level child nodes
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
// Add second level child nodes to child 1
DefaultMutableTreeNode grandChild1 = new DefaultMutableTreeNode("grandChild 1");
DefaultMutableTreeNode grandChild2 = new DefaultMutableTreeNode("grandChild 2");
child1.add(grandChild1);
child1.add(grandChild2);
// Construct the tree structure
root.add(child1);
root.add(child2);
// Create a JTree
JTree tree = new JTree(root);
// Add the tree to the frame
JScrollPane TreeView = new JScrollPane(tree);
add(TreeView);
setVisible(true);
}
public static void main(String[] args) {
new BasicJTreeExample();
}
}

JScrollPane 에서만 사용 가능JTree ⊂ JScrollPane ⊂ JPanel 가능import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class BasicJTableExample extends JFrame {
public BasicJTableExample() {
setTitle("Basic JTable Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Define column names for the table
String[] columnNames = {"ID", "Name", "Age", "Occupation"};
// Define data for the table (2D array)
Object[][] data = {
{1, "Alice", 30, "Engineer"},
{2, "Bob", 25, "Designer"},
{3, "Charlie", 35, "Teacher"},
{4, "Dave", 28, "Developer"}
};
// Create table model with the specified data and column names
DefaultTableModel model = new DefaultTableModel(data, columnNames);
// Create JTable with the model
JTable table = new JTable(model);
// Add the table to a scroll pane
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
setVisible(true);
}
public static void main(String[] args) {
new BasicJTableExample();
}
}

JTree와 마찬가지로 JScrollPane에서만 사용 가능JTappedPane 안에 Panel 넣기import javax.swing.*;
public class BasicJTabbedPaneExample extends JFrame {
public BasicJTabbedPaneExample() {
setTitle("Basic JTabbedPane Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create JTabbedPane
JTabbedPane tabbedPane = new JTabbedPane();
// Create panels for each tab
JPanel panel1 = new JPanel();
panel1.add(new JLabel("This is the first tab."));
JPanel panel2 = new JPanel();
panel2.add(new JLabel("This is the second tab."));
JPanel panel3 = new JPanel();
panel3.add(new JLabel("This is the third tab."));
// Add panels to JTabbedPane with titles
tabbedPane.addTab("Tab 1", panel1);
tabbedPane.addTab("Tab 2", panel2);
tabbedPane.addTab("Tab 3", panel3);
// Add the tabbed pane to the frame
add(tabbedPane);
setVisible(true);
}
public static void main(String[] args) {
new BasicJTabbedPaneExample();
}
}

JTappedPane 안에 어떻게 나눌건지 넣고, 각각 위치할 Panel 넣기 import javax.swing.*;
public class BasicJSplitPaneExample extends JFrame {
public BasicJSplitPaneExample() {
setTitle("Basic JSplitPane Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create two panels to split
JPanel leftPanel = new JPanel();
leftPanel.add(new JLabel("This is the left panel."));
JPanel rightPanel = new JPanel();
rightPanel.add(new JLabel("This is the right panel."));
// Create JSplitPane with the two panels
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
splitPane.setDividerLocation(150); // Initial divider position
// Add the split pane to the frame
add(splitPane);
setVisible(true);
}
public static void main(String[] args) {
new BasicJSplitPaneExample();
}
}

JSlider : 유저가 슬라이더로 값을 조절할 수 있게 함 (ex. 볼륨 조절)slider = new JSlider(최소, 최대, 초기값);JSpinner : 유저가 여러 값 중 선택할 수 있도록 함spinner = new JSpinner(new SpinnerNumberModel(초기값, 최소, 최대, 증가량));JProgressBar : 작업률을 보여줌progressBar = new JProgressBar(초기값, 최대);
기본적인 레이아웃들
component들을 알아서 크기 조절 & 배치해줌
FlowLayout

BorderLayout
JFrame frame = new JFrame("BorderLayout");
frame.setLayout(new BorderLayout());
frame.add(new JButton("North"), BorderLayout.NORTH);
frame.add(new JButton("South"), BorderLayout.SOUTH);
frame.add(new JButton("West"), BorderLayout.WEST);
frame.add(new JButton("East"), BorderLayout.EAST);
frame.add(new JButton("Center"), BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

GridLayout
frame.setLayout(new GridLayout(2, 2)); // 2 * 2 행렬
GUI component 들로 유저 상호작용을 관리하는 과정
ActionListener : 버튼 클릭 같은 액선 이벤트를 들음MouseListener : 마우스 클릭이나 움직임 같은 마우스 이벤트를 들음KeyListener : 키 입력이나 떼기 같은 키보드 이벤트를 들음Event Source : 이벤트를 만드는 Component
Event Listener
Event Object : 이벤트의 타입, 소스에 대한 캡슐화 된 정보
Event Source가 이벤트를 생성하면 Event Listener 가 그것을 듣고 Event Object를 생성함.
