用 JLabel 对象填充多余的,就OK了,
JLabel label = new Label( " " );
panel.add( label ); //填在正确的位置
不记得 GridLayout 下有没有下面这个方法,可以改变控件大小
label.setPreferredSize( new Dimension( 58, 30 ));
不过我想你不喜欢这种布局,换个流布局(FlowLayout) 看看
这是我自己写的一个界面( 截取部分 ), 写的不好将就着看看吧,这是不是你想要的布局格式
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Index {
private JFrame frame;
private JPanel panel;
private JPanel panelChild;
private JMenuBar menuBar;
private JMenu menuUse;
private JMenu menuHelp;
private JMenu menuOut;
private JMenuItem menuItemUp;
private JMenuItem menuItemDown;
private JMenuItem menuItemBack;
private JMenuItem menuItemAbout;
private JMenuItem menuItemExit;
private JLabel label1;
private JLabel label2;
private JLabel machine;
private JLabel card;
private JLabel passport;
private JLabel start;
private JComboBox boxMachine;
private JTextField testCard;
private JTextField testPassport;
private JTextField testStart;
private JButton submit;
private JButton reset;
private Date data;
private SimpleDateFormat simple;
public Index()
{
frame = new JFrame( "网吧计费管理系统" );
panel = new JPanel();
panelChild = new JPanel();
menuBar = new JMenuBar();
menuUse = new JMenu( " 使用" );
menuHelp = new JMenu( " 帮助" );
menuOut = new JMenu( " 退出");
menuItemUp = new JMenuItem( "上机" );
menuItemDown = new JMenuItem( "下机" );
menuItemBack = new JMenuItem( "返回" );
menuItemAbout = new JMenuItem( "关于" );
menuItemExit = new JMenuItem( "退出" );
label1 = new JLabel( "欢迎使用网吧计费管理系统" );
label2 = new JLabel( "5本8帅制作" );
machine = new JLabel( "机器号码:" );
card = new JLabel( "卡 号:" );
passport = new JLabel( "密 码:" );
start = new JLabel( "开始时间:" );
boxMachine = new JComboBox();
testCard = new JTextField();
testPassport = new JTextField();
testStart = new JTextField();
submit = new JButton( "提交" );
reset = new JButton( "重置" );
data = new Date();
simple = new SimpleDateFormat( "yyyy-MM-dd hh: mm: ss" );
init();
getJPanelChild();
}
public void init()
{
menuBar.add( menuUse );
menuBar.add( menuHelp );
menuBar.add( menuOut );
menuUse.add( menuItemUp );
menuUse.add( menuItemDown );
menuUse.add( menuItemBack );
menuHelp.add( menuItemAbout );
menuOut.add( menuItemExit );
menuBar.setBounds( new Rectangle( 0, 0, 400, 30 ));
menuUse.setPreferredSize( new Dimension( 58, 30 ));
menuHelp.setPreferredSize( new Dimension( 58, 30 ));
menuOut.setPreferredSize( new Dimension( 58, 30 ));
label1.setBounds( new Rectangle( 45, 50, 300, 50 ));
label1.setFont(new Font("\u534e\u6587\u884c\u6977", Font.BOLD, 24));
label2.setBounds( new Rectangle( 130, 120, 200, 50 ));
label2.setFont(new Font("Dialog", Font.BOLD, 12));
/*** 上机事件 ***/
menuItemUp.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae )
{
label1.setVisible( false );
label2.setVisible( false );
panelChild.setVisible( true );
}
});
/*** menuItemDown - 下机事件 ***/
menuItemDown.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae )
{
}
});
/*** menuItemBack - 返回最初界面事件***/
menuItemBack.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae )
{
label1.setVisible( true );
label2.setVisible( true );
panelChild.setVisible( false );
}
});
/*** menuItemExit事件 退出信息 ***/
menuItemExit.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae )
{
System.exit( 0 );
}
});
panel.add( label1 );
panel.add( label2 );
panel.add( menuBar );
panel.add( panelChild );
panel.setLayout( null );
frame.setLocation( 350, 150 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setContentPane( panel );
frame.setSize( new Dimension( 400, 400 ));
frame.setVisible( true );
}
/*** 登陆界面方法 ***/
public void getJPanelChild()
{
machine.setPreferredSize(new Dimension(80, 30));
boxMachine.setPreferredSize(new Dimension(150, 30));
card.setPreferredSize(new Dimension(80, 30));
testCard.setPreferredSize(new Dimension(150, 30));
passport.setPreferredSize(new Dimension(80, 30));
testPassport.setPreferredSize(new Dimension(150, 30));
start.setPreferredSize(new Dimension(80, 30));
testStart.setPreferredSize(new Dimension(150, 30));
submit.setPreferredSize(new Dimension(80, 30));
reset.setPreferredSize(new Dimension(80, 30));
boxMachine.addItem( "请选择机器号码" );
for( int i = 1; i <= 50; i++ )
{
boxMachine.addItem( new Integer( i ));
}
testStart.setText( simple.format( data ));
/*** reset事件,清空信息 ****/
reset.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae )
{
testCard.setText( "" );
testPassport.setText( "" );
}
});
panelChild.add( machine );
panelChild.add( boxMachine );
panelChild.add( card );
panelChild.add( testCard );
panelChild.add( passport );
panelChild.add( testPassport );
panelChild.add( start );
panelChild.add( testStart );
panelChild.add( new JLabel( " ") );
panelChild.add( new JLabel( " ") );
panelChild.add( submit );
panelChild.add( new JLabel( " " ));
panelChild.add( reset );
panelChild.setBounds( new Rectangle( 50, 50, 300, 300 ));
panelChild.setLayout( new FlowLayout( FlowLayout.CENTER, 5, 10 ));
panelChild.setVisible( false );
}
}
[此贴子已经被作者于2006-8-16 23:22:47编辑过]