我制作了一个程序来监控网络中的活动机器,并将IP地址完美地显示给JList(jList1)。当我单击列表中的一个IP地址然后右键单击鼠标时,将出现一个JPOupMenu,其菜单为“Shutdown”。当我尝试从弹出菜单中单击“关闭”菜单时显示错误,说:
Apr 14, 2011 1:57:07 PM UsernamePasswordPackage.NetCafeTime$9 actionPerformed SEVERE: null java.lang.RuntimeException: Unsupported operating system.
at UsernamePasswordPackage.NetCafeTime.shutdown(NetCafeTime.java:359)
at UsernamePasswordPackage.NetCafeTime$9.actionPerformed(NetCafeTime.java:444)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
我用来显示弹出菜单的代码:
public void showProjectMenu(MouseEvent e){
JPopupMenu menu = new JPopupMenu();
JMenuItem menuItem;
menuItem = new JMenuItem("Shutdown");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
//JOptionPane.showMessageDialog(null, "newline");
try {
shutdown();
} catch (RuntimeException ex) {
Logger.getLogger(NetCafeTime.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NetCafeTime.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
menu.add(menuItem);
menu.show(e.getComponent(),e.getX(),e.getY());
}
鼠标点击事件代码:
private void jList1MousePressed(java.awt.event.MouseEvent evt) {
if(SwingUtilities.isRightMouseButton(evt)&&!jList1.isSelectionEmpty()&&jList1.locationToIndex(evt.getPoint())==jList1.getSelectedIndex()){
showProjectMenu(evt);
}
}
private void jList1MouseReleased(java.awt.event.MouseEvent evt) {
if(SwingUtilities.isRightMouseButton(evt)&&!jList1.isSelectionEmpty()&&jList1.locationToIndex(evt.getPoint())==jList1.getSelectedIndex()){
showProjectMenu(evt);
}
}
我用来显示弹出菜单的代码:
public void showProjectMenu(MouseEvent e){
JPopupMenu menu = new JPopupMenu();
JMenuItem menuItem;
menuItem = new JMenuItem("Shutdown");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
//JOptionPane.showMessageDialog(null, "newline");
try {
shutdown();
} catch (RuntimeException ex) {
Logger.getLogger(NetCafeTime.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NetCafeTime.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
menu.add(menuItem);
menu.show(e.getComponent(),e.getX(),e.getY());
}
代码我以前通过它的IP地址关闭另一台PC:
public void shutdown()throws RuntimeException, IOException{
String shutdownCommand;
String operatingSystem = System.getProperty("os.name");
if("Linux".equals(operatingSystem)||"Mac OS X".equals(operatingSystem)){
shutdownCommand = "shutdown -h now";
}else if("windows".equals(operatingSystem)){
shutdownCommand = "shutdown.exe -s -t 0";
}else{
throw new RuntimeException("Unsupported operating system.");
}
Runtime.getRuntime().exec(shutdownCommand);
}
问题是,我无法关闭其他电脑......
非常感谢任何形式的帮助,谢谢...
答案 0 :(得分:2)
错误说明:"Unsupported operating system"
并且在您的关机方法中,这是RuntimeException
的消息,如果当前操作系统更新“Linux”,“Max OS X”,则抛出此消息“视窗”。所以你检查了System.getProperty("os.name")
实际返回的内容了吗?
编辑:重新阅读您的问题后,我想您想要使用所选的IP地址关闭机器。所以在你的关机方法中我认为你必须首先连接到那台机器并在那里执行shutdown
命令。这意味着您需要在要远程关闭的每台计算机上运行服务器程序。编写自己的服务器或在每个服务器上安装SSH服务器,然后使用JSch
答案 1 :(得分:2)
我建议您查看this example。据我所知String operatingSystem = System.getProperty("os.name");
,此代码在此示例中返回 Windows 2000 。