2015-11-06 124 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(); 
      } 
     }); 
    } 
}