我正在尝试处理我的框架并创建一个新框架。新的一个被创建但旧的一个仍然存在。我最终在桌面上有两个框架。我错过了什么?感谢。
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。我住在爱荷华州,喜欢把我的空闲时间花在不受批评我帖子的红色箭盒的麻烦上。
答案 0 :(得分:1)
当你调用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);
}
}
祝你好主机状态好!