2015-11-06 105 views
3
loadingLab=new JLabel("The name is being saved.."); 
loadPanel.add(loadingLab); 
submitBttn=new JButton("Submit"); 
submitBttn.addActionListener(new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     System.out.println("Submit Button Clicked!!"); 
     try { 
      //something is wrong in here as it throws an exception 
      //what is wrong? 
      frame.setUndecorated(false); 
      frame.setOpacity(0.55f); 

      //when above both lines are commented, the code works fine 
      //but doesnt have transparency 
      frame.add(loadPanel,BorderLayout.SOUTH); 
      frame.setVisible(true); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

    } 
}); 

我想顯示透明JFrame當「提交」按鈕時,顯示有JLabel面板... 我一直在使用setOpacity(0.55f)嘗試,但它拋出異常..我到底做錯了什麼?點擊「提交」按鈕時如何設置半透明jframe?

+4

1)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 2)總是複製/粘貼錯誤和異常輸出! –

+0

爲了達到這個目的,框架必須是未修飾的(你做過'frame.setUndecorated(false);')。另外,如果你在actionlistener中使現有的框架不被修飾,你必須在它之前調用'frame.dispose()'(在它之後調用'frame.setVisible(true)') –

+0

@LuxxMiner thanx以獲得建議.... – Programmer007

回答

2

不幸的是,我認爲有沒有辦法讓系統窗口的裝飾,你可能會去與默認的。由於我不是100%確定是否要切換整個框架的不透明度或只是框架的背景,因此我在示例中包含了這兩個函數。 (mKorbels回答對您有幫助更多的,如果你不希望有一個飾)

代碼:

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JToggleButton; 

public class TransparentExample extends JFrame { 

    public TransparentExample() { 

     super("TransparentExample"); 
     Color defaultBackground = getBackground(); 
     float defaultOpacity = getOpacity(); 

     JToggleButton button1 = new JToggleButton("Toggle background transparency"); 
     button1.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (button1.isSelected()) { 
        setBackground(new Color(defaultBackground.getRed(), defaultBackground.getGreen(), 
          defaultBackground.getBlue(), 150)); 
       } else { 
        setBackground(defaultBackground); 
       } 
      } 
     }); 

     JToggleButton button2 = new JToggleButton("Toggle opacity of whole frame"); 
     button2.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       dispose(); 
       if (button2.isSelected()) { 
        setOpacity(0.55f); 
       } else { 
        setOpacity(defaultOpacity); 
       } 
       setVisible(true); 
      } 
     }); 

     getContentPane().setLayout(new FlowLayout()); 
     getContentPane().add(button1); 
     getContentPane().add(button2); 
     setSize(800, 600); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame.setDefaultLookAndFeelDecorated(true); 
       TransparentExample frame = new TransparentExample(); 
       frame.setVisible(true); 
      } 
     }); 
    } 

} 

沒有切換按鈕幀的圖片中選擇: enter image description here

與第一幀的圖片togglebutton selected: enter image description here

選中第二個togglebutton的幀的圖片: enter image description here

+0

非常感謝...我一直在尋找第二個togglebutton(設置不透明度)..我的另一個問題是,你如何使它的面板不透明背景半透明 – Programmer007

+0

@ Programmer007那麼,如果你不介意裝飾不是半透明的,你可以用第一個按鈕來代替'setBackground(new Color(0,0,0,0));'用'setBackground(new Color(defaultBackground.getRed(),defaultBackground .getGreen(),defaultBackground.getBlue(),150));'。這會讓背景變得半透明,但是每個組件(按鈕)都是完全可見的(包括一個添加的面板,只要你沒有調用'setOpaque(false)'就可以)。 –

+0

半透明的工作...現在將在不透明的面板上工作 – Programmer007

1

@ Programmer007寫道 - 例外的是 「 java.awt.IllegalComponentStateException:幀顯示的。」

  • 請在那裏我看不到任何,更多信息about the possible exceptions to read

  • 提到不知道,一切都是關於你的努力,轉化爲SSCCE/MCVE,短,可運行, compilable

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JDialog; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 

public class GenericForm extends JDialog { 

    private static final long serialVersionUID = 1L; 
    private Timer timer; 
    private JDialog dialog = new JDialog(); 
    private int count = 0; 

    public GenericForm() { 
     dialog.setSize(400, 300); 
     dialog.setUndecorated(true); 
     dialog.setOpacity(0.5f); 
     dialog.setName("Toggling with opacity"); 
     dialog.getContentPane().setBackground(Color.RED); 
     dialog.setLocation(150, 150); 
     dialog.setVisible(true); 
     timer = new javax.swing.Timer(1500, updateCol()); 
     timer.setRepeats(true); 
     timer.start(); 
    } 

    private Action updateCol() { 
     return new AbstractAction("Hello World") { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       boolean bol = dialog.getOpacity() < 0.55f; 
       count += 1; 
       if (count < 10) { 
        if (bol) { 
         dialog.setOpacity(1.0f); 
         dialog.getContentPane().setBackground(Color.WHITE); 
        } else { 
         dialog.setOpacity(0.5f); 
         dialog.getContentPane().setBackground(Color.RED); 
        } 
       } else { 
        System.exit(0); 
       } 
      } 
     }; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new GenericForm(); 
      } 
     }); 
    } 
}