JAVA 按钮改变大小问题
小A阿瑾 2020-02-01 11:20:40 本人用java写一个小界面,现在有一个面板 p1用来存放两个按钮
然后主框架要放三个按钮,并且要改变它的位置和大小使得看起来美观,但现在一使用setBounds方法 父容器就要定义为null,
但一旦定义为null 我的之前的面板p1 就会被覆盖掉 请问怎么办?》??
以下是代码:
package vocation1;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class MainChessFrame extends JFrame {
//创造五个按钮
///private JButton jbtStart = new JButton("开始游戏");
//private JButton jbtIntroduce= new JButton("游戏说明");
//private JButton jbtExit = new JButton("退出游戏");
private JButton jbtGame = new JButton("游戏");
private JButton jbtSetting = new JButton("设置");
//菜单栏面板
private static JPanel p1 = new JPanel();
//主面板显示按钮
//private JPanel p2 = new JPanel();
//显示字符串面板 包括:五子棋 作者的名字
//构造方法
public MainChessFrame() {
//将菜单面板按钮用FlowLayout布局
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
//将 游戏按钮和 设置按钮放到菜单面板上
p1.add(jbtGame);
p1.add(jbtSetting);
jbtGame.setBackground(Color.WHITE);
jbtSetting.setBackground(Color.WHITE);//将按钮的背景色 设置为白色
//将菜单面板放到上边(北面)
add(p1,BorderLayout.NORTH);
//将菜单面板添加到框架上
add(p1);
p1.setBackground(Color.WHITE);//将背景色设置为白色
//构造一种新字体: 宋体
//Font f1 = new Font("宋体",Font.BOLD,50);
//以下是p2
// p2.setLayout(new GridLayout(5,1,10,10));
// p2.add(p1,BorderLayout.NORTH);//将菜单面板p1 放到主面板p2的上边
//p2.setBackground(Color.white);//将背景色设置为白色
//p2.add(jbtStart);
//p2.add(jbtIntroduce);
//p2.add(jbtExit);
//将三个按钮的背景色设置为白色
//jbtStart.setBackground(Color.white);
//jbtIntroduce.setBackground(Color.white);
//jbtExit.setBackground(Color.white);
//将三个按钮用新字体
//jbtStart.setFont(f1);
//jbtIntroduce.setFont(f1);
//jbtExit.setFont(f1);
//将 p2 添加到框架上
//add(p2);
//以下是p3
}
public static void main(String[] args) {
MainChessFrame frame = new MainChessFrame();
frame.setTitle("小阿瑾的五子棋");
frame.setSize(800,600);
frame.setLayout(null); 就这个地方!!!!
frame.add(p1,BorderLayout.NORTH);
JButton jbtStart = new JButton("开始游戏");
JButton jbtIntroduce= new JButton("游戏说明");
JButton jbtExit = new JButton("退出游戏");
//设置按钮尺寸
jbtStart.setBounds(400, 400, 100, 100);
frame.add(jbtStart);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.WHITE);
frame.setVisible(true);
}
}