如何从JAVA中的动作侦听器向容器添加按钮?

时间:2014-05-10 16:51:02

标签: java

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StudentDatabase extends JFrame {
    JLabel jl= new JLabel("Name:");
    JLabel jl1= new JLabel("Class");
    JLabel jl2= new JLabel("Reg number");
    JLabel jl3= new JLabel("Semester");
    JLabel jl4= new JLabel("GPA");
    public JLabel jl5= new JLabel("Semester");
    JLabel jl6= new JLabel("Credit Hours");
    JLabel jl7= new JLabel("Semester 2");
    JButton jb= new JButton("SAVE");
    String name, cLass, regn;
    JTextField jt= new JTextField("  Enter  Name");
    JTextField jt1= new JTextField("   Enter Class");
    JTextField jt2= new JTextField("  Enter  Reg number");
    JTextField jt3= new JTextField("  Enter  Semester");
    JTextField jt4= new JTextField("  GPA    ");
    JTextField jt5= new JTextField("  Enter Semester GPA");
    JTextField jt6= new JTextField("  Enter  CH");
    public Container c= this.getContentPane();
    StudentDatabase(){

        c.setLayout(new FlowLayout());
        this.setBounds(100,100,300,400);
        c.add(jl);
        c.add(jt);
        c.add(jl1);
        c.add(jt1);
        c.add(jl2);
        c.add(jt2);
        c.add(jl3);
        c.add(jt3);
        c.add(jl4);
        c.add(jt4);

        jb.addActionListener(new mal());
        name=jt.getText();
        cLass=jt1.getText();
        regn=jt2.getText();

        c.add(jb);
        this.setVisible(true);
        c.setVisible(true);


    }
    protected class mal implements ActionListener {
        String name, cLass, regn,s,g,ch;

        public void actionPerformed(ActionEvent e){

            s=jt3.getText();
            s=s.trim();
            System.out.println("abnc");
            System.out.println(s);
            if(s.equals("1")){

                c.add(jl5);
                c.add(jt5);
                c.add(jl6);
                c.add(jt6);
                c.setVisible(true);

                System.out.println(s);
                ch=jt6.getText();
                System.out.println(ch);
                jt4.setText(s);
            }

        }
    }

}

所以这是我的代码。我想在按下SAVE按钮时显示一些按钮。为此,我将它们添加到动作监听器中。但问题是,当我的动作监听器被调用时,按钮不会显示。我该如何解决这个问题?

0 个答案:

没有答案