티스토리 뷰
728x90
숫자 만들기 게임 GUI를 구현하는 걸 배우고, 더 공부가 필요할 것 같아 정리해 봤다.
영어가 더 편해서 영어로 정리를 하겠다.
Java Swing class Hierarchy Diagram
Swing
- Swing in Java is a GUI toolkit that includes the GUI components.
- Swing provides a rich set of widgets and packages to make sophisticated GUI components for Java applications.
- Swing is a part of Java Foundation Classes(JFC), which is an API for Java GUI programing that provide GUI.
- The Java Swing library is built on top of the Java Abstract Widget Toolkit (AWT).
Container Class
- Container classes that can have other components on it.
- So for creating a Java Swing GUI, we need at least one container object.
- 3 types of Java Swing containers.
- Panel
- It's a pure container and is not a window in itself. The sole purpose if a Panel is to organize the components on to a window.
- Frame
- It's a fully finctioning window with its title and icons.
- Dialog
- It can be thought of like a pop-ip window that pops out when a message has to be displayed. It's not a fully functioning window like the Frame.
- Panel
GUI (Graphical User Interface)
- GUI in Java is an easy-to-use visual experience builder for Java applications.
- It's mainly made of graphical components like buttons, lagels, windows, etc. through which the user can interact with an applicaton.
- GUI plays an important role to buld easy interfaces for Java applications.
GUI 직접 구현해보기
📌 JFrame
public class Main {
public static void main(String[] args) {
//JFrame = a GUI window add components to
JFrame frame = new JFrame(); //creates a frame
frame.setTitle("JFrame title goes here"); //sets title of frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit out of application
frame.setResizable(false); //prevent frame frame being resized
frame.setSize(420,420); //sets the x-dimension, and y-dimension of frame
frame.setVisible(true); //make frame visible
ImageIcon Image = new ImageIcon("logo.png"); // create an ImageIcon
frame.setIconImage(Image.getImage()); // change icon of frame
frame.getContentPane().setBackground(new Color(0,0,0)); // change color of the backgound
}
}
📌JLable
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
public class Label {
public static void main(String[] args) {
//JLabel = a GUI display area for a string of text, an image, or both
ImageIcon image = new ImageIcon("icon.png");
Border border = BorderFactory.createLineBorder(Color.green, 3);
JLabel label = new JLabel(); // create a label
label.setText("Bro, do you even code?"); // set text of label
label.setIcon(image);
label.setHorizontalTextPosition(JLabel.CENTER); //set text Left,Center,Right of imageicon
label.setVerticalTextPosition(JLabel.TOP); //set text Top,Center,Bottom of imageicon
label.setForeground(new Color(0x00ff00)); // set font color of text
label.setFont(new Font("MV Boli", Font.PLAIN, 20)); //set font of text
label.setIconTextGap(-25); //set gap of text to image
label.setBackground(Color.white); //set background color
label.setOpaque(true); //display background color
label.setBorder(border);
label.setVerticalAlignment(JLabel.CENTER); // set vertical position of icontext within label
label.setHorizontalAlignment(JLabel.CENTER);
label.setBounds(100,100,250,250); // set x,y position within frame as well as dimensions
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setSize(500,500);
// frame.setLayout(null);
frame.setVisible(true);
frame.add(label);
frame.pack();
}
📌 JPanel
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main2 {
// JPanel = a GUI component that functions as a container t hold other components
public static void main(String[] args) {
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
redPanel.setBounds(0, 0, 250, 250);
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.blue);
bluePanel.setBounds(250, 0, 250, 250);
JPanel greenPanel = new JPanel();
greenPanel.setBackground(Color.green);
greenPanel.setBounds(0, 250, 500, 250);
greenPanel.setLayout(new BorderLayout());
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setSize(750,750);
frame.setVisible(true);
frame.add(redPanel);
frame.add(bluePanel);
frame.add(greenPanel);
}
}
📌 JButton
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyFrame extends JFrame implements ActionListener {
JButton button; // Declare global variable
MyFrame() {
button = new JButton();
button.setBounds(200, 100, 250, 100);
button.addActionListener(this);
button.setText("I'm a button");
button.setFocusable(false);
button.setHorizontalTextPosition(JButton.CENTER);
button.setVerticalTextPosition(JButton.BOTTOM);
button.setFont(new Font("Comic Sans", Font.BOLD,25));
button.setIconTextGap(-15);
button.setForeground(Color.cyan);
button.setBackground(Color.lightGray);
button.setBorder(BorderFactory.createEtchedBorder());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(500, 500);
this.setVisible(true);
this.add(button);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button) {
System.out.println("poo");
}
}
}
'Programming Language > JAVA' 카테고리의 다른 글
[Java] Exercise: Simple Calculator Program (자바로 간단한 계산기 구현하기) (0) | 2023.10.04 |
---|---|
[Java] next(), nextLine() (0) | 2023.10.03 |
[Java] 랜덤게임 만들기 (0) | 2023.10.01 |
[Java] 배열(Array) & 배열 리스트 (ArrayList) (0) | 2023.09.30 |
[Java] Get, Set (0) | 2023.09.27 |