Ruang penulis
← Kembali ke jurnal
Swing

How do I arrange the swing component using BoxLayout?

This example demonstrate how we can arrange component in row or column order using the BoxLayout layout manager. Instead of use the BoxLayout manager we can also use the Box component as our content pane to get the same effect as using the BoxLayout manager. package org.kodejava.swing; import javax.swing.*; public class BoxLayoutDemo extends JFrame { […]

DWayan · 10 Jan 2009 · 1 menit baca

Artikel oleh Wayan · Sumber: Kode Java ↗

Terjemahan artikel ini belum tersedia. Isi asli ditampilkan.

This example demonstrate how we can arrange component in row or column order using the BoxLayout layout manager. Instead of use the BoxLayout manager we can also use the Box component as our content pane to get the same effect as using the BoxLayout manager.

package org.kodejava.swing;

import javax.swing.*;

public class BoxLayoutDemo extends JFrame {
    public BoxLayoutDemo() {
        initialize();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new BoxLayoutDemo().setVisible(true));
    }

    private void initialize() {
        setSize(400, 400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // Set the layout of the Content Pane to BoxLayout using BoxLayout.X_AXIS
        // will arrange the component left to right. We can use the BoxLayout.Y_AXIS
        // to arrange the component top to bottom.
        setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));

        JLabel label = new JLabel("Username : ");
        JTextField textField = new JTextField();

        JLabel password = new JLabel("Password :");
        JPasswordField passwordField = new JPasswordField();

        getContentPane().add(label);
        getContentPane().add(textField);

        getContentPane().add(password);
        getContentPane().add(passwordField);
    }
}
← Jelajahi catatan lainnya