回 帖 发 新 帖 刷新版面

主题:[原创]大家帮我看看这个程序!!!!!

我想在点击每个菜单按钮时关闭原来窗体,新打开一个窗体,怎么改进代码,或者怎么实现????谢谢大家!!!!



package AnythingSet;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

//import JavaSwing.ComboBoxDemo;
/**
 * 
 * @author Administrator
 *
 */
public class ViewFrame extends Applet  implements ActionListener
{
    /**
     * @serialField
     */
    private static final long serialVersionUID = 1L;
    private  JFrame f;
    private Panel flowPanel;
    private Panel gridPanel;
    private Panel flowPanel1;
    private Panel boxPanel;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b4;
    private JButton b5;
    public ViewFrame()
    {
        f=new JFrame("学生管理系统");
        f.setSize(600,400);
        f.setLocation(100,100);
        f.setBackground(Color.gray);
        Container contentPane=f.getContentPane();
        contentPane.setLayout(new BorderLayout());
        setPanel1();
        setPanel2();
        contentPane.add(flowPanel,"North");
        contentPane.add(gridPanel,"Center");
        f.setBackground(Color.BLUE);
        f.setResizable(false);
        f.setVisible(true);
    
    }
    public void exit()
    {
        f.addWindowListener(new WindowListener()
        {
            public void windowActivated(WindowEvent e) 
            {}
            public void windowClosed(WindowEvent e) 
            {}
            public void windowClosing(WindowEvent e) 
            {
                System.exit(0);
            }
            public void windowDeactivated(WindowEvent e) 
            {}
            public void windowDeiconified(WindowEvent e) 
            {}
            public void windowIconified(WindowEvent e) 
            {}
            public void windowOpened(WindowEvent e) 
            {}
            
        });
    }
    
    public void setPanel1()
    {
        flowPanel=new Panel();
        flowPanel.setSize(600, 40);
        Box box=Box.createHorizontalBox();
        Font font=new Font("楷体_GB2312",Font.BOLD,40);
        JLabel l;
        l=new JLabel("学生管理系统(v1.1)");
        l.setHorizontalTextPosition(JLabel.HEIGHT);
        l.setFont(font);
        box.add(l,Box.BOTTOM_ALIGNMENT);
        flowPanel.add(box,Panel.CENTER_ALIGNMENT);
        flowPanel.setBackground(Color.LIGHT_GRAY);
    }
    public void setPanel2()
    {
        gridPanel=new Panel();
        gridPanel.setSize(600, 360);
        gridPanel.setLayout(new GridLayout(1,2));
        //Box baseBox1=Box.createHorizontalBox();
        setFlowPanel();
        setBoxPanel();
        gridPanel.add(flowPanel1);
        gridPanel.add(boxPanel);
        //baseBox1.add(flowPanel1, Box.TOP_ALIGNMENT);
        
        //baseBox1.add(Box.createHorizontalStrut(20));
        //baseBox1.add(boxPanel,Box.LEFT_ALIGNMENT);
        //gridPanel.add(baseBox1);
    }
    public void setFlowPanel()
    {
        flowPanel1=new Panel();
        //flowPanel1.setSize(300, 300);
        flowPanel1.setLayout(new BorderLayout());
        JLabel l=new JLabel(new ImageIcon(".\\icon\\1.jpg"));
        flowPanel1.add(l,"Center");
        flowPanel1.setBackground(Color.blue);
    }
    public void setBoxPanel()
    {
        boxPanel=new Panel();
        boxPanel.setSize(300,300);
        boxPanel.setLocation(300,100);
        boxPanel.setLayout(new BoxLayout(boxPanel,BoxLayout.X_AXIS));
        Box baseBox=Box.createVerticalBox();
        b1=new JButton("                            输入学生信息                                 ");
        b2=new JButton("                            查询学生信息                                 ");
        b3=new JButton("                            统计平均成绩                                  ");
        b4=new JButton("                               统计总成绩                                   ");
        b5=new JButton("                                 关于我们                                     ");
        JLabel ll=new JLabel("                              ====功能菜单====");
        baseBox.add(Box.createHorizontalStrut(1));
        baseBox.add(ll);
        baseBox.add(Box.createHorizontalStrut(1));
        baseBox.add(b1);
        baseBox.add(Box.createVerticalStrut(10));
        baseBox.add(b2);
        baseBox.add(Box.createVerticalStrut(10));
        baseBox.add(b3);
        baseBox.add(Box.createVerticalStrut(10));
        baseBox.add(b4);
        baseBox.add(Box.createVerticalStrut(10));
        baseBox.add(b5);
        baseBox.add(Box.createHorizontalStrut(1));
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        JLabel l=new JLabel("                       科比工作室版权所有,盗版必究。");
        //baseBox.add(Box.createHorizontalStrut(10));
        baseBox.add(l);
        boxPanel.add(baseBox,Panel.TOP_ALIGNMENT);
        flowPanel1.setBackground(Color.green);
    }
    public static void main(String[] args)
    {
        
        new ViewFrame();
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
        {
            exit();
            //new ComboBoxDemo();
        }
        if(e.getSource()==b2)
        {
            exit();
        }
        if(e.getSource()==b3)
        {
            exit();
        }
        if(e.getSource()==b4)
        {
            exit();
        }
        if(e.getSource()==b5)
        {
            exit();
        }
        
    } 
}

回复列表 (共3个回复)

沙发

大概知道了你想要的效果,我也编过swing程序,确实不好怎么控制,
不过我采用,隐藏的方法来,不显示,setViseble()方法控制,或者用模式对话框,也可以实现要的效果,,
想办法继承。。。。。
把JFrame这样的主体窗口方到static这样的成员中去保存起来,要显示的时候就调用ClassName.Jfram.esetVisible(ture);不要的时候就setViseble(false)就可以了。。
但是这样做,不知道是不是有违背我们的面向对象开发原则。。

还有一种方法就是,你编写的类的构造时候节个主体窗口的对象的应用,在子窗口中,想办法隐藏主体窗口。
如:
class MainFram {
    public stativ void main(...){
        JFrame frame=new JFrame("System");
         ................
        SubFrame f=new SubFrame(frame);
         ......
    }
}
class Subframe{
  public Subfarme(JFrame frame){
      frame.setVisible(false);
      ......
}

板凳

不知道有没更好的方法,,

3 楼

thank you !!!!

我来回复

您尚未登录,请登录后再回复。点此登录或注册