简体   繁体   中英

JButton wont change size or position on JPanel

I added a button into JPanel and tried to change the size and the position of the button. I've tried different lines of codes but they wont work. Also putting in parent.setLayout(null); or panel.setLayout(null); will just completely remove the button and the background

Here is the code:

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

public class app {

    public static void main(final String[] args) {
        final JFrame parent = new JFrame("CPS TEST");
        JButton button = new JButton("Button");
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.add(button);
        panel.setBackground(Color.DARK_GRAY);
        parent.add(panel, BorderLayout.CENTER);
        parent.setSize(500, 300);
        parent.setBackground(Color.CYAN);
        parent.setLocationRelativeTo(null);
        parent.setVisible(true);
        parent.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Here are three ways to increase the size of a button:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Changing the position of a component is worthy of a separate question, but changing the size of a button is easy.

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