2014-03-25 30 views
1

下面的例子将显示一个带有jframe窗口的按钮。我只想要按钮可见,它如何实现?如何只显示带有输出jframe或jpanel的按钮可见?

public final void initUI() { 

    JPanel panel = new JPanel(); 
    getContentPane().add(panel); 

    panel.setLayout(null); 

    JButton quitButton = new JButton("Quit"); 
    quitButton.setBounds(50, 60, 80, 30); 
    quitButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      System.exit(0); 
     } 
    }); 

    panel.add(quitButton); 

    setTitle("Quit button"); 
    setSize(300, 200); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
} 
+1

无框 - >无按钮 – Gee858eeG

+1

+1哦..那么人们在图像上显示按钮的常见做法是什么(启动画面,.jpg等)? –

回答

3

取决于你的意思是什么“带出jframe或jpanel可见?”您创建一个透明的窗口...

Boo

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class GhostButton { 

    public static void main(String[] args) { 
     new GhostButton(); 
    } 

    public GhostButton() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JButton ghostButton = new JButton("Boo!"); 
       ghostButton.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         System.exit(0); 
        } 
       }); 

       JFrame frame = new JFrame("Testing"); 
       frame.setUndecorated(true); 
       frame.setBackground(new Color(0,0,0,0)); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(ghostButton); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

} 

如果删除frame.setBackground(new Color(0,0,0,0));,你会得到一个无框窗

PS-这下的Java 7+的作品,有技巧,使它在Java 6下工作,但我没有在这里发布

+0

是的,我支持这个答案。在几分钟内对你的答案进行评分 –