
import java.awt.BorderLayout; |
import java.awt.Color; |
import java.awt.Image; |
import java.awt.Toolkit; |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionListener; |
import java.beans.PropertyVetoException; |
import java.net.URL; |
import javax.swing.ImageIcon; |
import javax.swing.JButton; |
import javax.swing.JDesktopPane; |
import javax.swing.JFrame; |
import javax.swing.JInternalFrame; |
import javax.swing.JPanel; |
/** |
* 桌面面板和内部窗体 |
* @author Administrator |
* |
*/ |
public class JDeskPane_and_JInternalFrame extends JFrame{ |
JDesktopPane desktopPane = null; |
|
//InternalFrame类----是自己定义的一个类 |
InternalFrame aInFrame = null; |
InternalFrame bInFrame = null; |
InternalFrame cInFrame = null; |
|
|
public static void main(String[] args) { |
//再次显示JFrame窗体------能起到刷新的效果 |
new JDeskPane_and_JInternalFrame().setVisible(true); |
} |
/** |
* 构造方法 |
*/ |
public JDeskPane_and_JInternalFrame(){ |
this.setTitle("JDeskPane_and_JInternalFrame"); |
//显示JFrame窗体 |
this.setVisible(true); |
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
this.setSize(800, 600); |
//设置JFrame窗体在电脑显示屏居中 |
this.setLocationRelativeTo(null); |
|
//获取图片的路径 |
URL resource = this.getClass().getResource("aa.jpg"); |
//获取图片 |
Image image = Toolkit.getDefaultToolkit().getImage(resource); |
//设置JFrame窗体的----图标 |
this.setIconImage(image); |
|
desktopPane = new JDesktopPane(); |
//设置JDesktopPane的背景颜色 |
desktopPane.setBackground(Color.lightGray); |
this.getContentPane().add(desktopPane, BorderLayout.CENTER); |
|
|
final JPanel panel = new JPanel(); |
//设置JPanel的背景颜色 |
panel.setBackground(Color.gray); |
this.getContentPane().add(panel, BorderLayout.NORTH); |
JButton aButton = new JButton("AAA--Button"); |
aButton.addActionListener(new ButtonListener(aInFrame, "aButton---实例化的AAA")); |
panel.add(aButton); |
|
JButton bButton = new JButton("BBB--Button"); |
bButton.addActionListener(new ButtonListener(bInFrame, "bButton---实例化的BBB")); |
panel.add(bButton); |
|
JButton cButton = new JButton("CCC--Button"); |
cButton.addActionListener(new ButtonListener(cInFrame, "cButton--实例化的CCC")); |
panel.add(cButton); |
} |
|
|
/** |
* 设置一个按钮事件的监听类 |
* |
* @author Administrator |
* |
*/ |
private class ButtonListener implements ActionListener{ |
InternalFrame inFrame; |
String title; |
|
/** |
*构造方法 |
*/ |
public ButtonListener(InternalFrame inFrame, String title) { |
this.inFrame = inFrame; |
this.title = title; |
} |
|
/** |
* 鼠标的事件监听 |
*/ |
@Override |
public void actionPerformed(ActionEvent e) { |
/** |
* 只有窗体没有实例化,或者窗体实例化后关闭了。 |
* 才能打开窗体 |
*/ |
if(inFrame == null || inFrame.isClosed()){ |
//获取桌面面板中所有-----实例化的内部窗体 |
JInternalFrame[] allFrames = desktopPane.getAllFrames(); |
/*根据实例化的JInternalFrame,设置连个窗体的间隔, |
使实例化后的窗体不叠在一起。*/ |
int Hight = 30 * allFrames.length; |
int x = 30 + Hight; |
int y = x; |
int width = 400; |
int height = 300; |
|
inFrame = new InternalFrame(title); |
//设置inFrame实例化后的内部窗体的位置、大小 |
inFrame.setBounds(x, y, width, height); |
//设置inFrame内部窗体显示 |
inFrame.setVisible(true); |
//将inFrame添加到JDesktopPane中 |
desktopPane.add(inFrame); |
} |
|
//设置触发按钮事件的窗体被选中。 |
//那个按钮实例化的JInternalFrame,点击后这是内部窗体被选中 |
try { |
inFrame.setSelected(true); |
} catch (PropertyVetoException e1) { |
// TODO Auto-generated catch block |
e1.printStackTrace(); |
} |
} |
|
} |
|
|
/** |
* 内部窗体类------JInternalFrame |
* @author Administrator |
* |
*/ |
private class InternalFrame extends JInternalFrame{ |
public InternalFrame(String title){ |
//设置内部窗体的标题 |
this.setTitle(title); |
//设置内部窗体---是否可以改变大小 |
this.setResizable(true); |
//设置内部窗体---是否可以关闭 |
this.setClosable(true); |
//设置内部窗体---是否最大化 |
this.setMaximizable(true); |
//设置内部窗体---是否可以最小化 |
this.setIconifiable(true); |
|
URL resource = this.getClass().getResource("aa.jpg"); |
ImageIcon imageIcon = new ImageIcon(resource); |
//设置窗体的图标 |
this.setFrameIcon(imageIcon); |
} |
} |
|
} |




中级程序员
by: 陆痴 发表于:2017-04-24 21:55:36 顶(1) | 踩(1) 回复
点击一个按钮,在JDesktopPane(桌面面板)中弹出一个JInternalFrame(内部窗体)

回复评论