图形界面简单使用(JFrame)
JFrame
窗体:界面的骨架,以下是搭建骨架的基本步骤和方法。
// `设置窗口的标题`
this.setTitle("xxx");
// 设置图标
this.setIconImage(getImageByPath("/img/tb.png"));
// 用于设置当前窗口的宽和高
this.setSize(584, 698);
// 设置窗口菜单上面的X的功能
// -DO_NOTHING_ON_CLOSE(什么也不做),
// -HIDE_ON_CLOSE (隐藏当前窗口),
// -DISPOSE_ON_CLOSE (隐藏当前窗口,并释放窗体占有的其他资源),
// -EXIT_ON_CLOSE (结束窗口所在的应用程序)。
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置当前窗口的背景颜色
this.setBackground(Color.yellow);
//自动以颜色
Color color = new Color(233, 233, 233)
// 让当前窗口居中显示
this.setLocationRelativeTo(null);
// 设置当前打开的窗口是否可以改变大小
this.setResizable(false);
//添加控件到当前窗口
this.add(插件);
// 设置是否显示当前窗口:true 显示, false 不显示
this.setVisible(true);
JPanel
面板:容器,内部可以摆放各种控件
常用方法
paint(Graphics g); //绘制方法:Graphics画笔对象
g.setColor(Color.red); //设置画笔颜色
g.drawImage(img,x,y,width,hight,observer); //绘制图片,observer一般传入this
//重新绘制画面
repaint();
JPanel中还有许多方法,如果有需要可以查阅api文档。欢迎大家帮忙补充,谢谢了!