Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

java - 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);
    }
}
question from:https://stackoverflow.com/questions/65925774/jbutton-wont-change-size-or-position-on-jpanel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here

enter image description here

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...