2013-02-14 73 views
0

有一個窗口中我想放置一個按鈕,然後繪製它下面的整個區域。換句話說,按鈕應該覆蓋一幅畫。在繪畫上分層按鈕

import java.awt.*; 
import javax.swing.*; 

class Window 
{ 
    private JFrame frame; 
    private JButton launchButton; 
    private JPanel pnllaunchButton; 
    private JPanel paintingPanel; 
    //width and height of client area 
    private Rectangle dim; 

    //w,h - width and height of the whole window 
    public Window(String title,int w,int h) 
    { 
     frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocation(0,0); 
     frame.setMinimumSize(new Dimension(110,110)); 
     frame.setSize(w, h); 
     frame.setVisible(true); 
     frame.setTitle(title);   
     frame.setResizable(false); 

     addLaunchButton(); 
    }   

    private void addLaunchButton() 
    { 
     pnllaunchButton = new JPanel(); 
     launchButton = new JButton("Plot!"); 
     dim = new Rectangle(); 

     frame.getContentPane().getBounds(dim); 

     pnllaunchButton.setBounds(dim.width-100,dim.height-25,100,25); 

     launchButton.setBounds(dim.width-100,dim.height-25,100,25); 

     pnllaunchButton.setLayout(null); 

     pnllaunchButton.add(launchButton); 

     frame.getContentPane().add(pnllaunchButton);   
     frame.getContentPane().setComponentZOrder(pnllaunchButton, new Integer(2));  
    }  
    public void drawCoordinateSystem() 
    {  
     paintingPanel = new JPanel(); 

     paintingPanel.add(new CoordinateSystem()); 

     frame.getContentPane().add(paintingPanel); 
     frame.getContentPane().setComponentZOrder(paintingPanel,new Integer(3)); 

    } 

} 

class CoordinateSystem extends JPanel 
{ 
    @Override 
    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     Dimension size = this.getSize(); 

     g.setColor(Color.BLACK); 
     g.drawLine(0,size.height/2,size.width, size.height/2); 

     g.drawLine(size.width/2, 0, size.width/2, size.height); 

    } 
}   

public class GC { 

    public static void main(String[] args) 
    { 
     Window h = new Window("GC",800,600);   
     h.drawCoordinateSystem();  
    } 
} 

此代碼不符合規範。程序創建一個空的窗口並輸出:

run: 
Exception in thread "main" java.lang.IllegalArgumentException: illegal component position 
    at java.awt.Container.checkAdding(Container.java:504) 
    at java.awt.Container.setComponentZOrder(Container.java:759) 
    at Window.addLaunchButton(Window.java:46) 
    at Window.<init>(Window.java:26) 
    at GC.main(GC.java:10) 

你能指出我的錯誤嗎?方法似乎沒有在javadoc中精確描述。

+2

所以,而不是旋轉座標和圖層,爲什麼不只是添加按鈕到背景的面板/組件?另外請記住,Z順序是0索引,只能在'0'和'componentCount - 1'之間的值。 – MadProgrammer 2013-02-14 01:34:06

回答

5
  • 重命名你的班級。 Window類已經是標準核心Java庫的一部分,並且您的類名可能會導致當前或未來的問題。
  • 不要我們null佈局和setBounds(...)。這是壞的,壞的,不好的,並且會使你很難維護或升級你的應用程序。相反,瞭解並使用佈局管理器。
  • 考慮將JLabel設置爲contentPane,使其變爲不透明,給它一個佈局和一個ImageIcon並將其添加到它。
  • ImageIcon可以用網格容納一個BufferedImage。

例如:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.GradientPaint; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 

import javax.swing.*; 

public class MyWindow { 
    private static final int PREF_W = 800; 
    private static final int PREF_H = 600; 
    private static final Color COLOR0 = Color.red; 
    private static final Color COLOR1 = Color.blue; 
    private static final float COLOR_REPEAT_DIST = 30f; 
    private JLabel backGroundLabel = new JLabel(); 

    public MyWindow() { 
     backGroundLabel.setOpaque(true); 
     backGroundLabel.setLayout(new BorderLayout()); 
     int eb = 15; 
     BufferedImage bkgrndImg = createBkgrndImage(); 
     ImageIcon icon = new ImageIcon(bkgrndImg); 
     backGroundLabel.setIcon(icon); 

     JPanel bottomPanel = new JPanel(); 
     bottomPanel.setLayout(new FlowLayout(SwingConstants.RIGHT, eb, eb)); 
     bottomPanel.setOpaque(false); 
     bottomPanel.add(new JButton("Plot")); 
     backGroundLabel.add(bottomPanel, BorderLayout.PAGE_END); 
    } 

    private BufferedImage createBkgrndImage() { 
     BufferedImage img = new BufferedImage(PREF_W, PREF_H, BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2 = img.createGraphics(); 
     g2.setPaint(new GradientPaint(0f, 0f, COLOR0, COLOR_REPEAT_DIST, COLOR_REPEAT_DIST, COLOR1, true)); 
     g2.fillRect(0, 0, PREF_W, PREF_H); 
     g2.dispose(); 
     return img; 
    } 

    public JComponent getMainPane() { 
     return backGroundLabel; 
    } 

    private static void createAndShowGui() { 
     MyWindow mainPanel = new MyWindow(); 

     JFrame frame = new JFrame("MyWindow"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel.getMainPane()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

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

它看起來像這樣:
enter image description here

+1

男人,你的答案總是延伸和幫助。爲你+1。 – 2013-02-14 01:42:43

+1

+1很酷的繪畫。 – camickr 2013-02-14 04:31:34

1

傳遞一個int而不是IntegersetComponentZOrder方法。

private void addLaunchButton() 
{ 
    pnllaunchButton = new JPanel(); 
    launchButton = new JButton("Plot!"); 
    dim = new Rectangle(); 

    frame.getContentPane().getBounds(dim); 

    pnllaunchButton.setBounds(dim.width-100,dim.height-25,100,25); 

    launchButton.setBounds(dim.width-100,dim.height-25,100,25); 

    pnllaunchButton.setLayout(null); 


    pnllaunchButton.add(launchButton); 

    frame.getContentPane().add(pnllaunchButton);  
    frame.getContentPane().setComponentZOrder(pnllaunchButton, 2);  
} 
+0

什麼都沒有發生 – 0x6B6F77616C74 2013-02-14 20:46:22