在GUI中组织其他组件。但无边框,丌能做独立窗口。 以Jpanel为例,给一个曾经的实例
//MyPanel.java
import java.awt.*
import javax.swing.*
import java.util.Random
public class MyPanel extends JPanel {
private Circle circle[]
public MyPanel() {
setBackground(Color.black) // 面板背景色
setPreferredSize(new Dimension(600,450)) // 面板大小
int numCircle=new Random().nextInt(6)+5
//int numCircle=circle.length
circle=new Circle[numCircle]
for(int i=0i<numCirclei++){ // 创建圆的实例
Color color=new Color(new Random().nextInt(256),new Random().nextInt(256),new Random().nextInt(256))
circle[i]=new Circle(new Random().nextInt(50)+10,color,new Random().nextInt(420)+61,new Random().nextInt(320)+61)
}
}
/* 每个JPanel对象都有painComponent方法,绘制添加在容器上的组件, 在
* 组件首次显示或重新显示时被自动调用。
*/
public void paintComponent(Graphics g) {
super.paintComponent(g)
// 调用父类方法, 清空画布,绘制背景色
setFont(new Font ("Times New Roman",Font.BOLD,20)) // 设置字体
g.setColor(Color.pink)
g.drawString("Hello World!",250,200)
for(int i=0i<circle.lengthi++){
circle[i].draw(g)
}
}
}
//Circle.java
import java.awt.*
/**此类定义了绘制圆的基本方法
*@author JF
*@version 1.0
*/
public class Circle {
// 直径和圆外接正方形左上角坐标
private int diameter,x,y
// 指定圆的大小和位置
private Color color // 颜色
public Circle(){
}
/**构造函数,设置圆类的属性
*@param int diameter,Color color,int x,int y
*@return none
*/
Circle(int diameter,Color color,int x,int y ){
this.diameter=diameter
this.color=color
this.x=x
this.y=y
}
/**填充圆类
*@param Graphics
*@return none
*/
public void draw(Graphics g){
g.setColor(color)
g.fillOval(x,y,diameter,diameter)
}
}
//MyCircle.java
import javax.swing.*
public class MyCircle{
public static void main(String[] args){
JFrame f=new JFrame("Circle")
f.setVisible(true)
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
f.getContentPane().add(new MyPanel())
f.pack()
}
}
使用方法:
JPanel可以为添加到窗体中的轻型控件提供通用的容器。默认情况下,面板容器不会向控件添加任何除自身背景之外的颜色,但是,可以轻松地向其添加边框(borders)并根据需要改制样式。
在大多数Java GUI外观体验(look and feel)中,面板容器默认是不透明。不透明的面板容器跟普通的目录窗格功能差别不大,并且可以有效帮助样式改进。
设置图层管理器
和其他容器(container)一样,面板容器panel也使用布局管理器(Layout Manager)对添加到容器中的组件(compnent)进行定位和设置尺寸大小。
默认情况下,面板容器的布局管理器是FlowLayout(流式布局)类的一个实例,这个类对放置在容器中的空间按行进行布局(从左到右诸行排列)。在创建面板容器时,你可以轻松地使用任何其他
布局管理器通过调用setLayout方法或指定一个布局管理器。
如下是一个在创建面板容器时设置图层管理器的例子:
JPanel p=new JPanel(new BorderLayout())
但这种方法不能用在BoxLayout(盒子布局)中,因为BoxLayout构造器需要一个预留(pre-existing)容器。如下所示:
JPanel p=new JPanel()
p.setLayout(new BoxLayout(p,BoxLayout.PAGE_AXIS))
添加控件
向面板容器中添加组件时使用add()方法,而向add()方法中传递的参数决定于该面板容器使用哪个布局管理器。当所用的布局管理器是FlowLayout,BoxLayout,GridLayout,或SpringLayout时,像通常那样向add()方法传递单一的参数即可,如下所示:
aFlowPanel.add(aComponent)
aFlowPanel.add(anotherComponent)
而当布局管理器为BorderLayout时,还需要提供另一个参数来指定添加到容器中控件的位置,例如:
aBorderPanel.add(aComponent,BorderLayout.CENTER)
aBorderPanel.add(anotherComponent,BorderLayout.PAGE_END)
对于GridBagLayout布局管理器,可以使用上述任一方法,但必须以某种方式对每个组件指定GridBagConstraints,例如:
JPanel pane=new JPanel(new GridBagLayout())
GridBagConstraints c=new GridBagConstraints()
警告
Swing不是线程安全的。
此类的序列化对象与以后的Swing版本不兼容。当前序列化支持适用于短期存储,或适用于在运行相同Swing版本的应用程序之间进行RMI(Remote Method Invocation,远程方法调用)。
扩展资料:JPanel一个面板也是一个容器JPanel Pan=new JPanel()
Pan.setLayout(null)//设置为空布局也就是自由布局
Pan.add(控件)//添加控件
JPanel是java图形化界面中最常使用的容器。实例:在窗体中添加一个蓝色的面板
//PanelDemo.java
import javax.swing.*
import java.awt.*//引入AWT包,因为要使用到颜色类
class PanelDemo{
public static void main(String[]args)throws Exception
{JFrame f=new JFrame("第一个Java窗口")
f.setSize(300,200)
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
f.setVisible(true)
f.setResizable(false)
f.setLocationRelativeTo(null)
f.setLayout(null)//设置窗体布局为空布局
JPanel p=new JPanel()//实例化一个面板
//设置面板背景色为蓝色,如果不引入AWT包,程序将出错,可以试试看
p.setBackground(Color.BLUE)
p.setSize(100,100)//设置面板对象大小
f.getContentPane().add(p)//将面板添加到窗体中
//如果使用下面添加面板的方法,面板将布满整个窗口,可以试试看
//f.setContentPane(p)
}
}
参考资料:
百度百科——JPanel
java中关闭Panel,使用dispose方法,实例如下:
import java.awt.Buttonimport java.awt.Color
import javax.swing.JFrame
import java.awt.Label
import java.awt.Panel
import java.awt.TextField
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
public class demo2 implements ActionListener {
JFrame nw= new JFrame("登录")
Panel nn=new Panel()
Label a1=new Label("用户名")
Label a2=new Label("密码 ")
TextField b1=new TextField("name ")
TextField b2=new TextField("password")
Button n1=new Button("确定")
public void run(){
nn.add(a1)
nn.add(b1)
nn.add(a2)
b2.setEchoChar('*')
n1.addActionListener(this)
nn.add(b2)
nn.add(n1)
nw.add(nn)
nw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
nw.setBackground(Color.cyan)
nw.setSize(300, 300)
nw.setVisible(true)
}
public static void main(String[] args){
demo2 e1=new demo2()
e1.run()
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==n1){
JFrame nw1=new JFrame("信息")
TextField b3=new TextField("功能尚未开放")
nw1.add(b3)
nw1.setSize(200,200)
nw1.setVisible(true)
}
}
}