有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java自动关闭第二个Swing JFrame

我在一个文件中创建了两个类。一个是指记事本,第二个是指加载屏幕progressBar

但当我尝试执行它时,进度条框架仍保留在屏幕上,只有在我按下关闭按钮时才会关闭。我想自动关闭进度条,而不是在100%而不是另一帧后点击关闭按钮

public class swingNotePad extends MouseAdapter implements ActionListener,MouseListener{
JFrame f;
JTextArea txt;
JMenuBar mb;
JMenu m1,m2,m3,m4,m5;
JMenuItem mi1,mi2,mi3,mi4,mi5,mi6,mi7,mi8;
JPopupMenu pm;
JSeparator js,js2;
public void MethodswingNotePad(){
     f=new JFrame("Developed by UC");
     txt=new JTextArea();
     txt.setBounds(0,0,600,570);
     js=new JSeparator();                                                              
     mb=new JMenuBar();
     m1=new JMenu("File");
     js2=new JSeparator(SwingConstants.VERTICAL);
     m2=new JMenu("Edit");
     m3=new JMenu("Format");
     m4=new JMenu("View");
     m5=new JMenu("Help");
     mi1=new JMenuItem("Cut");
     mi2=new JMenuItem("Copy");
     mi3=new JMenuItem("Paste");
     mi4=new JMenuItem("Select All");
     mi5=new JMenuItem("cut");
     mi6=new JMenuItem("copy");
     mi7=new JMenuItem("paste");
     mi8=new JMenuItem("select all");
     m2.add(mi1);
     m2.addSeparator();
     m2.add(mi2);
     m2.addSeparator();
     m2.add(mi3);
     m2.addSeparator();
     m2.add(mi4);
     mi1.addActionListener(this);
     mi2.addActionListener(this);
     mi3.addActionListener(this);
     mi4.addActionListener(this);

     pm=new JPopupMenu();
     pm.add(mi5);pm.add(mi6);pm.add(mi7);pm.add(mi8);

     mb.add(m1);

    mb.add(m2);
    mb.add(m3);
    mb.add(m4);
    mb.add(m5);
     f.add(txt);f.add(pm);   
     f.setJMenuBar(mb);
     f.setSize(600,600);
     f.setLayout(null);
     f.setVisible(true);
     f.setDefaultCloseOperation(EXIT_ON_CLOSE);
 }
public void actionPerformed(ActionEvent e){
    if(e.getSource()==mi1){
        txt.cut();
    }
    if(e.getSource()==mi2){
        txt.copy();
    }
    if(e.getSource()==mi3){
        txt.paste();
    }
    if(e.getSource()==mi4){
        txt.selectAll();
    }
}
public static void main(String...args){
swingNotePad a=new swingNotePad();     
swingProgressBar2 obj=new swingProgressBar2();
obj.iterate();
a.MethodswingNotePad();
 }

 }



class swingProgressBar2 extends JFrame{
JProgressBar pb;
JLabel l;
int i=0,n=0;
swingProgressBar2(){
    pb=new JProgressBar(0,2000);
    pb.setBounds(50,60,150,40);
    l=new JLabel("Loading...");
    l.setBounds(120,20,100,20);
    add(pb);add(l);
    pb.setStringPainted(true);
    pb.setValue(0);
    setSize(300,300);
    setLayout(null);
    setVisible(true);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    if(i==2000){
        System.exit(0) ;   }
}
public void iterate(){
    while(i<2001){
        try{
            i+=100;
            pb.setValue(i);
            Thread.sleep(75);

        }
        catch(Exception e){
            System.out.println(e);
        }
    }   
    }
}

共 (1) 个答案

  1. # 1 楼答案

    在构造函数中不能System.exit(0)。当代码完成时,迭代甚至不会开始。尝试将代码放入迭代方法中。并使用dispose(),因为System.exit(0)将退出整个应用程序

    例如

    public void iterate(){
     while(i<2001){
      try{
       i+=100;
       pb.setValue(i);
       Thread.sleep(75);
    
      }catch(Exception e){
       System.out.println(e);
      }
     }   
     dispose();
    }