2012-07-02 50 views
0
if (Desktop.isDesktopSupported()) 
    { 
     Desktop.getDesktop().browse(URI.create(URL)); 
    } 

我該如何編輯在此處打開的窗口的位置和大小?它將需要有可能做跨平臺。如果沒有跨平臺的解決方案,那麼Desktop適用於什麼解決方案?修改從Desktop.Browser打開的窗口的位置和位置

(這是遠走高飛的問題,但我也放大到網頁的一部分?)

(也關的問題,但如果有另一種方式來打開的窗口,與包括手柄,怎麼可以這樣做嗎?)

+1

我知道一個平臺相關的解決方案,但只適用於Windows,它需要JNA或JNI。 –

+0

我需要它是跨平臺的,但我對你的方法很感興趣。 –

+0

感謝編輯Dimo。 –

回答

2

從我的經驗來看,getDesktop().browse(URI.create(URL));在Mac上無法正常工作,所以即使您可以找到相當於Mac的@Hovercraft Full Eels解決方案,也無所謂。

我還沒有在Mac上測試過使用Java 7.1的桌面,但它沒有與以前的Java版本兼容。

如果您真的想跨平臺,我認爲您需要使用JavaFX中的WebView - JavaFX現在可以在Mac上使用,它是一個功能齊全的瀏覽器。有很多關於將JavaFX與Swing混合的教程,我認爲[JavaFX示例]包含了webView。 1

+0

這是否也工作在LWJGL(似乎無法通過谷歌搜索找到答案) –

+1

LWJGL是純Java的,對不對?如果客戶端桌面安裝了JavaFX運行時(現在可以自動識別最新的Java 7下載),您可以從Java應用程序啓動帶有Web視圖的JavaFX場景並將其指向您選擇的網頁。 – Thorn

+0

不知道,我是從C#背景進入遊戲「我的世界」修改。我不知道所有關於它的信息,但我會試試這個。謝謝您的幫助。 –

2

這就是我在Windows平臺設置中移動和重新調整Windows大小的方式。這是我的一些USER32 JNA的Windows代碼庫的一小部分:

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 

import javax.swing.*; 

import com.sun.jna.Native; 
import com.sun.jna.Pointer; 
import com.sun.jna.win32.StdCallLibrary; 

@SuppressWarnings("serial") 
public class TestMoveWindow extends JPanel { 
    public static final String[] BOUNDS_NAMES = { "x", "y", "w", "h" }; 
    private JTextField windowNameField = new JTextField(15); 
    private JTextField[] boundsFields = new JTextField[BOUNDS_NAMES.length]; 

    public TestMoveWindow() { 
     JPanel winNamePanel = new JPanel(); 
     winNamePanel.add(new JLabel("Window Name:")); 
     winNamePanel.add(windowNameField); 

     JPanel boundsPanel = new JPanel(new GridLayout(1, 0, 5, 0)); 
     for (int i = 0; i < BOUNDS_NAMES.length; i++) { 
     JPanel fieldPanel = new JPanel(); 
     fieldPanel.add(new JLabel(BOUNDS_NAMES[i] + ":")); 
     boundsFields[i] = new JTextField(3); 
     fieldPanel.add(boundsFields[i]); 
     boundsPanel.add(fieldPanel); 
     } 

     JPanel btnPanel = new JPanel(); 

     Action doItAction = new AbstractAction("Do it!") { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      String startOfWindowName = windowNameField.getText().trim(); 
      Pointer hWnd = JnaUtil.getWinHwnd(startOfWindowName); 
      if (hWnd == null || startOfWindowName.isEmpty()) { 
       String message = String.format(
        "Window named \"%s\" was not found", startOfWindowName); 
       JOptionPane.showMessageDialog(TestMoveWindow.this, message, 
        "Window Not Found", JOptionPane.ERROR_MESSAGE); 
       return; 
      } 
      int x = 0; 
      int y = 0; 
      int w = 0; 
      int h = 0; 

      String xStr = boundsFields[0].getText(); 
      String yStr = boundsFields[1].getText(); 
      String wStr = boundsFields[2].getText(); 
      String hStr = boundsFields[3].getText(); 
      try { 
       x = Integer.parseInt(xStr); 
       y = Integer.parseInt(yStr); 
       w = Integer.parseInt(wStr); 
       h = Integer.parseInt(hStr); 
      } catch (NumberFormatException e1) { 
       String message = String 
        .format(
          "Numbers cannot be parsed: \"%s\", \"%s\", \"%s\", \"%s\"", 
          xStr, yStr, wStr, hStr); 
       JOptionPane.showMessageDialog(TestMoveWindow.this, message, 
        "Numbers Not Parseable", JOptionPane.ERROR_MESSAGE); 
       return; 
      } 

      JnaUtil.moveWindow(hWnd, x, y, w, h); 

     } 
     }; 
     btnPanel.add(new JButton(doItAction)); 

     windowNameField.addActionListener(doItAction); 
     for (JTextField boundField : boundsFields) { 
     boundField.addActionListener(doItAction); 
     } 

     setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); 
     add(winNamePanel); 
     add(boundsPanel); 
     add(btnPanel); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("Test Move Window"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new TestMoveWindow()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 

} 

class JnaUtil { 
    private static final User32 user32 = User32.INSTANCE; 
    private static Pointer callBackHwnd; 

    public static Pointer getWinHwnd(final String startOfWindowName) { 
     callBackHwnd = null; 

     user32.EnumWindows(new User32.WNDENUMPROC() { 
     @Override 
     public boolean callback(Pointer hWnd, Pointer userData) { 
      byte[] windowText = new byte[512]; 
      user32.GetWindowTextA(hWnd, windowText, 512); 
      String wText = Native.toString(windowText).trim(); 

      if (!wText.isEmpty() && wText.startsWith(startOfWindowName)) { 
       callBackHwnd = hWnd; 
       return false; 
      } 
      return true; 
     } 
     }, null); 
     return callBackHwnd; 
    } 

    public static boolean moveWindow(Pointer hWnd, int x, int y, int nWidth, 
     int nHeight) { 
     boolean bRepaint = true; 
     return user32.MoveWindow(hWnd, x, y, nWidth, nHeight, bRepaint); 
    } 

} 

interface User32 extends StdCallLibrary { 
    User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class); 

    interface WNDENUMPROC extends StdCallCallback { 
     boolean callback(Pointer hWnd, Pointer arg); 
    } 

    boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer userData); 

    boolean MoveWindow(Pointer hWnd, int x, int y, int nWidth, int nHeight, 
     boolean bRepaint); 

    int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount); 

} 

這就要求你必須從你的classpath的JNA庫jna.jar和platform.jar。而且,這僅適用於Windows平臺。代碼的第一部分,TestMoveWindow只是一個用於演示程序如何工作的測試GUI,它實際上只不過是對JnaUtil.moveWindow(hWnd, x, y, w, h);的調用,但代碼的「肉」是我的JnaUtil類和User32接口。 JNA圖書館做這個所以容易,這是荒謬的。爲了讓我的代碼正常工作,您還需要知道窗口文本的開始 - 通常是網頁的標題 - 以便它可以獲得指向窗口的指針。

我真的不知道是否存在任何跨平臺的解決方案。我想認爲有,但我有點懷疑,因爲我相信這總是需要系統調用,但我很想證明是錯誤的!

+0

即使沒有簡單的跨平臺解決方案存在,每個主要操作系統(Linux,Mac,Windows)的解決方案應該存在,是否正確? –

+1

@Gavin:絕對是!但不要試圖問我*任何有關Unix,Mac或Windows以外的任何東西。 :) –