package cn.com.csuinfo.gui;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;public class MainFrame extends JFrame {
private JPanel panel = new JPanel();
private JPanel toppanel ;//= new JPanel();
private void init() {
toppanel = new JPanel();
JPanel p = new JPanel();
p.setBackground(Color.GREEN);
BorderLayout layout = new BorderLayout();
this.add(toppanel);
Container c = this.getContentPane();
c.setLayout(layout);
c.add(this.addTop(),BorderLayout.NORTH); //添加到NORTH为什么会不显示呢????
// c.add(this.addTop(),BorderLayout.CENTER); //而添加到CENTER却可以
c.add(new JButton("south"), BorderLayout.SOUTH);
c.add(new JButton("left"), BorderLayout.WEST);
// this.add(this.addTop());
this.setTitle("音像销售管理");
this.setFrameLocation();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void setFrameLocation() {
this.setSize(900, 600);
// 获得屏幕尺寸
Dimension dimension = this.getToolkit().getScreenSize();
int width = (dimension.width - this.getWidth()) / 2;
int height = (dimension.height - this.getHeight()) / 2;
// 设置窗口左上角的位置
this.setLocation(width, height);
}
private JPanel addTop() {
// panel = new JPanel();
SpringLayout layout = new SpringLayout(); JButton[] btn = { new JButton("单据查询"), new JButton("音像查询"),
new JButton("软件帮助"),new JButton("退出系统") };
panel.setLayout(layout);
panel.setBackground(Color.GREEN);
for(int i = 0;i < btn.length;i++ ) {
panel.add(btn[i]);
System.out.println("添加按钮");
}
layout.putConstraint(SpringLayout.NORTH, btn[0], 10, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, btn[0], 40, SpringLayout.WEST, panel);
System.out.println("设置第一个按钮");
layout.putConstraint(SpringLayout.NORTH, btn[1], 10, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, btn[1], 40, SpringLayout.EAST, btn[0]);
System.out.println("设置第二个按钮");
//
layout.putConstraint(SpringLayout.NORTH, btn[2], 10, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, btn[2], 40, SpringLayout.EAST, btn[1]);
System.out.println("设置第三个按钮");
layout.putConstraint(SpringLayout.NORTH, btn[3], 10, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, btn[3], 40, SpringLayout.EAST, btn[2]);
System.out.println("设置第四个按钮");
return panel;
}
public MainFrame() {
init();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame();
}}
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;public class MainFrame extends JFrame {
private JPanel panel = new JPanel();
private JPanel toppanel ;//= new JPanel();
private void init() {
toppanel = new JPanel();
JPanel p = new JPanel();
p.setBackground(Color.GREEN);
BorderLayout layout = new BorderLayout();
this.add(toppanel);
Container c = this.getContentPane();
c.setLayout(layout);
c.add(this.addTop(),BorderLayout.NORTH); //添加到NORTH为什么会不显示呢????
// c.add(this.addTop(),BorderLayout.CENTER); //而添加到CENTER却可以
c.add(new JButton("south"), BorderLayout.SOUTH);
c.add(new JButton("left"), BorderLayout.WEST);
// this.add(this.addTop());
this.setTitle("音像销售管理");
this.setFrameLocation();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void setFrameLocation() {
this.setSize(900, 600);
// 获得屏幕尺寸
Dimension dimension = this.getToolkit().getScreenSize();
int width = (dimension.width - this.getWidth()) / 2;
int height = (dimension.height - this.getHeight()) / 2;
// 设置窗口左上角的位置
this.setLocation(width, height);
}
private JPanel addTop() {
// panel = new JPanel();
SpringLayout layout = new SpringLayout(); JButton[] btn = { new JButton("单据查询"), new JButton("音像查询"),
new JButton("软件帮助"),new JButton("退出系统") };
panel.setLayout(layout);
panel.setBackground(Color.GREEN);
for(int i = 0;i < btn.length;i++ ) {
panel.add(btn[i]);
System.out.println("添加按钮");
}
layout.putConstraint(SpringLayout.NORTH, btn[0], 10, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, btn[0], 40, SpringLayout.WEST, panel);
System.out.println("设置第一个按钮");
layout.putConstraint(SpringLayout.NORTH, btn[1], 10, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, btn[1], 40, SpringLayout.EAST, btn[0]);
System.out.println("设置第二个按钮");
//
layout.putConstraint(SpringLayout.NORTH, btn[2], 10, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, btn[2], 40, SpringLayout.EAST, btn[1]);
System.out.println("设置第三个按钮");
layout.putConstraint(SpringLayout.NORTH, btn[3], 10, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, btn[3], 40, SpringLayout.EAST, btn[2]);
System.out.println("设置第四个按钮");
return panel;
}
public MainFrame() {
init();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame();
}}
楼主【aduan668】截止到2008-07-17 14:13:07的历史汇总数据(不包括此帖):
发帖的总数量:8 发帖的总分数:160 每贴平均分数:20
回帖的总数量:18 得分贴总数量:3 回帖的得分率:16%
结贴的总数量:8 结贴的总分数:160
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:0.00 % 无满意结分率:0.00 %
敬礼!
// c.add(this.addTop(),BorderLayout.CENTER); //而添加到CENTER却可以
//因为BorderLayout中,添加到top时,宽度是与父组件相同,故这里的PreferredSize的宽度没用,随便什么int都行
panel.setPreferredSize(new Dimension(-1, 50));