java如何使按钮自动填充满整个JPanel?

如题所述

最开始用的就是Borderlayout布局,要加到面板的Center位置,而且你还要确定,你的这个JPanel 有多大说不定,那个JTextArea已经填满了这个JPanel,但是这个JPanel却不是你想象中那么大。

设置为1x1的格子,就充满了:setLayout(new GridLayout(1,1));

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。

Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

温馨提示:内容为网友见解,仅供参考
第1个回答  2018-01-31
设置Jpanel的布局管理器为gridLayout(1,1);
例如:
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.*;

public class TestJpanel extends JFrame {
public static void main(String [] arge){
new TestJpanel();
}

public TestJpanel(){
setBounds(10,10,400,400);
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
panel.setLayout(new GridLayout(1, 1));
JButton button = new JButton();
panel.add(button);
this.add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

}
第2个回答  2018-04-04

设置Jpanel的布局管理器为gridLayout(1,1);

这个例子是网上找到的,以为我不是专门学编程的哈。

例如:
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.*;

public class TestJpanel extends JFrame {
public static void main(String [] arge){
new TestJpanel();
}

public TestJpanel()

{

setBounds(10,10,400,400);

JPanel panel = new JPanel();

panel.setBackground(Color.RED);

panel.setLayout(new GridLayout(1, 1));

JButton button = new JButton();

panel.add(button);

this.add(panel);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);
}

}

相似回答