본문 바로가기
프로그래밍

자바 swing에서 버튼 클래스의 스타일 변경

by 아이티비즈 2023. 4. 27.

대학생 때 자바에서 프로그래밍을 하다 보면 javaFX처럼 외부에서 가져오는 것이 아니라, JDK자체에 포함된 GUI( AWT, SWING)를 사용하여 프로젝트를 하는 경우가 더 많이 있었던 것 같습니다.

Swing에는 다양한 컨포넌트들이 존재하고 각각의 컴포넌트들에는 기능 구현을 할 수 있도록 메서드들이 존재합니다.

 

이번 포스팅에서는 Button 클래스에서 제공되는 메소드들을 활용하여 스타일을 변경하는 방법에 대해 포스팅하도록 하겠습니다.

1. setText(String text) 버튼의 텍스트를 설정합니다.

예제:

JButton button = new JButton();
button.setText("Click Me");

 

2. setForeground(Color color) 버튼의 텍스트 색상을 설정합니다.

예제:

JButton button = new JButton();
button.setForeground(Color.RED);

 

3. setBackground(Color color) 버튼의 배경 색상을 설정합니다.

예제:

JButton button = new JButton();
button.setBackground(Color.BLUE);

 

4. setFont(Font font) 버튼의 글꼴을 설정합니다.

예제:

JButton button = new JButton();
button.setFont(new Font("Arial", Font.BOLD, 20));

 

5. setPreferredSize(Dimension dimension) 버튼의 크기를 설정합니다.

예제:

JButton button = new JButton();
button.setPreferredSize(new Dimension(100, 50));

 

6. setBorder(Border border) 버튼의 테두리를 설정합니다.

예제:

JButton button = new JButton();
button.setBorder(BorderFactory.createLineBorder(Color.BLACK));

 

7. setEnabled(boolean enabled) 버튼의 활성화/비활성화 상태를 설정합니다.

예제:

JButton button = new JButton();
button.setEnabled(false);

 

8. setOpaque(boolean isOpaque) 버튼의 배경색이 불투명한지 여부를 설정합니다.

예제:

JButton button = new JButton();
button.setOpaque(true);

 

여기까지 버튼(Button) 클래스에서 사용되는 메소드 들에 대해 알아보았습니다. 그렇다면 실제 코드로 작성해 보면 어떻게 보일지 작성해 보겠습니다.

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

public class ButtonStyleExample extends JFrame {
    public ButtonStyleExample() {
        // 프레임 설정
        setTitle("Button Style Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 200);
        setLocationRelativeTo(null);
        
        // 버튼 생성
        JButton button = new JButton();
        button.setText("Click Me");
        button.setForeground(Color.RED);
        button.setBackground(Color.BLUE);
        button.setFont(new Font("Arial", Font.BOLD, 20));
        button.setPreferredSize(new Dimension(100, 50));
        button.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        button.setEnabled(false);
        button.setOpaque(true);
        
        // 프레임에 버튼 추가
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(button);
        
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new ButtonStyleExample();
    }
}

위의 코드를 실행해 보면 아래와 같은 화면을 볼 수 있습니다.

Button class excute screen
Button class excute screen

 

위의 코드는 자바 Swing을 사용하여 버튼을 생성하고, 버튼의 스타일을 변경하는 예제 코드입니다. 이 코드의 실행 흐름은 다음과 같습니다.

1. ButtonStyleExample 클래스 생성자 호출

프레임의 제목을 "Button Style Example"로 설정합니다.

프레임이 닫힐 때 프로그램을 종료합니다.

프레임의 크기를 400x200으로 설정합니다.

프레임을 화면 중앙에 위치시킵니다.

 

2. JButton 객체 생성 버튼 객체를 생성합니다.

버튼의 텍스트를 "Click Me"로 설정합니다.

버튼의 텍스트 색상을 빨간색으로 설정합니다.

버튼의 배경 색상을 파란색으로 설정합니다.

버튼의 글꼴을 Arial, 굵은 체, 20pt로 설정합니다.

버튼의 크기를 100x50으로 설정합니다.

버튼의 테두리를 검은색으로 설정합니다.

버튼을 비활성화 상태로 설정합니다.

버튼의 배경색이 불투명하게 설정합니다.

 

3. 프레임에 버튼 추가

프레임의 레이아웃을 FlowLayout으로 설정합니다.

 

4. 버튼을 프레임에 추가합니다.

프레임을 화면에 표시 프레임을 화면에 표시합니다.

 

5. main() 메서드 호출

ButtonStyleExample 클래스의 객체를 생성하여 프로그램을 실행합니다.

 

자바 swing에서 버튼( Button ) 클래스( Class )의 스타일 변경
자바 swing에서 버튼( Button ) 클래스( Class )의 스타일 변경

댓글