当前位置: 代码迷 >> Java相关 >> 关于java GUI的问题
  详细解决方案

关于java GUI的问题

热度:163   发布时间:2012-05-30 15:24:54.0
关于java GUI的问题
我想设计一个学生信息的简单管理系统,界面采用的是的基于一个框架的,改变框架内的内容的程序。
但是当我触发触发器时界面并没有改变,我的思路是移除框架内的JPanel,再增加一个新的JPanel。但是内容并没有改变,但是当我用鼠标更改框架的大小时JPanel会更新,请问这是为什么? 下面是部分代码。(我不想用弹出新窗口的那种方法)。
package manage;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class Menu extends JFrame implements ActionListener{
   
    Star star = new Star();
   
    JButton b1 = new JButton("nzx");
   
    public Menu(){

        this.add(star);
        this.setJMenuBar(star.MB1);
        System.out.println("menu");
        this.setBounds(10, 10, 400, 400);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        star.Jlogin.addActionListener(this);
        star.Jbreak.addActionListener(this);
        
        
    }
   
        public void actionPerformed(ActionEvent e){
            
            
            if(e.getSource() == star.Jlogin){
                this.setJMenuBar(new MenuOfStudent().MOSD);
                System.out.println("welcome student");
            }
            else if(e.getSource() == star.Jbreak){
                MenuOfTeacher mot = new MenuOfTeacher();
                this.setJMenuBar(mot.MOTH);
                System.out.println("welcome teacher");
            }
        }
        
   
   

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Menu();

    }

}
*****************************************************
package manage;

import java.awt.*;
import javax.swing.*;

import java.util.*;

public class MenuOfStudent extends JPanel{
    JMenuBar MOSD = new JMenuBar();
    JButton Jlogin = new JButton("login");
    JButton Jbreak = new JButton("break");
    private JMenu MOS1 = new JMenu("11");
    private JMenu MOS2 = new JMenu("22");
    private JMenu MOS3 = new JMenu("33");
    private JMenuItem MOS1_1 = new JMenuItem("111");
    private JMenuItem MOS1_2 = new JMenuItem("112");
    private JMenuItem MOS1_3 = new JMenuItem("113");
    private JMenuItem MOS2_1 = new JMenuItem("221");
    private JMenuItem MOS2_2 = new JMenuItem("222");
    private JMenuItem MOS2_3 = new JMenuItem("223");
    private JMenuItem MOS3_1 = new JMenuItem("331");
    private JMenuItem MOS3_2 = new JMenuItem("332");
    private JMenuItem MOS3_3 = new JMenuItem("333");
   
    public MenuOfStudent(){
        MOS1.add(MOS1_1);
        MOS1.add(MOS1_2);
        MOS1.add(MOS1_3);
        MOS2.add(MOS2_1);
        MOS2.add(MOS2_2);
        MOS2.add(MOS2_3);
        MOS3.add(MOS3_1);
        MOS3.add(MOS3_2);
        MOS3.add(MOS3_3);
        MOSD.add(MOS1);
        MOSD.add(MOS2);
        MOSD.add(MOS3);
        
        //this.add(new JButton("star"));
        //this.add(new JButton("stop"));
        
        
    }
   
    public void paint(Graphics g){
        g.drawRect(10, 10, 100, 100);
    }


}
***************************************************************
package manage;

import java.awt.Graphics;

import javax.swing.*;

public class MenuOfTeacher extends JPanel{
    JMenuBar MOTH = new JMenuBar();
    private JMenu MOT1 = new JMenu("11");
    private JMenu MOT2 = new JMenu("22");
    private JMenu MOT3 = new JMenu("33");
    private JMenuItem MOT1_1 = new JMenuItem("111");
    private JMenuItem MOT1_2 = new JMenuItem("112");
    private JMenuItem MOT1_3 = new JMenuItem("113");
    private JMenuItem MOT2_1 = new JMenuItem("221");
    private JMenuItem MOT2_2 = new JMenuItem("222");
    private JMenuItem MOT2_3 = new JMenuItem("223");
    private JMenuItem MOT3_1 = new JMenuItem("331");
    private JMenuItem MOT3_2 = new JMenuItem("332");
    private JMenuItem MOT3_3 = new JMenuItem("333");
   
    public MenuOfTeacher(){
        MOT1.add(MOT1_1);
        MOT1.add(MOT1_2);
        MOT1.add(MOT1_3);
        MOT2.add(MOT2_1);
        MOT2.add(MOT2_2);
        MOT2.add(MOT2_3);
        MOT3.add(MOT3_1);
        MOT3.add(MOT3_2);
        MOT3.add(MOT3_3);
        MOTH.add(MOT1);
        MOTH.add(MOT2);
        MOTH.add(MOT3);
    }
    public void paint(Graphics g){
        g.drawRect(100, 100, 200, 200);
    }

}
***************************************************************
package manage;

import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;

public class Star extends JPanel{
    JRadioButton jrbt, jrbs, jrbv;
    ButtonGroup group = new ButtonGroup();
    JButton Jlogin = new JButton("login");
    JButton Jbreak = new JButton("break");
   
    JMenuBar MB1 = new JMenuBar();
   
    JMenu MSI = new JMenu("information");
   
    JMenu MSH = new JMenu("help");
    JMenuItem MSH1 = new JMenuItem("About");
   
    public Star(){
        
        this.setLayout(new FlowLayout(FlowLayout.LEFT, 40, 40));
        this.add(new JLabel("name        "));
        this.add(new JTextField(20));
        this.add(new JLabel("password"));
        this.add(new JPasswordField(20));
        this.add(jrbt = new JRadioButton("teacher"));
        this.add(jrbs = new JRadioButton("student"));
        this.add(jrbv = new JRadioButton("visiter"));
        
        group.add(jrbs);
        group.add(jrbt);
        group.add(jrbv);
        
        this.add(Jlogin);
        this.add(Jbreak);
        
        MB1.add(MSI);
        
        MSH.add(MSH1);
        MB1.add(MSH);
        
    }

}

搜索更多相关的解决方案: package  public  import  

----------------解决方案--------------------------------------------------------
我把你这两个改了下:  star.Jlogin.addActionListener(this);
         star.Jbreak.addActionListener(this);
如下:
star.Jlogin.addActionListener((java.awt.event.ActionListener) this);
         star.Jbreak.addActionListener((java.awt.event.ActionListener) this);
虽然我不怎么懂,但是我觉得应该是调用对象上的问题,用当前对象。

----------------解决方案--------------------------------------------------------
  相关解决方案