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);
}
}
...全文
280 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
小A阿瑾 2020-02-03
  • 打赏
  • 举报
回复
引用 2 楼 花谢尊前不敢香 的回复:
父容器不要布局了,要给面板设置setBounds啊,确定位置,大小
已解决了
小A阿瑾 2020-02-03
  • 打赏
  • 举报
回复
引用 1 楼 三仙半 的回复:
用Eclipse做吗?如果是的话,建议安装一个WindowBuilder插件,可视化的编辑界面,否则太痛苦了。
好的谢谢
  • 打赏
  • 举报
回复
父容器不要布局了,要给面板设置setBounds啊,确定位置,大小
三仙半 2020-02-03
  • 打赏
  • 举报
回复
用Eclipse做吗?如果是的话,建议安装一个WindowBuilder插件,可视化的编辑界面,否则太痛苦了。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧