您的位置:首页 > 编程语言 > Java开发

java swing 窗口布局

2010-05-07 12:24 260 查看
Code:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



public class AppleExpertSystem {

public static void main(String[] args) {

AppleMainFrame aesFrame = new AppleMainFrame("系统");

aesFrame.setVisible(true);

}

}



class AppleMainFrame extends JFrame implements ActionListener {

Container p;

final JScrollPane leftPane; //放目录树面板

final JScrollPane rightPane; //主面板

final JSplitPane jsp; //分割窗口

final JPanel treePane = new JPanel();

final JPanel mainPane = new JPanel();

int V_SBar = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;

int H_SBar = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;



final JMenuBar mb = new JMenuBar();

final JMenu[] m = {new JMenu("开始"),

new JMenu("决策"),

new JMenu("视图"),

new JMenu("帮助")};

final JMenuItem[][] mi = {{new JMenuItem("功能"),new JMenuItem("退出")},

{new JMenuItem("")},

{new JMenuItem("")},

{new JMenuItem("使用帮助"),new JMenuItem("关于")}};

public AppleMainFrame(String title) {

super(title);

setSize(800, 600);

centerOnScreen();

p = getContentPane();

for(int i=0; i<m.length; i++) {

for(int j=0; j<mi[i].length; j++) {

m[i].add(mi[i][j]);

mi[i][j].addActionListener(this);

}

mb.add(m[i]);

}

setJMenuBar(mb);

treePane.add(new JTextArea("left"));

mainPane.add(new JTextArea("right"));

leftPane = new JScrollPane(treePane, V_SBar, H_SBar);

rightPane = new JScrollPane(mainPane, V_SBar, H_SBar);

jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPane, rightPane);

Dimension winSize = getSize();

jsp.setDividerLocation(winSize.width/6);

jsp.setDividerSize(5);

p.add(jsp);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}



public void actionPerformed(ActionEvent e) {

}



public void centerOnScreen() { //使窗体被创建后在屏幕中间显示

Dimension displaySize = getToolkit().getScreenSize();

Dimension winSize = getSize();

int x = (displaySize.width - winSize.width) / 2;

int y = (displaySize.height - winSize.height) / 2;

if(x < 0) {

x = 0;

}

if(y < 0) {

y = 0;

}

setLocation(x, y);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐