简体   繁体   中英

how can i center Label in frame Swing?

I want to make the copyright in the image below in the center of the frame. I tried to add SwingConstants.CENTER and Component.CENTER_ALIGNMENT but neither worked. Can someone please help me with this one? Thanks in advance.

enter image description here

This is my code:

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

public class MyFrame extends JFrame {
    JTextField text1;
    JTextField text2;
    JLabel label1;
    JLabel label2;
    JButton encrypt;
    JButton decrypt;
    JLabel copyright;
    //constructor
    public MyFrame(){
        super("Encryption and Decryption");
        setLayout(new FlowLayout());        //add first text field
        label1 = new JLabel("enter a text:  ");
        add(label1);
        text1= new JTextField(10);
        add(text1);

        //add second text field
        label2 = new JLabel("result:  ");
        add(label2);
        text2= new JTextField(10);
        add(text2);
        encrypt = new JButton("encrypt!");
        add(encrypt);
        decrypt = new JButton("decrypt!");
        add(decrypt);
        copyright = new JLabel(" © Project realized by Ayoub Touti ");
        add(copyright);

        //organizing elements in a panel
        JPanel panel = new JPanel(new GridLayout(4,2,12,6));
        panel.add(label1);
        panel.add(text1);
        panel.add(label2);
        panel.add(text2);
        panel.add(encrypt);
        panel.add(decrypt);
        panel.add(copyright);
        add(panel);
    }
}

//////////////////////////////

import javax.swing.*;
public class main {
    public static void main(String[] args) {
        MyFrame frame = new MyFrame();
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setSize (500, 200);
        frame.setResizable(true);
        frame.setVisible (true);
        frame.setLocationRelativeTo(null); // to open the window in the main screen
    }
}

Use a GridBagLayout (or a combination of layouts through compound components, slightly demonstrated below)

在此处输入图像描述

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBorder(new EmptyBorder(32, 32, 32, 32));
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = gbc.LINE_END;
            gbc.insets = new Insets(0, 8, 0, 8);

            add(new JLabel("enter a text:"), gbc);
            gbc.gridy++;
            add(new JLabel("result:"), gbc);

            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = gbc.LINE_START;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);

            JPanel actionPane = new JPanel(new GridBagLayout());
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.fill = gbc.HORIZONTAL;
            gbc.insets = new Insets(0, 0, 0, 0);

            actionPane.add(new JButton("encyprt!"), gbc);
            gbc.gridx++;
            actionPane.add(new JButton("decyprt!"), gbc);

            gbc = new GridBagConstraints();
            gbc.gridwidth = gbc.REMAINDER;
            gbc.gridy = 2;

            add(actionPane, gbc);
            gbc.gridy++;
            add(new JLabel("© Project realized by Ayoub Touti"), gbc);
        }
    }
}

See How to Use GridBagLayout for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM