java swing 窗口布局

Code:
  1. import java.awt.*;   
  2. import java.awt.event.*;   
  3. import javax.swing.*;   
  4.   
  5. public class AppleExpertSystem {   
  6.     public static void main(String[] args) {   
  7.         AppleMainFrame aesFrame = new AppleMainFrame("系统");   
  8.         aesFrame.setVisible(true);   
  9.     }   
  10. }   
  11.   
  12. class AppleMainFrame extends JFrame implements ActionListener {   
  13.     Container p;   
  14.     final JScrollPane leftPane;                   //放目录树面板   
  15.     final JScrollPane rightPane;                  //主面板   
  16.     final JSplitPane jsp;                         //分割窗口   
  17.     final JPanel treePane = new JPanel();   
  18.     final JPanel mainPane = new JPanel();   
  19.     int V_SBar = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;   
  20.     int H_SBar = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;   
  21.        
  22.     final JMenuBar mb = new JMenuBar();   
  23.     final JMenu[] m = {new JMenu("开始"),   
  24.                                          new JMenu("决策"),   
  25.                                          new JMenu("视图"),   
  26.                                          new JMenu("帮助")};   
  27.     final JMenuItem[][] mi = {{new JMenuItem("功能"),new JMenuItem("退出")},   
  28.                                 {new JMenuItem("")},   
  29.                                 {new JMenuItem("")},   
  30.                                 {new JMenuItem("使用帮助"),new JMenuItem("关于")}};   
  31.     public AppleMainFrame(String title) {   
  32.         super(title);   
  33.         setSize(800600);   
  34.         centerOnScreen();   
  35.         p = getContentPane();   
  36.         for(int i=0; i<m.length; i++) {   
  37.       for(int j=0; j<mi[i].length; j++) {     
  38.         m[i].add(mi[i][j]);       
  39.         mi[i][j].addActionListener(this);   
  40.       }       
  41.       mb.add(m[i]);       
  42.     }   
  43.         setJMenuBar(mb);   
  44.         treePane.add(new JTextArea("left"));   
  45.         mainPane.add(new JTextArea("right"));   
  46.         leftPane = new JScrollPane(treePane, V_SBar, H_SBar);   
  47.         rightPane = new JScrollPane(mainPane, V_SBar, H_SBar);   
  48.         jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPane, rightPane);   
  49.         Dimension winSize = getSize();   
  50.         jsp.setDividerLocation(winSize.width/6);   
  51.         jsp.setDividerSize(5);   
  52.         p.add(jsp);   
  53.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  54.     }   
  55.        
  56.     public void actionPerformed(ActionEvent e) {   
  57.     }   
  58.        
  59.     public void centerOnScreen() {        //使窗体被创建后在屏幕中间显示   
  60.         Dimension displaySize = getToolkit().getScreenSize();   
  61.         Dimension winSize = getSize();   
  62.         int x = (displaySize.width - winSize.width) / 2;   
  63.         int y = (displaySize.height - winSize.height) / 2;   
  64.         if(x < 0) {   
  65.             x = 0;   
  66.         }   
  67.         if(y < 0) {   
  68.             y = 0;   
  69.         }   
  70.         setLocation(x, y);   
  71.     }   
  72. }  

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值