jpanel的分割

liqiaomu410 2010-10-19 08:04:04
请问各位高手姐姐 哥哥们 jpanel中有的什么方法可以比例分割面板。 这样可以使别的面板可以安比例大小添加进来。
...全文
81 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
package cn.com.edu.view.frame; import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Dimension; import java.awt.GridBagLayout; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.JToolBar; import org.jvnet.substance.SubstanceLookAndFeel; import org.jvnet.substance.skin.FindingNemoSkin; import cn.com.edu.action.JMenuItemAction; import cn.com.edu.action.MainFrameAction; import cn.com.edu.util.GBC; import cn.com.edu.view.panel.AddStudentInfoPanel; import cn.com.edu.view.panel.FindStudentInfoPanel; /** * 教务管理系统主界面 * * @author Administrator * */ public class MainFrame extends JFrame { private static MainFrame instance; private JMenuBar bar;// 菜单条 private JMenu studentJMenu;// 菜单 private JMenu teacherJMenu;// 菜单 private JPanel center = new JPanel();// 中心面板用来放置卡片 private CardLayout card = new CardLayout();// 卡片布局 private JPanel west;// 西边面板 private JSplitPane split;// 分割面板 private JToolBar tool;// 工具条 private MainFrameAction action = new MainFrameAction(this);// 按钮事件对象 private JMenuItemAction menuItemAction = new JMenuItemAction(this);// 菜单事件对象 private SystemTray tray;// 系统托盘 private TrayIcon trayIcon;// 设置系统托盘的图片 /** * 使用单子设计模式主界面对象 * */ private MainFrame() { init(); } public static MainFrame getInstance() { if (instance == null) { instance = new MainFrame(); } return instance; } /** * 初始化主界面 * */ public void init() { // 设置标题 this.setTitle("教务管理系统"); // 设置标题图片 ImageIcon icon = new ImageIcon("img/switchuser.png"); this.setIconImage(icon.getImage()); // 得到屏幕对象 Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); // 设置主界面大小 this.setSize(size.width, size.height - 20); // 设置居中 this.setLocationRelativeTo(null); // 添加工具条 this.add(createTool(), BorderLayout.NORTH); // 将菜单添加到主界面 this.setJMenuBar(createJMenuBar()); // 将卡片添加到主界面 center.setLayout(card); addCardPanel(center); this.add(createSplit()); // 设置关闭主界面 this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE); //创建系统托盘 createSystemTray(); //关闭窗口事件 closeWindow(this); // 设置显示主界面 this.setVisible(true); } public JMenuBar createJMenuBar() { if (bar == null) { bar = new JMenuBar(); studentJMenu = createJMenu("学生管理"); teacherJMenu = createJMenu("老师管理"); addJMenuItem(studentJMenu, "添加学生信息"); addJMenuItem(studentJMenu, "查询学生信息"); addJMenuItem(studentJMenu, "修改学生信息"); addJMenuItem(studentJMenu, "删除学生信息"); studentJMenu.addSeparator(); addJMenuItem(studentJMenu, "退出"); bar.add(studentJMenu); bar.add(teacherJMenu); } return bar; } /** * 创建菜单 * * @param name * @return */ private JMenu createJMenu(String name) { JMenu menu = new JMenu(name); return menu; } /** * 将创建的菜单项添加到菜单 * * @param menu * @param name */ private void addJMenuItem(JMenu menu, String name) { JMenuItem item = new JMenuItem(name); item.addActionListener(menuItemAction); menu.add(item); } /** * 用于添加卡片 * * @param center */ public void addCardPanel(JPanel center) { JPanel jp2 = new JPanel(); JPanel jp3 = new JPanel(); JPanel jp4 = new JPanel(); jp2.add(new JButton("卡片2")); jp3.add(new JButton("卡片3")); jp4.add(new JButton("卡片4")); center.add(new AddStudentInfoPanel(), "添加学生信息"); center.add(new FindStudentInfoPanel(), "查询学生信息"); center.add(jp3, "修改学生信息"); center.add(jp4, "删除学生信息"); } /** * 创建西边面板,用添加选项按钮 * * @return */ public JPanel createWestPanel() { if (west == null) { west = new JPanel(); west.setLayout(new GridBagLayout()); west.add(createButton("添加学生信息", "img/switchuser.png"), new GBC(0, 0).setInset(10)); west.add(createButton("查询学生信息", "img/switchuser.png"), new GBC(0, 1).setInset(10)); west.add(createButton("修改学生信息", "img/switchuser.png"), new GBC(0, 2).setInset(10)); west.add(createButton("删除学生信息", "img/switchuser.png"), new GBC(0, 3).setInset(10)); } return west; } /** * 创建按钮方法 * * @param name * @return */ public JButton createButton(String name, String icon) { JButton button = new JButton(name); button.setIcon(new ImageIcon(icon)); button.addActionListener(action); return button; } public CardLayout getCard() { return card; } public JPanel getCenter() { return center; } /** * 分割面板 * * @return */ public JSplitPane createSplit() { if (split == null) { split = new JSplitPane(); split.setOneTouchExpandable(true); split.setLeftComponent(createWestPanel()); split.setRightComponent(center); } return split; } /** * 创建工具条 * * @return */ public JToolBar createTool() { if (tool == null) { tool = new JToolBar(); tool.add("添加学生信息", createButton("添加学生信息", "img/switchuser.png")); tool.add("查询学生信息", createButton("查询学生信息", "img/switchuser.png")); tool.add("修改学生信息", createButton("修改学生信息", "img/switchuser.png")); tool.add("删除学生信息", createButton("删除学生信息", "img/switchuser.png")); tool.add("帮助", createButton("帮助", "img/syssetup.png")); } return tool; } ///////////////////////////系统托盘设置///////////////////////////////////// /** * 窗口事件 * * @param jframe */ public void closeWindow(MainFrame jframe) { jframe.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int show = JOptionPane.showConfirmDialog(null, "是否确定关闭?", "确认关闭系统", JOptionPane.YES_NO_OPTION); if (show == JOptionPane.YES_OPTION) { System.exit(0); } } public void windowIconified(WindowEvent e) { if (getState() == 1) {// 最小化 try { tray.add(trayIcon); } catch (AWTException e1) { e1.printStackTrace(); } setVisible(false); } } }); } /** * 创建系统托盘 * */ public void createSystemTray() { // 得到当前系统的托盘对象 tray = SystemTray.getSystemTray(); ImageIcon icon = new ImageIcon("img/2.png"); // 添加鼠标右键 弹出菜单 PopupMenu menu = new PopupMenu(); MenuItem show = new MenuItem("显示窗体"); MenuItem exit = new MenuItem("退出窗体"); trayIcon = new TrayIcon(icon.getImage(), "学生管理系统", menu); trayIcon.addMouseListener(new MouseAdapter() { /** * 鼠标点击事件 */ public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) {// 鼠标双击 tray.remove(trayIcon); setVisible(true); // 设置窗口全屏 setExtendedState(JFrame.MAXIMIZED_BOTH); } } }); /** *鼠标右键显示窗体 */ show.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tray.remove(trayIcon); setVisible(true); // 设置窗口全屏 setExtendedState(JFrame.MAXIMIZED_BOTH); } }); /** * 鼠标右键关闭窗体 */ exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int show = JOptionPane.showConfirmDialog(null, "是否确定关闭?", "确认关闭系统", JOptionPane.YES_NO_OPTION); if (show == JOptionPane.YES_OPTION) { System.exit(0); } } }); menu.add(show); menu.add(exit); } /** * @param args */ public static void main(String[] args) { SubstanceLookAndFeel.setSkin(new FindingNemoSkin()); // 蓝色幽灵 // SubstanceLookAndFeel.setSkin(new OfficeBlue2007Skin()); // 麦田风光 // SubstanceLookAndFeel.setSkin(new FieldOfWheatSkin()); // 默认皮肤 // SubstanceLookAndFeel.setSkin(new BusinessSkin()); // 朦胧风格 // SubstanceLookAndFeel.setSkin(new MistAquaSkin()); MainFrame.getInstance(); } }
Java拼图游戏面向对象课程设计报告 ———————————————————————————————— 作者: ———————————————————————————————— 日期: "姓 名 " "学 号 "20137045 "班 级 " " "Name " "Student No." "Class " " "代码总行数"222 "项目名称 " " "Code Lines" "Item " " "1.实验目的 " "本次课程设计旨在通过一个完整项目的开发,巩固面向对象程序设计、软件工程、 " "数据库技术等课程的相关知识,加深学生对Java语言的理解,尤其是对面向对象思" "想、Java编码规范、JDBC访问数据库的理解,使学生进一步掌握环境构建、代码编 " "写、文档阅读与理解、文档编写、软件测试、发行与部署等技能,进一步提高学生 " "的学习能力、理解能力、表达能力及沟通能力. " "2.实验内容 " "本次课程设计选定的实验项目是使用JAVA语言编写一个拼图游戏并实现其基本功能" "。不用自己手动切割图片,再进行加载.而是应用类方法在内存中进行图片切割和 " "加载,方便了用户的使用。 " "利用了Graphics中的public abstract boolean drawImage()方法把 img 中由 " "(sx1, sy1)(sx2, sy2)指定的矩形区域画到 observer 中由(dx1, dy1)(dx2, " "dy2)指定的矩形区域内进行构思拼图游戏的具体实现。 " "导入了一个可播放背景音乐的jar包,在玩游戏时可以欣赏背景音乐。 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "3.类图 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "4.运行界面 " " " "图1、游戏初始界面 " " " "图2、游戏运行界面 " " " " " " " "图3、拼图成功界面 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "5.关键代码(要求在类的定义以及方法定义的前面给出相关描述信息) " "//定义一个Pintu类 " "public class Jpgsaw extends JFrame{ " "private Image image, buff; //原始图片,缓存图片 " "private int[][] map = {{0, 1, 2}, " "{3, 4, 5}, " "{6, 7, 8} " "}; //定义数组map,给分割后的图片标号 " "private Point point = new Point(2, 2); //记录第九块图片位置 " "private int sx; //分割后每一个图片的宽度 " "private int sy; //分割后每一个图片的高度 " "private Canvas canvas; " "//加载图片容器,定义可以显式定位子元素使用坐标是相对 Canvas " "区域的区域,绘制图形 " "private Graphics gs; //gs画出Canvas图片坐标 " "private Graphics gb; //gb画出buff图像 " "private boolean isRunning = false; //游戏是否正在进行 " "private JButton start = new JButton("开始新的游戏"); // 按钮1 " "private JButton show = new JButton("显示正确图片"); //按钮2 " "private JTextArea showStep = new JTextArea("显示步骤");//设置步骤显示 " "private JPanel panel = new JPanel(); " "//一般轻量级面板容器,装在上面2个按钮 " "private int steps = 0; // 记录移动的步骤 " "public Jpgsaw(String title) { //构造方法,初始化变量 " "super(title);//调用了当前类Jpgsaw的父类JFrame的构造方法 " "try { //异常抛出 " "image = ImageIO.read(new File("gg。jpg")); /

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧