Java JFrame frame.dispose()没有按预期工作



我正在尝试处理我的框架并创建一个新的。新的被创造出来,但旧的仍然存在。最后,我的桌面上有两张相框。我错过了什么?谢谢。

package org.rockislandschools;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DisplayStatus extends JFrame {
    public void buildFrame(String ipAdd, String status){
    JFrame frame = new JFrame("Host Status");
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
    JPanel panel = new JPanel();
    JLabel ipLabel = new JLabel(ipAdd);
    ipLabel.setOpaque(true);
    ipLabel.setBackground(Color.white);
    ipLabel.setPreferredSize(new Dimension(20, 25));
    JLabel stateLabel = new JLabel(status);
    stateLabel.setOpaque(true);
    if (status.equals("Up")){
        stateLabel.setBackground(Color.GREEN);
    }
    if (status.equals("Down")){
        stateLabel.setBackground(Color.red);
    }
    stateLabel.setPreferredSize(new Dimension(20, 25));
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    panel.setPreferredSize(new Dimension(400,400));
    frame.getContentPane().add(panel);
    frame.getContentPane().add(ipLabel, BorderLayout.CENTER);
    frame.getContentPane().add(stateLabel, BorderLayout.AFTER_LAST_LINE);
    frame.pack();
    frame.setVisible(true);
    }
}

package org.rockislandschools;
import java.io.IOException;
import java.net.InetAddress;
public class HostStatus {
    public String IsReachableReturnString(String ip){
        String canBeReachedReturnString = "Down";
        int timeout = 10000;
        try {
            if (InetAddress.getByName(ip).isReachable(timeout)) canBeReachedReturnString = "Up";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return canBeReachedReturnString;
    }
    public boolean IsReachable(String ip){
        boolean canBeReached = false;
        int timeout = 10000;
        try {
            if (InetAddress.getByName(ip).isReachable(timeout)) canBeReached = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        //System.out.println(canBeReached);
        return canBeReached;
    }
}
package org.rockislandschools;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
    public static void main(String[] args) {
        if(args.length == 0)
        {
            System.out.println("Please specify IP Address.");
            System.exit(0);
        }
        String address = args[0];
        HostStatus status = new HostStatus();
        String currentState = status.IsReachableReturnString(address);
        DisplayStatus statusFrame = new DisplayStatus();
        statusFrame.buildFrame(address, currentState);
        //String newState = status.IsReachableReturnString(address);
        while (true){
        String newState = status.IsReachableReturnString(address);
        if (newState.equals(currentState)){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                 Logger.getLogger(Main.class.getName()).log(Level.SEVERE,null, ex);
            }
            System.out.println("Nothing has changed");
        }
            else {
            statusFrame.setVisible(false);
            statusFrame.dispose();
            System.out.println("Building a new frame");
            currentState = newState;
            statusFrame.buildFrame(address, currentState);
            }
        }
     }
  }
关于更多的细节,我不确定我需要什么。我可以告诉你,我刚刚开始尝试学习Java。我住在爱荷华州,我喜欢在空闲时间不被批评我的帖子的红色箭头框打扰。

当你调用statusFrame.dispose();你在一个不存在的JFrame上调用它。我修复了代码,下面是每个文件:

编辑:我有一个版本的系统托盘和GUI选择我的github上的IP: https://github.com/ArgonBird18/HostStatus

Main.java

package org.rockislandschools;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
public class Main {
    public static void main(String[] args) {
        if(args.length == 0)
        {
            System.out.println("Please specify IP Address.");
            System.exit(0);
        }
        String address = args[0];
        HostStatus status = new HostStatus();
        String currentState = status.IsReachableReturnString(address);
        JFrame statusFrame = new JFrame();
        statusFrame = DisplayStatus.buildFrame(address, currentState);
        //String newState = status.IsReachableReturnString(address);
        while (true){
            String newState = status.IsReachableReturnString(address);
            if (newState.equals(currentState)){
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
                System.out.println("Nothing has changed, Time is " + timeStamp );
            } else {
                //statusFrame.setVisible(false);
                statusFrame.dispose();
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
                System.out.println("Building a new frame, Time is " + timeStamp);
                currentState = newState;
                statusFrame = DisplayStatus.buildFrame(address, currentState);
            }
        }
    }
}

HostStatus.java

package org.rockislandschools;
import java.io.IOException;
import java.net.InetAddress;
public class HostStatus {
    public String IsReachableReturnString(String ip){
        String canBeReachedReturnString = "Down";
        int timeout = 10000;
        try {
            if (InetAddress.getByName(ip).isReachable(timeout)) canBeReachedReturnString = "Up";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return canBeReachedReturnString;
    }
    public boolean IsReachable(String ip){
        boolean canBeReached = false;
        int timeout = 10000;
        try {
            if (InetAddress.getByName(ip).isReachable(timeout)) canBeReached = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        //System.out.println(canBeReached);
        return canBeReached;
    }
}

DisplayStatus.java

package org.rockislandschools;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DisplayStatus {
    public static JFrame buildFrame(String ipAdd, String status){
        JFrame frame = new JFrame("Host Status");
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
        JPanel panel = new JPanel();
        JLabel ipLabel = new JLabel(ipAdd);
        ipLabel.setOpaque(true);
        ipLabel.setBackground(Color.white);
        ipLabel.setPreferredSize(new Dimension(20, 25));
        JLabel stateLabel = new JLabel(status);
        stateLabel.setOpaque(true);
        if (status.equals("Up")){
            stateLabel.setBackground(Color.GREEN);
        }
        if (status.equals("Down")){
            stateLabel.setBackground(Color.red);
        }   
        stateLabel.setPreferredSize(new Dimension(20, 25));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setPreferredSize(new Dimension(400,400));
        frame.getContentPane().add(panel);
        frame.getContentPane().add(ipLabel, BorderLayout.CENTER);
        frame.getContentPane().add(stateLabel, BorderLayout.AFTER_LAST_LINE);
        frame.pack();
        frame.setVisible(true);
        return frame;
    }
    public static void editFrame(JFrame frame,String status,String ipAdd){
        frame.setContentPane(new JPanel());
        JPanel panel = new JPanel();
        JLabel ipLabel = new JLabel(ipAdd);
        ipLabel.setOpaque(true);
        ipLabel.setBackground(Color.white);
        ipLabel.setPreferredSize(new Dimension(20, 25));
        JLabel stateLabel = new JLabel(status);
        stateLabel.setOpaque(true);
        if (status.equals("Up")){
            stateLabel.setBackground(Color.GREEN);
        }
        if (status.equals("Down")){
            stateLabel.setBackground(Color.red);
        }   
        stateLabel.setPreferredSize(new Dimension(20, 25));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setPreferredSize(new Dimension(400,400));
        frame.getContentPane().add(panel);
        frame.getContentPane().add(ipLabel, BorderLayout.CENTER);
        frame.getContentPane().add(stateLabel, BorderLayout.AFTER_LAST_LINE);
    }
}

祝你的主机状态程序好运!

最新更新