Swing控件可以实现水平翻转

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

创新互联建站是一家专注于网站设计、做网站与策划设计,乐陵网站建设哪家好?创新互联建站做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:乐陵等地区。乐陵做网站价格咨询:028-86922220

设置此组件及其包含的所有组件的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. }// 
  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外观

分享标题:Swing控件可以实现水平翻转
文章地址:http://www.shufengxianlan.com/qtweb/news40/209440.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联