JButton wont change size or position on JPanel

69
January 28, 2021, at 1:40 PM

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);
    }
}
Answer 1

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.

READ ALSO
Python 2.7 fnmatch NOT editing text

Python 2.7 fnmatch NOT editing text

I have a file with 600k+ records of string labels I am trying to edit with an update cursor using both the string modules and fnmatch to find patterns to editThe section using fnmatch is successfully printing the matched records but not changing / parsing...

80
How to build a known apk for play store in flutter?

How to build a known apk for play store in flutter?

i made app and build it as an apk file in flutter and i tried to install it in my android phone but while installing it, play store said: the app is unkhown do you wanna install, and then i installed the app and app was working well, the question is how i can solve it, Do i have to buy a play store account?...

79
Unable to start Hive metastore related to java exception

Unable to start Hive metastore related to java exception

I installed Apache hive 3, Apache Hadoop 3, Trino and Mysql (mysql Ver 1414 Distrib 5

106