java如何使按钮自动填充满整个JPanel,一个JPanel里面就一个按钮。
2个回答
展开全部
设置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);
}
}
例如:
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询