好吧,我已經有一些關於使用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方法,但我仍然得到這樣的屏幕:
正如你所看到的,它看起來不太好。那麼我目前的主要方法代碼:
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();
}
任何想法如何改變它來解決問題?我是否需要發佈其他方法?
謝謝大家對您有所幫助。 看起來這一切都很好,我的代碼也沒問題,我想,這只是有別人一個不必要的構造函數調用,這會毀了一切。我想這一切都是因爲在開始時我的框架在JPanel實現中,這當然是一種錯誤的方式... –