import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class MainformFrame extends JFrame implements ActionListener{
JButton sm,cm,scm,um,exitB;
public MainformFrame(){
setTitle("学生信息管理系统");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,200);
sm=new JButton("学生管理");
cm=new JButton("课程管理");
scm=new JButton("选课管理");
um=new JButton("用户管理");
exitB=new JButton("退出");
exitB.setToolTipText("安全退出系统");
JPanel lowerPanel=new JPanel();
Container c=this.getContentPane();
lowerPanel.add(sm);
lowerPanel.add(cm);
lowerPanel.add(scm);
lowerPanel.add(um);
lowerPanel.add(exitB);
c.add(lowerPanel);
sm.addActionListener(this);
cm.addActionListener(this);
scm.addActionListener(this);
um.addActionListener(this);
exitB.addActionListener(this);
} @Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==sm)
{
sms();
}
if(e.getSource()==cm)
{
cms();
}
if (e.getSource()==scm)
{
scms();
}
if (e.getSource()==um)
{
ums();
}
if (e.getSource()==exitB)
{
shutdown();
}

} private void shutdown() {
} private void ums() {
} private void scms() {
} private void cms() {
} private void sms() {
}
public static void main(String[]args){
new MainformFrame();
}
@SuppressWarnings("unused")
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
   super.paintComponent(g);
   Image img = Toolkit.getDefaultToolkit().getImage("hello.jpg");
   g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
     }
    } }我想所学生管理,课程管理,选课管理,用户管理各放一行,其间间隔相同,而"退出"则放在右下边,在“用户管理”的右边,请问该怎么做呢,我是初学者,请指教,谢谢……

解决方案 »

  1.   

    窗口布局的问题:使用GridLayout(int rows, int cols)(创建具有指定行数和列数的网格布局。) 
    lowerPanel.setLayout(new GridLayout(4,1));添加进去即可或者你也可以设置按钮在lowerPanel里面的最佳位置楼主若想做好Swing界面,建议用eclipse中的visual class实现窗口脱放,所有的界面代码自动生成。最后祝楼主好运.
              
      

  2.   

    package com.chinasofti.myteam.jdbc;import java.awt.*;
    import java.awt.event.*;import javax.swing.*;public class MainformFrame extends JFrame implements ActionListener {
    JButton sm, cm, scm, um, exitB;


    public MainformFrame () {
    setTitle("学生信息管理系统");
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(200, 200);
    sm = new JButton("学生管理");
    cm = new JButton("课程管理");
    scm = new JButton("选课管理");
    um = new JButton("用户管理");
    exitB = new JButton("退出");
    exitB.setToolTipText("安全退出系统");
    JPanel lowerPanel = new JPanel();

    lowerPanel.setLayout(new GridLayout(5,1));
    Container c = this.getContentPane();
    lowerPanel.add(sm , "1");   
    lowerPanel.add(cm , "2");
    lowerPanel.add(scm , "3");
    lowerPanel.add(um , "4");
    lowerPanel.add(exitB);
    c.add(lowerPanel);
    sm.addActionListener(this);
    cm.addActionListener(this);
    scm.addActionListener(this);
    um.addActionListener(this);
    exitB.addActionListener(this);
    } @Override
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == sm) {
    sms();
    }
    if (e.getSource() == cm) {
    cms();
    }
    if (e.getSource() == scm) {
    scms();
    }
    if (e.getSource() == um) {
    ums();
    }
    if (e.getSource() == exitB) {
    shutdown();
    } } private void shutdown() {
    } private void ums() {
    } private void scms() {
    } private void cms() {
    } private void sms() {
    } public static void main(String[] args) {
    new MainformFrame ();
    } @SuppressWarnings("unused")
    private class MyPanel extends JPanel {
    public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g);
    Image img = Toolkit.getDefaultToolkit().getImage("hello.jpg");
    g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
    }
    }}
      

  3.   

    你看这样还可以吧lowerPanel.setLayout(new GridLayout(5,1)); 
    lowerPanel.add(sm , "1");  
    lowerPanel.add(cm , "2"); 
    lowerPanel.add(scm , "3"); 
    lowerPanel.add(um , "4"); 
    添加这么几句就可以了,跟你要的效果怎样?
      

  4.   

    用GridBagLayout比GridLayout灵活,楼主研究下GridBagLayout和GridLayout的用法
      

  5.   

    谢谢你,但还是不行哦,我把那部分代码改成:
                      JPanel lowerPanel1=new JPanel();
    JPanel lowerPanel2=new JPanel();
    JPanel lowerPanel3=new JPanel();
    JPanel lowerPanel4=new JPanel();
    //JPanel lowerPanel5=new JPanel();
    lowerPanel1.add(sm);
    lowerPanel2.add(cm);
    lowerPanel3.add(scm);
    lowerPanel4.add(um);
    lowerPanel4.add(exitB);
    c.add(lowerPanel1);
    c.add(lowerPanel2);
    c.add(lowerPanel3);
    c.add(lowerPanel4);
    但下面那个"用户管理”和“退出”在同一行,可“用户管理”没法和“选课管理”那些同一列