Java GUI (Graphical User Interface)

6 minute read

X. Java GUI (Graphical User Interface):

A. Swing components (JFrame, JPanel, JButton, JTextField, JTable):

Trong Java, Java Swing là một bộ công cụ được sử dụng để phát triển giao diện người dùng đồ họa (GUI). Dưới đây là một số thành phần Swing cơ bản:

  1. JFrame:

    • JFrame là cửa sổ chính của một ứng dụng Swing.
    • Nó chứa tất cả các thành phần khác như JButton, JPanel, v.v.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      
      import javax.swing.*;
      
      public class MyFrame extends JFrame {
          public MyFrame() {
              setTitle("My Frame");
              setSize(400, 300);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
          }
      
          public static void main(String[] args) {
              new MyFrame();
          }
      }
      
  2. JPanel:

    • JPanel là một vùng cô lập trong cửa sổ, được sử dụng để chứa các thành phần khác như JButton, JTextField, v.v.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      
      import javax.swing.*;
      
      public class MyPanel extends JPanel {
          public MyPanel() {
              JButton button = new JButton("Click Me");
              add(button);
          }
      
          public static void main(String[] args) {
              JFrame frame = new JFrame("My Panel");
              frame.setSize(400, 300);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.add(new MyPanel());
              frame.setVisible(true);
          }
      }
      
  3. JButton:

    • JButton là một nút có thể được nhấp để thực hiện một hành động.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      
      import javax.swing.*;
      
      public class MyButtonFrame extends JFrame {
          public MyButtonFrame() {
              setTitle("Button Example");
              setSize(400, 300);
      
              JButton button = new JButton("Click Me");
              add(button);
      
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
          }
      
          public static void main(String[] args) {
              new MyButtonFrame();
          }
      }
      
  4. JTextField:

    • JTextField là một ô văn bản cho phép người dùng nhập dữ liệu.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      
      import javax.swing.*;
      
      public class MyTextFieldFrame extends JFrame {
          public MyTextFieldFrame() {
              setTitle("Text Field Example");
              setSize(400, 300);
      
              JTextField textField = new JTextField(20); // 20 là độ rộng của ô văn bản
              add(textField);
      
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
          }
      
          public static void main(String[] args) {
              new MyTextFieldFrame();
          }
      }
      
  5. JTable:

    • JTable là một thành phần dùng để hiển thị dữ liệu dưới dạng bảng.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      
      import javax.swing.*;
      import javax.swing.table.*;
      
      public class MyTableFrame extends JFrame {
          public MyTableFrame() {
              setTitle("Table Example");
              setSize(400, 300);
      
              String[][] data = {{"1", "John", "Doe"}, {"2", "Jane", "Smith"}};
              String[] columnNames = {"ID", "First Name", "Last Name"};
      
              JTable table = new JTable(data, columnNames);
              JScrollPane scrollPane = new JScrollPane(table);
              add(scrollPane);
      
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
          }
      
          public static void main(String[] args) {
              new MyTableFrame();
          }
      }
      

Những thành phần Swing cơ bản như JFrame, JPanel, JButton, JTextField, và JTable giúp bạn xây dựng các giao diện người dùng đồ họa (GUI) trong Java một cách dễ dàng và linh hoạt.

B. Xử lý sự kiện với Swing:

Trong Java Swing, để xử lý sự kiện, bạn có thể sử dụng các bộ xử lý sự kiện như ActionListener, MouseListener, KeyListener, WindowListener, v.v. Dưới đây là cách sử dụng các bộ xử lý sự kiện này:

  1. Xử lý sự kiện với ActionListener:

    • ActionListener được sử dụng để xử lý sự kiện khi người dùng thực hiện một hành động trên một thành phần như JButton hoặc JMenuItem.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      
      import javax.swing.*;
      import java.awt.event.*;
      
      public class ActionListenerExample {
          public static void main(String[] args) {
              JFrame frame = new JFrame("ActionListener Example");
              JButton button = new JButton("Click Me");
      
              button.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      JOptionPane.showMessageDialog(frame, "Button Clicked!");
                  }
              });
      
              frame.getContentPane().add(button);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(300, 200);
              frame.setVisible(true);
          }
      }
      
  2. Xử lý sự kiện với MouseListener:

    • MouseListener được sử dụng để xử lý sự kiện chuột như nhấp chuột, kéo chuột, v.v.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      
      import javax.swing.*;
      import java.awt.event.*;
      
      public class MouseListenerExample {
          public static void main(String[] args) {
              JFrame frame = new JFrame("MouseListener Example");
              JButton button = new JButton("Click Me");
      
              button.addMouseListener(new MouseListener() {
                  public void mouseClicked(MouseEvent e) {
                      System.out.println("Mouse Clicked");
                  }
      
                  public void mousePressed(MouseEvent e) {}
                  public void mouseReleased(MouseEvent e) {}
                  public void mouseEntered(MouseEvent e) {}
                  public void mouseExited(MouseEvent e) {}
              });
      
              frame.getContentPane().add(button);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(300, 200);
              frame.setVisible(true);
          }
      }
      
  3. Xử lý sự kiện với KeyListener:

    • KeyListener được sử dụng để xử lý sự kiện từ bàn phím như nhấn phím, nhả phím, v.v.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      
      import javax.swing.*;
      import java.awt.event.*;
      
      public class KeyListenerExample {
          public static void main(String[] args) {
              JFrame frame = new JFrame("KeyListener Example");
              JTextField textField = new JTextField(20);
      
              textField.addKeyListener(new KeyListener() {
                  public void keyPressed(KeyEvent e) {
                      System.out.println("Key Pressed: " + e.getKeyChar());
                  }
      
                  public void keyReleased(KeyEvent e) {}
                  public void keyTyped(KeyEvent e) {}
              });
      
              frame.getContentPane().add(textField);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(300, 200);
              frame.setVisible(true);
          }
      }
      

Trong các ví dụ trên, chúng ta sử dụng các bộ xử lý sự kiện ActionListener, MouseListener, và KeyListener để xử lý sự kiện từ các thành phần Swing như JButton và JTextField. Các bộ xử lý này cung cấp các phương thức để xử lý các hành động tương ứng của người dùng trên giao diện người dùng của bạn.

C. Layout Managers (FlowLayout, BorderLayout, GridLayout):

Trong Java Swing, Layout Managers là các đối tượng được sử dụng để quản lý và tự động sắp xếp vị trí của các thành phần trong một giao diện người dùng. Dưới đây là một số Layout Managers phổ biến:

  1. FlowLayout:

    • FlowLayout sắp xếp các thành phần theo hướng của dòng (trái sang phải) hoặc cột (trên xuống dưới).
    • Mỗi thành phần được đặt vào vị trí tiếp theo theo hướng được xác định.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      
      import javax.swing.*;
      import java.awt.*;
      
      public class FlowLayoutExample extends JFrame {
          public FlowLayoutExample() {
              setTitle("FlowLayout Example");
              setSize(400, 200);
      
              setLayout(new FlowLayout());
      
              add(new JButton("Button 1"));
              add(new JButton("Button 2"));
              add(new JButton("Button 3"));
              add(new JButton("Button 4"));
              add(new JButton("Button 5"));
      
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
          }
      
          public static void main(String[] args) {
              new FlowLayoutExample();
          }
      }
      
  2. BorderLayout:

    • BorderLayout chia giao diện thành năm khu vực: North, South, East, West và Center.
    • Mỗi khu vực chứa một thành phần duy nhất, và các thành phần được căn chỉnh theo cạnh của khu vực.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      
      import javax.swing.*;
      import java.awt.*;
      
      public class BorderLayoutExample extends JFrame {
          public BorderLayoutExample() {
              setTitle("BorderLayout Example");
              setSize(400, 200);
      
              setLayout(new BorderLayout());
      
              add(new JButton("North"), BorderLayout.NORTH);
              add(new JButton("South"), BorderLayout.SOUTH);
              add(new JButton("East"), BorderLayout.EAST);
              add(new JButton("West"), BorderLayout.WEST);
              add(new JButton("Center"), BorderLayout.CENTER);
      
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
          }
      
          public static void main(String[] args) {
              new BorderLayoutExample();
          }
      }
      
  3. GridLayout:

    • GridLayout sắp xếp các thành phần thành một lưới ô vuông với số hàng và cột được xác định trước.
    • Mỗi ô trong lưới chứa một thành phần, và các ô được điền theo thứ tự từ trái sang phải, từ trên xuống dưới.
    • Ví dụ:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      
      import javax.swing.*;
      import java.awt.*;
      
      public class GridLayoutExample extends JFrame {
          public GridLayoutExample() {
              setTitle("GridLayout Example");
              setSize(400, 200);
      
              setLayout(new GridLayout(2, 3));
      
              add(new JButton("Button 1"));
              add(new JButton("Button 2"));
              add(new JButton("Button 3"));
              add(new JButton("Button 4"));
              add(new JButton("Button 5"));
              add(new JButton("Button 6"));
      
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
          }
      
          public static void main(String[] args) {
              new GridLayoutExample();
          }
      }
      

Bằng cách sử dụng Layout Managers, bạn có thể tạo ra các giao diện người dùng linh hoạt và dễ dàng điều chỉnh trong Java Swing.