JAVA 按钮改变大小问题
本人用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);
}
}
然后主框架要放三个按钮,并且要改变它的位置和大小使得看起来美观,但现在一使用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);
}
}
...全文
相关推荐
java写的记事本程序带工具栏可以改变字体颜色
在这个记事本程序中,工具栏按钮的点击事件会触发相应的功能,比如改变字体颜色,可能通过调用JTextPane的setCharacterAttributes()方法来实现。
radio-actions:通过单击单选按钮将文本更改为不同的大小写
Radio Actions"是一个关于如何通过单击单选按钮来改变文本显示大小写的特性,这通常涉及到前端开发,尤其是JavaScript或相关的库,如jQuery。
改变按钮图片
比如,处理图片大小、分辨率和色彩模式的兼容性问题,以及在不同屏幕尺寸上的适配。9. 性能优化:在处理大量图片或者内存有限的设备上,要考虑到内存管理,避免内存泄漏。
fckeditor文本编辑 实现了上传大小控制 中文名问题 格式控制等
用户可以通过选择不同的格式按钮来改变输入内容的样式。开发者可以通过自定义工具栏按钮或修改`fckeditor.js`来添加或删除格式选项。
Java继承JPanel实现自适应布局的面板
这使得开发者可以创建更加灵活和响应式的用户界面,而无需手动处理窗口大小改变时的组件布局问题。


Java SE
