2014-02-15 40 views
2

我希望我的JFrame透明。我使用下面的代碼使其透明。它在Windows上運行良好,但在Linux上使所有JComponet透明。將JFrame設置爲透明使得所有的JComponet在Linux中都是透明的

這裏是JFrame的上窗口的外觀和linux Here is the look on windows and linux

我的代碼:

public class NewJFrame extends javax.swing.JFrame { 

/** 
* Creates new form NewJFrame 
*/ 
public NewJFrame() { 

    setUndecorated(true); 
    setBackground(new Color(0,0,0,0)); 
    initComponents();  

    jLabel1.setIcon(new ImageIcon(getClass().getResource("device_pc.png"))); 

} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents 
private void initComponents() { 

    jButton1 = new javax.swing.JButton(); 
    jButton2 = new javax.swing.JButton(); 
    jButton3 = new javax.swing.JButton(); 
    jLabel1 = new javax.swing.JLabel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jButton1.setText("jButton1"); 

    jButton2.setText("jButton2"); 

    jButton3.setText("jButton3"); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(128, 128, 128) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jButton3) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(jButton2) 
        .addGap(59, 59, 59) 
        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addComponent(jButton1)) 
      .addContainerGap(31, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(25, 25, 25) 
      .addComponent(jButton1) 
      .addGap(18, 18, 18) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jButton2) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addGap(18, 18, 18) 
      .addComponent(jButton3) 
      .addContainerGap(107, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold>//GEN-END:initComponents 

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new NewJFrame().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify//GEN-BEGIN:variables 
private javax.swing.JButton jButton1; 
private javax.swing.JButton jButton2; 
private javax.swing.JButton jButton3; 
private javax.swing.JLabel jLabel1; 
// End of variables declaration//GEN-END:variables 
} 
+0

嘗試並添加到JPanel上,並將面板不透明設置爲false,看看會發生什麼。 –

+0

試過。但沒有運氣。 – user3289782

+0

測試[this out](http://stackoverflow.com/a/21771615/2587435)看看這隻鳥是否是透明的 –

回答

0

只是一個想法,爲什麼不設置JButtons在晦澀真,比如果不工作,給他們每個人一個BG/FG顏色(也許使用自定義顏色,將alpha值設置爲100等)。

祝你好運!

Linux是最好的。