2012-09-18 124 views
1

好吧,我已經有一些關於使用splitpane將我的框架分成兩個區域的提示,但我無法設法讓它顯示出有用的東西。代碼如下所示:與JSplitPane奮鬥

public class Whiteboard extends JPanel { 

int width = 600; 
int sidePanelWidth = 200; 
int lineHeight = 120; 
int numberOfLines = 5; 
JFrame frame = null; 
Glyph glyph = null; 
//java.awt.Rectangle bounds = new java.awt.Rectangle(); 
Bounds bounds = null; 
JSplitPane splitPane = null; 
JPanel tools = null; 


public Whiteboard() { 
    frame = new JFrame(); 
    frame.setSize(width + sidePanelWidth, getFullHeight()); 
    FlowLayout simpleLayout = new FlowLayout(); 
    frame.setLayout(simpleLayout); 

    tools = new JPanel(); 
    tools.setSize(new Dimension(sidePanelWidth, getFullHeight())); 
    this.setSize(width, getFullHeight()); 

    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this, tools); 
    splitPane.setPreferredSize(new Dimension(width + sidePanelWidth, getFullHeight())); 
    splitPane.setOneTouchExpandable(false); 
    splitPane.setDividerLocation(150); 

    frame.add(splitPane); 
    this.setBackground(Color.white); 
    java.awt.Rectangle rectBounds = this.getBounds(); 
    bounds = new Bounds((int)rectBounds.getX(), (int)rectBounds.getY(), (int)(rectBounds.getX() + rectBounds.getWidth()), (int)(rectBounds.getY() + rectBounds.getHeight())); 

} 

public int getFullHeight() { 
    return lineHeight * numberOfLines; 
} 

現在我改變了這樣的代碼:

public static void main(String[] args) { 
    int sidePanelWidth = 200; 

    JFrame frame = new JFrame();   
    Whiteboard whiteboard = new Whiteboard();   

    JPanel sidePanel = new JPanel(); 
    sidePanel.setPreferredSize(new Dimension(sidePanelWidth, whiteboard.getFullHeight())); 


    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 
    splitPane.add(whiteboard, JSplitPane.LEFT); 
    splitPane.add(sidePanel, JSplitPane.RIGHT); 

    frame.setLayout(new FlowLayout()); 
    frame.getContentPane().add(splitPane); 


    frame.pack(); 
    frame.setVisible(true);   
    whiteboard.repaint(); 
} 

而且構造這樣:

public Whiteboard() { 
    this.setPreferredSize(new Dimension(width, getFullHeight())); 
    this.setBackground(Color.red); 

}

現在我不不知道問題出在哪裏,也許是因爲它沒有調用paintComponent方法。我試圖通過調用repaint()它強制它,它並沒有幫助,它只是不稱爲這個組件

編輯:好吧,現在看起來它畢竟調用paintComponent方法,但我仍然得到這樣的屏幕: enter image description here

正如你所看到的,它看起來不太好。那麼我目前的主要方法代碼:

public static void main(String[] args) { 
    int sidePanelWidth = 200; 

    JFrame frame = new JFrame();   
    Whiteboard whiteboard = new Whiteboard();   

    JPanel sidePanel = new JPanel(); 
    sidePanel.setPreferredSize(new Dimension(sidePanelWidth, whiteboard.getFullHeight())); 


    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 
    splitPane.add(whiteboard, JSplitPane.LEFT); 
    splitPane.add(sidePanel, JSplitPane.RIGHT); 

    frame.setLayout(new FlowLayout()); 
    frame.getContentPane().add(splitPane); 

    frame.pack(); 
    frame.setVisible(true);   
    whiteboard.repaint(); 
} 

任何想法如何改變它來解決問題?我是否需要發佈其他方法?

+0

謝謝大家對您有所幫助。 看起來這一切都很好,我的代碼也沒問題,我想,這只是有別人一個不必要的構造函數調用,這會毀了一切。我想這一切都是因爲在開始時我的框架在JPanel實現中,這當然是一種錯誤的方式... –

回答

4

JPanel的構造函數中創建一個JFrame應該不會完成。

這裏是我創建了一個例子:

Screen shot

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

public class JavaApplication100 { 

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

      @Override 
      public void run() { 
       new JavaApplication100().createAndShowUI(); 
      } 
     }); 
    } 

    private void createAndShowUI() { 

     JFrame frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     initComponents(frame.getContentPane()); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    private void initComponents(Container contentPane) { 
     JPanel mainPanel = new JPanel(); 

     //create our 2 seperate panels (could be custom ones) 
     JPanel leftPanel = new JPanel(); 
     JPanel rightPanel = new JPanel(); 

     //add labels for viewing 
     leftPanel.add(new JLabel("LEFT")); 
     rightPanel.add(new JLabel("RIGHT")); 

     //just so you can see em or they would be small 
     leftPanel.setPreferredSize(new Dimension(300, 300)); 
     rightPanel.setPreferredSize(new Dimension(300, 300)); 

     JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 

     //add panels to split pane 
     jsp.add(rightPanel, JSplitPane.RIGHT); 
     jsp.add(leftPanel, JSplitPane.LEFT); 

     mainPanel.add(jsp);//add splitpane to mainpanel 
     contentPane.add(mainPanel); 

    } 
} 

編輯/ UPDATE:如果你要的顏色背景覆蓋paintComponent(Graphics g)WhiteBoard其中

按您的評論延伸JPanel像這樣:

public class WhiteBoard extends JPanel { 
@Override 
public void paintComponent(Graphics g) { 
super.paintComponent(g); 

Graphics2D g2 = (Graphics2D) g; 
g2.setColor(Color.red); 
g2.fillRect(0, 0, getWidth(), getHeight()); 
} 
} 
+0

好的,我會嘗試,但是我以前在主方法中創建框架時有負面經歷,結果是,我在框架上看不到任何東西,因爲面板永遠不會調用它們的paintComponent。 :/ –

+1

@ArturasM看我的最新 –

+0

嗯,我找不到有什麼問題,但對我來說,它似乎不起作用,不知何故,它看起來很尷尬,繪圖板也沒有畫出來。 –

3

你可以使用JSplitPane.setDividerLocation(int),而不是...

public class TestSplitPane extends JFrame { 

    public TestSplitPane() throws HeadlessException { 
     setSize(600, 600); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 

     setLayout(new BorderLayout()); 

     JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 

     splitPane.setLeftComponent(new JLabel("I'm on the left")); 
     splitPane.setRightComponent(new JLabel("I'm on the right")); 

     add(splitPane); 

     splitPane.setDividerLocation(200); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
//     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
         if ("Nimbus".equals(info.getName())) { 
          javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
          break; 
         } 
        } 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       new TestSplitPane().setVisible(true); 
      } 
     }); 
    } 
} 

enter image description here

+0

+1不錯的一個瘋狂的,只有改變我建議不擴展'JFrame',但我相信你已經知道 –

+0

@DavidKroukamp是的,這是一個簡單的例子;) – MadProgrammer