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.
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