2011-06-23 196 views
15

要對準我的JFrame右擊向左,我使用:從右向左設置JFrame方向!

setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 

,但是這隻有當我使用JFrame的以下樣式(飾):

public class RightToLeft { 
    public static void main(String []args){ 
    javax.swing.SwingUtilities.invokeLater(new Runnable(){ 
     public void run() { 
     try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } 
     catch (Exception e) { e.printStackTrace(); } 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     JFrame frame = new JFrame("العنوان بالعربي"); 
     frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     frame.setSize(300,300); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.setVisible(true); 
     } 
    }); 
    } 
} 

enter image description here

但我希望它沒有這種裝飾工作。如何解決這個問題?

編輯:

@mre我希望像這樣的一個JFrame:

enter image description here

EDIT2:

我真的真的需要這個問題是固定的,所以我提供500+誰給這樣的JFrame(與WindowsLookAndFeel):

enter image description here

+0

@ Eng.Fouad,我不知道我理解你的問題。這聽起來像你想要一個未裝飾的'JFrame' ... – mre

+0

@mre看到更新 –

+1

@ Eng.Fouad,然後,而不是加載跨平臺的外觀和感覺,爲什麼你不加載特定的外觀和感覺,你想要使用? – mre

回答

5

@ Eng.Fouad

只是玩笑而這個??? ??? ...

Substance L&F

代碼:

import java.awt.ComponentOrientation; 
import javax.swing.JFrame; 
import javax.swing.UIManager; 
import org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel; 

public class RightToLeft { 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
        UIManager.setLookAndFeel(new SubstanceOfficeSilver2007LookAndFeel()); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       JFrame.setDefaultLookAndFeelDecorated(true); 
       JFrame frame = new JFrame("العنوان بالعربي"); 
       frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
       frame.setSize(300, 300); 
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    private RightToLeft() { 
    } 
} 
+0

我在哪裏可以下載這個包「org.pushingpixels.substance.api.skin」? –

+0

@ Eng.Fouad來自舊的和原始包http://java.net/projects/substance/,但在今年分叉並繼續名爲Insubstantial https://github.com/Insubstantial(對於最難的更改,您有使用invokeAndWait而不是invokeLater http://stackoverflow.com/questions/6579087/gui-problem-with-java-substance-look-and-feel/6579925#6579925),這裏是L&F的有用almanach http:// stackoverflow .com/questions/3954616/java中的look-and-feel-in,JGoodies對CompoentOrientation有很好的實現,你必須嘗試一下,因爲我從未需要改變 – mKorbel

+0

請你簡單解釋一下如何使用這個包?我不熟悉使用pakages。我會給你+50的代表,因爲它已經快到期了,你的解決方案似乎滿足我想要的 –

1

它看起來像組件定向特徵與Windows的LookAndFeel(至少不是標題欄)

+2

標題欄與LAF無關 - 它是由操作系統提供。真的是,WindowsLAF不支持LAFDecoration(又名:而不是使用默認的OS窗口裝飾,讓LAF自己滾動) – kleopatra

3

我懷疑它與OS做更多的支持。通常(如果您不打電話給setDefaultLookAndFeelDecorated),它是提供框架裝飾的操作系統,而不是LAF。

您應該嘗試在操作系統中更改您的偏好設置,以表示您要從右至左定位。

對不起,我不知道這些設置會在哪裏。 一旦你這樣做了,那麼你應該能夠刪除對setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 的呼叫,因爲LAF將從語言環境中選擇操作系統設置。

編輯

This Link介紹如何在Windows 7中啓用從右到左文本,然後我認爲你還需要change your locale.

1

這裏是one posibility。此實用程序專爲Mac用戶切換到Windows並希望左側的窗口按鈕設計,但它應該與您的需求相同。

該解決方案與Java無關(所以我不知道它是否可以滿足您的需求),聽起來像是在您的應用程序外部。我沒有能夠自己嘗試(我沒有運行Windows),所以我不能擔保它,但它可能是值得一試。

+0

這很好,但我需要它與我的程序內部工作 –

7

下面介紹您通過您的代碼段觀察什麼:

ComponentOrientation只適用於擺幅(和AWT實際上)組件。

當使用JFrame.setDefaultLookAndFeelDecorated(true);時,整個幀裝飾是由Swing(實際上是LookAndFeel本身)執行的,所以這可以按預期工作。

如果您沒有設置此選項,那意味着操作系統負責框架裝飾,但操作系統無法知道您的Swing組件使用的ComponentOrientation

我希望操作系統(你有沒有提到你使用的操作系統是什麼?它似乎是Windows 7的權利?)根據當前選擇的區域設置執行正確的修飾。因此,如果你有一個阿拉伯語區域設置,並在你的操作系統上選擇,我想所有的窗口裝飾都是正確的。您是否嘗試更改該區域設置(通過「區域和語言」控制面板)?它有用嗎?

注意:我不認爲您可以直接從Java更改這些操作系統設置,但可以使用Locale.getDefault()來閱讀它們。

綜上所述:

  • 首先,你必須確保文本方向而言你的操作系統配置正確;對不起,我在這裏幫不了忙,因爲我的Windows機器上沒有安裝任何從右到左的語言。

  • 然後,使用該系統的外觀和感覺,並確保JFrame.setDefaultLookAndFeelDecorated(false);

  • 如果不工作,那麼你可以考慮發佈您的代碼段,您的系統配置到Oracle的Java漏洞列表一起

接下來是關於如何提高這部分代碼(但是這不是你的問題的修復程序)附加說明

乙y中的方式,如果你讓用戶定義其操作系統的語言首選項,那麼你應該沒有明確硬編碼frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);而是使用類似:

frame.applyComponentOrientation(
    ComponentOrientation.getOrientation(Locale.getDefault())); 

Locale.getDefault(),除非你的應用程序中明確改變,將返回目前選擇的操作系統級別爲Locale

另請注意,最好使用applyComponentOrientation()而不是setComponentOrientation(),因爲前者將給定方向遞歸設置爲組件的整個層次結構。

最後,您必須確保您的窗口中使用的LayoutManager是從右到左的意識;這通常適用於所有標準Swing佈局,但不適用於所有第三方佈局管理器,如果您使用一些佈局管理器。

+0

我試過'applyComponentOrientation (ComponentOrientation.getOrientation(Locale.getDefault()));'但它不起作用:\ –

+0

首先,你能確認你的OS語言環境是否是阿拉伯語?你可以添加'System.out.println(Locale.getDefault());'到你的代碼來確認。 – jfpoilpret

+0

輸出:'ar_SA' –