gpt4 book ai didi

java - 远程桌面关闭

转载 作者:行者123 更新时间:2023-12-01 19:18:13 26 4
gpt4 key购买 nike

我编写了一个程序来监视联网位置中的 Activity 计算机,并完美地将 IP 地址显示到 JList(jList1)。当我单击列表中的 IP 地址之一,然后右键单击鼠标时,将出现一个 JPopupMenu,其中包含“关机”菜单。当我尝试从弹出菜单中单击“关闭”菜单时,显示错误:

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 地址关闭另一台电脑的代码:

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);
}

问题是,我无法关闭另一台电脑...

任何形式的帮助将不胜感激,谢谢......

最佳答案

错误显示:“不支持的操作系统”,在您的关闭方法中,这是 RuntimeException 的消息,如果当前操作系统较近,则会抛出该消息“Linux”、“Max OS X”,也不是“Windows”。那么您检查过 System.getProperty("os.name") 实际返回的内容吗?

编辑:重新阅读您的问题后,我认为您想要关闭具有所选 IP 地址的计算机。因此,在您的关闭方法中,我认为您必须首先连接到该计算机并在那里执行 shutdown 命令。这意味着您需要在要远程关闭的每台计算机上运行服务器程序。要么编写自己的服务器,要么在每台服务器上安装 SSH 服务器并使用 JSch 连接到计算机。

关于java - 远程桌面关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5659292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com