一般来说,我们常把JPanel[面板]放到JFrame窗体中
但是也有一种内部窗体JInternalFrame ,可以放到其他的容器JDesktopPane里,
效果图如下
代码如下
import java.awt.*import java.awt.event.*
import java.beans.PropertyVetoException
import javax.swing.*
public class FrameDemo extends JFrame implements ActionListener{
JButton jb
JDesktopPane jdp
public FrameDemo(){
jb =new JButton("创建一个内部窗体")
jb.addActionListener(this)
jdp = new JDesktopPane()
add(jdp)
add(jb,BorderLayout.SOUTH)
setSize(500,500)
setDefaultCloseOperation(EXIT_ON_CLOSE)
setLocationRelativeTo(null)
setVisible(true)
}
public static void main(String[] args) {
new FrameDemo()
}
int index = 1
int x = 50
int y=50
public void actionPerformed(ActionEvent e) {
//内部窗口
JInternalFrame iframe = new JInternalFrame("第"+index+"个内部窗口"
, true, true, true, true)
index++
iframe.setLocation(x+=10, y+=10)
iframe.setSize(210, 180)
iframe.setVisible(true)
jdp.add(iframe)
try {
iframe.setSelected(true)//被选中
} catch (PropertyVetoException e1) {
e1.printStackTrace()
}
}
}
import java.awt.event.ActionEventimport java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JPanel
public class Demo1 {
/*
* 在主方法中创建窗口对象。
* 主方法只管创建不同的画板对象往里添加就行了。
*
*/
public static void main(String[] args) {
MyFrame frame = new MyFrame()
MyPanelOne panel1 = new MyPanelOne()
frame.add(panel1)
}
}
class MyFrame extends JFrame { // 主界面
public MyFrame() {
this.setTitle("主界面")
this.setSize(500, 500)
this.setVisible(true)
}
void addPanel(MyPanelOne panel) { // 传递画板的函数
this.setContentPane(panel)
this.setVisible(true)
}
}
class MyPanelOne extends JPanel { // 画板1
public MyPanelOne() {
createButton()
}
private void createButton() {
JButton b1 = new JButton("按钮一")
this.add(b1)
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("hello")
}
})
}
}
Frame是框架窗体,有边框的,Panel是面板,无边框,一般把几个Panel加到一个Frame上 Sun公司的定义:Frame是带标题和边框的最顶层窗体;Panel是个最简单的容器类,它提供空间让程序放其它组件,包括其它Panel。 添加到容器中的组件放在一个列表中。列表的顺序将定义组件在容器内的正向堆栈顺序。如果将组件添加到容器中时未指定索引,则该索引将被添加到列表尾部(此后它位于堆栈顺序的底部)。public Component getComponent(int n) 获得此容器中的第 n 个组件。public Component[] getComponents() 获得此容器中的所有组件。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)