Swing控件可以实现水平翻转,有看到过阿拉伯国家软件的就知道他们的写作习惯是从右到左的形式,SUN公司为了方便不同客户为Swing控件提供两个实现翻转效果的方法applyComponentOrientation(ComponentOrientationorientation)
设置此组件及其包含的所有组件的ComponentOrientation属性。setComponentOrientation(ComponentOrientationo)
设置语言敏感的方向,用于排序此组件内的元素或文本。
下面的代码建议在JDK1.6上面运行,Swing控件布局采用1.6的类
- importjava.awt.ComponentOrientation;
- importjava.awt.event.ActionEvent;
- importjava.awt.event.ActionListener;
- publicclassNewJFrameextendsjavax.swing.JFrame{
- publicNewJFrame(){
- initComponents();
- jButton1.setActionCommand("jbutton1");
- jButton1.addActionListener(newJButton_ActionListener(this));
- jButton2.setActionCommand("jbutton2");
- jButton2.addActionListener(newJButton_ActionListener(this));
- }
- //<editor-folddefaultstateeditor-folddefaultstate="collapsed"
desc="GeneratedCode">- privatevoidinitComponents(){
- jComboBox1=newjavax.swing.JComboBox();
- jButton1=newjavax.swing.JButton();
- jButton2=newjavax.swing.JButton();
- jCheckBox1=newjavax.swing.JCheckBox();
- jRadioButton1=newjavax.swing.JRadioButton();
- jScrollPane1=newjavax.swing.JScrollPane();
- jTree1=newjavax.swing.JTree();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jComboBox1.setModel(newjavax.swing.DefaultComboBoxModel(newString[]
{"Item1","Item2","Item3","Item4"}));- jButton1.setText("jButton1");
- jButton2.setText("jButton2");
- jCheckBox1.setText("jCheckBox1");
- jRadioButton1.setText("jRadioButton1");
- jScrollPane1.setViewportView(jTree1);
- javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
- .addGap(22,22,22)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jRadioButton1,javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE,174,Short.MAX_VALUE)- .addComponent(jCheckBox1,javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE,174,Short.MAX_VALUE)- .addComponent(jComboBox1,javax.swing.GroupLayout.Alignment.LEADING,
0,174,Short.MAX_VALUE))- .addGap(29,29,29)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.
Alignment.LEADING,false)- .addGroup(layout.createSequentialGroup()
- .addComponent(jButton1,javax.swing.GroupLayout.PREFERRED_SIZE,109,
javax.swing.GroupLayout.PREFERRED_SIZE)- .addGap(26,26,26)
- .addComponent(jButton2,javax.swing.GroupLayout.PREFERRED_SIZE,106,
javax.swing.GroupLayout.PREFERRED_SIZE))- .addComponent(jScrollPane1))
- .addGap(234,234,234))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.
Alignment.TRAILING,false)- .addComponent(jButton2,javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.
DEFAULT_SIZE,Short.MAX_VALUE)- .addComponent(jButton1,javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.
DEFAULT_SIZE,Short.MAX_VALUE)- .addComponent(jComboBox1,javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE,37,Short.MAX_VALUE))- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jCheckBox1,javax.swing.GroupLayout.PREFERRED_SIZE,34,
javax.swing.GroupLayout.PREFERRED_SIZE)- .addGap(18,18,18)
- .addComponent(jRadioButton1,javax.swing.GroupLayout.PREFERRED_SIZE,35,
javax.swing.GroupLayout.PREFERRED_SIZE))- .addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,250,
javax.swing.GroupLayout.PREFERRED_SIZE))- .addContainerGap(86,Short.MAX_VALUE))
- );
- pack();
- }//</editor-fold>
- publicstaticvoidmain(Stringargs[]){
- finalNewJFramenewJFrame=newNewJFrame();
- java.awt.EventQueue.invokeLater(newRunnable(){
- publicvoidrun(){
- newJFrame.setVisible(true);
- }
- });
- }
- publicvoidjButton1_ActionListener()
- {
- this.getContentPane().applyComponentOrientation
(ComponentOrientation.LEFT_TO_RIGHT);- this.getRootPane().updateUI();
- }
- publicvoidjButton2_ActionListener()
- {
- this.getContentPane().applyComponentOrientation
(ComponentOrientation.RIGHT_TO_LEFT);- this.getRootPane().updateUI();
- }
- //Variablesdeclaration-donotmodify
- privatejavax.swing.JButtonjButton1;
- privatejavax.swing.JButtonjButton2;
- privatejavax.swing.JCheckBoxjCheckBox1;
- privatejavax.swing.JComboBoxjComboBox1;
- privatejavax.swing.JRadioButtonjRadioButton1;
- privatejavax.swing.JScrollPanejScrollPane1;
- privatejavax.swing.JTreejTree1;
- //Endofvariablesdeclaration
- }
- classJButton_ActionListenerimplementsActionListener
- {
- privateNewJFramenewJFrame;
- publicJButton_ActionListener(NewJFramenewJFrame)
- {
- this.newJFrame=newJFrame;
- }
- publicvoidactionPerformed(ActionEvente){
- if("jbutton1".equals(e.getActionCommand()))
- {
- this.newJFrame.jButton1_ActionListener();
- }elseif("jbutton2".equals(e.getActionCommand()))
- {
- this.newJFrame.jButton2_ActionListener();
- }
- }
- }
【编辑推荐】