java 内部窗体_Java平铺窗体 内部窗体

0818b9ca8b590ca3270a3433284dd417.png

package swings;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JDesktopPane;

import javax.swing.JFrame;

import javax.swing.JInternalFrame;

import javax.swing.JPanel;

import javax.swing.JToolBar;

import javax.swing.UIManager;

import javax.swing.border.EmptyBorder;

public class MDITileSort extends JFrame {

private JPanel contentPane;

private JDesktopPane desktopPane;

/**

* Launch the application.

*/

public static void main(String[] args) {

try {

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

} catch (Throwable e) {

e.printStackTrace();

}

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

MDITileSort frame = new MDITileSort();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public MDITileSort() {

setTitle("\u5B50\u7A97\u4F53\u5782\u76F4\u6392\u5217");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 545, 403);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

contentPane.setLayout(new BorderLayout(0, 0));

setContentPane(contentPane);

desktopPane = new JDesktopPane();

contentPane.add(desktopPane, BorderLayout.CENTER);

JToolBar toolBar = new JToolBar();

contentPane.add(toolBar, BorderLayout.NORTH);

JButton button = new JButton("\u52A0\u8F7D\u5B50\u7A97\u4F53");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

do_button_actionPerformed(e);

}

});

toolBar.add(button);

JButton button_1 = new JButton("\u7A97\u4F53\u5E73\u94FA");

button_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

do_button_1_actionPerformed(e);

}

});

toolBar.add(button_1);

}

private int fCount = 0;

private Random random = new Random();

protected void do_button_actionPerformed(ActionEvent e) {

JInternalFrame jif = new JInternalFrame("子窗体" + fCount++, true, true, true, true);// 创建内部窗体

jif.setSize(random.nextInt(100) + 100, random.nextInt(100) + 100);

jif.setLocation(random.nextInt(getWidth() - 100), random.nextInt(getHeight() - 100));// 随机定位内部窗体

desktopPane.add(jif);// 添加内部窗体到桌面面板

jif.setVisible(true);// 显示内部窗体

}

protected void do_button_1_actionPerformed(ActionEvent e) {

// 设置桌面面板使用网格布局管理器

desktopPane.setLayout(new GridLayout((int) Math.sqrt(fCount), 0));

desktopPane.doLayout();// 布局桌面面板中所有控件

desktopPane.setLayout(null);// 取消桌面面板的布局管理器

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值