2010-12-05 14 views
1

對於學校項目,我必須使用Java製作圖書館管理應用程序。由於最近出現了Reeder for Mac,我想我可能會嘗試使用未打孔的窗口重新創建界面(請參閱示例http://cl.ly/3VAn)。Swing窗口在Windows和Linux中任意添加邊距,但不是Mac

經過多次嘗試,我終於得到了它的基礎,在Mac上工作。 圖像轉到http://cl.ly/3VNU/ScreenshotMac.png

但在Windows和Ubuntu中,我得到了這個奇怪的餘量。 圖像轉到http://cl.ly/3V4I/ScreenshotLinux.png

窗口是一個帶有三個JPanel的JFrame,它們的paintComponent被覆蓋。 這裏是編譯好的.jar,http://cl.ly/3VCG來自己試試。 如果你認爲你需要源代碼,這裏是http://cl.ly/3VSz

在此先感謝!

+0

對不起,學習曲線實在太高,以幫助你在這裏。如果您修改了您的問題以包含您嘗試或檢查的內容列表,那麼您可能會獲得更多訪問者。你在使用佈局管理器嗎?如果是這樣,哪一個?你檢查過每個組件的界限嗎?那次測試的結果是什麼?等等。 – 2010-12-05 15:55:06

+0

您的gui是使用GUI構建器創建的。你將需要很多運氣和時間來修改它。更好的做法是從頭開始構建您的GUI,讓您更好地控制其創建。 – 2010-12-05 16:10:14

回答

1

幾件事情需要注意:

  1. 您只需要兩個面板一個鼠標處理程序。由於這可能會不必要地使工具欄中的事件處理複雜化,我會讓操作系統進行裝飾。

  2. 您應該本地化您的背景圖。

  3. 您可以根據需要使用setPreferredSize()並覆蓋getPreferredSize()

  4. main()方法應該建立在GUI「on the event dispatch thread.

  5. JFrame的默認佈局是BorderLayout一個組件之間沒有空隙。

下面是提出了一些原則的例子:

public class WelcomeWindow extends JFrame { 

    private ToolPanel top = new ToolPanel("/guiresources/BgTop.png"); 
    private PaperPanel middle = new PaperPanel("/guiresources/BgPaper.png"); 
    private ToolPanel bottom = new ToolPanel("/guiresources/BgBottom.png"); 

    public WelcomeWindow() throws IOException { 
     initComponents(); 
    } 

    private void initComponents() throws IOException { 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setUndecorated(true); 

     middle.setPreferredSize(new Dimension(640, 480)); 
     this.add(top, BorderLayout.NORTH); 
     this.add(middle, BorderLayout.CENTER); 
     this.add(bottom, BorderLayout.SOUTH); 

     MouseHandler mouseHandler = new MouseHandler(); 
     top.addMouseListener(mouseHandler); 
     top.addMouseMotionListener(mouseHandler); 
     bottom.addMouseListener(mouseHandler); 
     bottom.addMouseMotionListener(mouseHandler); 

     this.pack(); 
     this.setLocationRelativeTo(null); 
    } 

    private class MouseHandler extends MouseAdapter { 

     private Point point = new Point(); 

     @Override 
     public void mousePressed(MouseEvent e) { 
      point.x = e.getX(); 
      point.y = e.getY(); 
     } 

     @Override 
     public void mouseDragged(MouseEvent e) { 
      Point p = getLocation(); 
      setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y); 
     } 
    } 
} 

class PaperPanel extends JPanel { 

    protected Image image; 

    PaperPanel(String name) { 
     try { 
      image = ImageIO.read(getClass().getResource(name)); 
     } catch (IOException ex) { 
      ex.printStackTrace(System.err); 
     } 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 
    } 
} 

class ToolPanel extends PaperPanel { 

    ToolPanel(String name) { 
     super(name); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(image.getWidth(null), image.getHeight(null)); 
    } 
} 
相關問題