2014-01-19 26 views
0

環繞按鈕我如何把圍繞這樣的COG在框架上邊界的一個按鈕:的Java如何把邊境/窗口或JFrame的

enter image description here

+1

您可以'setUndecorated在框架上(真)',只是實現自己的頂部。 –

+1

你要麼需要實現你自己的UI委託或未裝飾窗口並實現你的邊界 – MadProgrammer

+0

在任何地方有一個簡短的例子嗎? – ManInMoon

回答

1

_「是一個短的例子嗎?「

是的,這裏...這是非常基本的。你需要做更多的事情。你會注意到我必須添加一個MouseMotionListenerJPanel作爲最上面的框架邊框,因爲當你從框架中移除裝飾時,你也會拿走這個功能。所以MouseMotionListener使框架可以再次拖動。

如果您願意,您還必須實施調整大小。當您按下圖像時,我已經執行了System exit()`。測試一下。你需要提供你自己的形象。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.LineBorder; 

public class UndecoratedExample { 
    static JFrame frame = new JFrame(); 
    static class MainPanel extends JPanel { 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(400, 400); 
     } 
    } 

    static class BorderPanel extends JPanel { 

     JLabel stackLabel; 
     int pX, pY; 

     public BorderPanel() { 
      ImageIcon icon = new ImageIcon(getClass().getResource(
        "/resources/stackoverflow1.png")); 
      stackLabel = new JLabel(); 
      stackLabel.setIcon(icon); 

      setBackground(Color.black); 
      setLayout(new FlowLayout(FlowLayout.RIGHT)); 

      add(stackLabel); 

      stackLabel.addMouseListener(new MouseAdapter() { 
       public void mouseReleased(MouseEvent e) { 
        System.exit(0); 
       } 
      }); 
      addMouseListener(new MouseAdapter() { 
       public void mousePressed(MouseEvent me) { 
        // Get x,y and store them 
        pX = me.getX(); 
        pY = me.getY(); 
       } 
      }); 
      addMouseMotionListener(new MouseAdapter() { 
       public void mouseDragged(MouseEvent me) { 
        frame.setLocation(frame.getLocation().x + me.getX() - pX, 
          frame.getLocation().y + me.getY() - pY); 
       } 
      }); 
     } 
    } 

    static class OutsidePanel extends JPanel { 

     public OutsidePanel() { 
      setLayout(new BorderLayout()); 
      add(new MainPanel(), BorderLayout.CENTER); 
      add(new BorderPanel(), BorderLayout.PAGE_START); 
      setBorder(new LineBorder(Color.BLACK, 5)); 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 

       frame.setUndecorated(true); 
       frame.add(new OutsidePanel()); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

enter image description here

+0

謝謝你的一切。但哇,在Frame上添加按鈕需要付出很多努力。這絕對沒有簡單的方法嗎? – ManInMoon