Swing控件可以实现水平翻转

开发 后端
本文介绍Swing控件可以实现水平翻转,有看到过阿拉伯国家软件的就知道他们的写作习惯是从右到左的形式。

Swing控件可以实现水平翻转,有看到过阿拉伯国家软件的就知道他们的写作习惯是从右到左的形式,SUN公司为了方便不同客户为Swing控件提供两个实现翻转效果的方法applyComponentOrientation(ComponentOrientationorientation)

设置此组件及其包含的所有组件的ComponentOrientation属性。setComponentOrientation(ComponentOrientationo)

设置语言敏感的方向,用于排序此组件内的元素或文本。

下面的代码建议在JDK1.6上面运行,Swing控件布局采用1.6的类

  1. importjava.awt.ComponentOrientation;  
  2. importjava.awt.event.ActionEvent;  
  3. importjava.awt.event.ActionListener;  
  4.  
  5. publicclassNewJFrameextendsjavax.swing.JFrame{  
  6.  
  7. publicNewJFrame(){  
  8. initComponents();  
  9. jButton1.setActionCommand("jbutton1");  
  10. jButton1.addActionListener(newJButton_ActionListener(this));  
  11. jButton2.setActionCommand("jbutton2");  
  12. jButton2.addActionListener(newJButton_ActionListener(this));  
  13. }  
  14.  
  15. //<editor-folddefaultstateeditor-folddefaultstate="collapsed"
    desc="GeneratedCode"> 
  16. privatevoidinitComponents(){  
  17.  
  18. jComboBox1=newjavax.swing.JComboBox();  
  19. jButton1=newjavax.swing.JButton();  
  20. jButton2=newjavax.swing.JButton();  
  21. jCheckBox1=newjavax.swing.JCheckBox();  
  22. jRadioButton1=newjavax.swing.JRadioButton();  
  23. jScrollPane1=newjavax.swing.JScrollPane();  
  24. jTree1=newjavax.swing.JTree();  
  25.  
  26. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
  27.  
  28. jComboBox1.setModel(newjavax.swing.DefaultComboBoxModel(newString[]
    {"Item1","Item2","Item3","Item4"}));  
  29.  
  30. jButton1.setText("jButton1");  
  31.  
  32. jButton2.setText("jButton2");  
  33.  
  34. jCheckBox1.setText("jCheckBox1");  
  35.  
  36. jRadioButton1.setText("jRadioButton1");  
  37.  
  38. jScrollPane1.setViewportView(jTree1);  
  39.  
  40. javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());  
  41. getContentPane().setLayout(layout);  
  42. layout.setHorizontalGroup(  
  43. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  44. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()  
  45. .addGap(22,22,22)  
  46. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)  
  47. .addComponent(jRadioButton1,javax.swing.GroupLayout.Alignment.LEADING,
    javax.swing.GroupLayout.DEFAULT_SIZE,174,Short.MAX_VALUE)  
  48. .addComponent(jCheckBox1,javax.swing.GroupLayout.Alignment.LEADING,
    javax.swing.GroupLayout.DEFAULT_SIZE,174,Short.MAX_VALUE)  
  49. .addComponent(jComboBox1,javax.swing.GroupLayout.Alignment.LEADING,
    0,174,Short.MAX_VALUE))  
  50. .addGap(29,29,29)  
  51. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.
    Alignment.LEADING,false)  
  52. .addGroup(layout.createSequentialGroup()  
  53. .addComponent(jButton1,javax.swing.GroupLayout.PREFERRED_SIZE,109,
    javax.swing.GroupLayout.PREFERRED_SIZE)  
  54. .addGap(26,26,26)  
  55. .addComponent(jButton2,javax.swing.GroupLayout.PREFERRED_SIZE,106,
    javax.swing.GroupLayout.PREFERRED_SIZE))  
  56. .addComponent(jScrollPane1))  
  57. .addGap(234,234,234))  
  58. );  
  59. layout.setVerticalGroup(  
  60. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  61. .addGroup(layout.createSequentialGroup()  
  62. .addContainerGap()  
  63. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.
    Alignment.TRAILING,false)  
  64. .addComponent(jButton2,javax.swing.GroupLayout.Alignment.LEADING,
    javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.
    DEFAULT_SIZE,Short.MAX_VALUE)  
  65. .addComponent(jButton1,javax.swing.GroupLayout.Alignment.LEADING,
    javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.
    DEFAULT_SIZE,Short.MAX_VALUE)  
  66. .addComponent(jComboBox1,javax.swing.GroupLayout.Alignment.LEADING,
    javax.swing.GroupLayout.DEFAULT_SIZE,37,Short.MAX_VALUE))  
  67. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  68. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  69. .addGroup(layout.createSequentialGroup()  
  70. .addComponent(jCheckBox1,javax.swing.GroupLayout.PREFERRED_SIZE,34,
    javax.swing.GroupLayout.PREFERRED_SIZE)  
  71. .addGap(18,18,18)  
  72. .addComponent(jRadioButton1,javax.swing.GroupLayout.PREFERRED_SIZE,35,
    javax.swing.GroupLayout.PREFERRED_SIZE))  
  73. .addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,250,
    javax.swing.GroupLayout.PREFERRED_SIZE))  
  74. .addContainerGap(86,Short.MAX_VALUE))  
  75. );  
  76.  
  77. pack();  
  78. }//</editor-fold> 
  79.  
  80. publicstaticvoidmain(Stringargs[]){  
  81.  
  82. finalNewJFramenewJFrame=newNewJFrame();  
  83.  
  84. java.awt.EventQueue.invokeLater(newRunnable(){  
  85. publicvoidrun(){  
  86. newJFrame.setVisible(true);  
  87. }  
  88. });  
  89. }  
  90.  
  91. publicvoidjButton1_ActionListener()  
  92. {  
  93. this.getContentPane().applyComponentOrientation
    (ComponentOrientation.LEFT_TO_RIGHT);  
  94. this.getRootPane().updateUI();  
  95. }  
  96.  
  97. publicvoidjButton2_ActionListener()  
  98. {  
  99. this.getContentPane().applyComponentOrientation
    (ComponentOrientation.RIGHT_TO_LEFT);  
  100. this.getRootPane().updateUI();  
  101. }  
  102.  
  103. //Variablesdeclaration-donotmodify  
  104. privatejavax.swing.JButtonjButton1;  
  105. privatejavax.swing.JButtonjButton2;  
  106. privatejavax.swing.JCheckBoxjCheckBox1;  
  107. privatejavax.swing.JComboBoxjComboBox1;  
  108. privatejavax.swing.JRadioButtonjRadioButton1;  
  109. privatejavax.swing.JScrollPanejScrollPane1;  
  110. privatejavax.swing.JTreejTree1;  
  111. //Endofvariablesdeclaration  
  112.  
  113. }  
  114.  
  115. classJButton_ActionListenerimplementsActionListener  
  116. {  
  117. privateNewJFramenewJFrame;  
  118. publicJButton_ActionListener(NewJFramenewJFrame)  
  119. {  
  120. this.newJFrame=newJFrame;  
  121. }  
  122. publicvoidactionPerformed(ActionEvente){  
  123. if("jbutton1".equals(e.getActionCommand()))  
  124. {  
  125. this.newJFrame.jButton1_ActionListener();  
  126. }elseif("jbutton2".equals(e.getActionCommand()))  
  127. {  
  128. this.newJFrame.jButton2_ActionListener();  
  129. }  
  130. }  

【编辑推荐】

  1. 浅谈学习Swing组件
  2. Swing使用invokeLater()方法
  3. 浅析Swing组件的规则
  4. 详解Swing中JTree组件的功能
  5. 简单介绍Swing外观
责任编辑:佚名 来源: 电子工业出版社
相关推荐

2009-07-10 09:58:08

Swing控件听觉反馈

2012-01-17 14:37:05

JavaSwing

2009-07-16 12:58:50

Swing控件

2022-08-23 08:01:09

CSS前端

2012-12-07 10:21:39

IBMdW

2009-07-10 13:55:48

Swing控件

2009-07-14 09:52:10

TableModelESwing

2009-07-14 15:39:34

Swing大多数控件

2009-07-10 14:26:28

实现SwingActionListe

2017-02-06 13:00:49

Android翻转卡片动画效果

2009-07-17 17:16:44

Swing实现的目标

2009-07-15 10:06:54

Swing实现MDI

2011-05-30 13:23:11

Android 动画

2012-01-17 14:29:38

JavaSwing

2021-10-21 17:55:06

物联网碳水平IOT

2009-07-15 15:35:59

Swing程序Swing性能

2009-07-16 08:53:03

Swing任务Swing线程

2009-07-10 13:36:32

Swing容器

2013-04-12 09:22:20

2015-03-23 16:32:20

点赞
收藏

51CTO技术栈公众号