0
我有一個簡單的例子,我使用java.awt.Canvas
作爲JSplitPane中的組件,JSplitPane不允許調整。誰能解釋爲什麼? (更重要的是,如何解決)當我使用java.awt.Canvas時,JSplitPane不會調整大小?
VerticalSplit.java:
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
abstract public class VerticalSplit {
public VerticalSplit(JPanel panel)
{
panel.setLayout(new BorderLayout());
JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
createComponent1(), createComponent2());
jsp.setResizeWeight(0.5);
panel.add(jsp, BorderLayout.CENTER);
}
abstract protected Component createComponent1();
abstract protected Component createComponent2();
}
CanvasTest1.java:(如!@tulskiy建議 - 感謝)
import java.awt.Canvas;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CanvasTest1 extends VerticalSplit
{
static public class PlainCanvas extends Canvas
{
@Override public void paint(Graphics g)
{
Dimension d = getSize();
int h = getHeight();
int w = getWidth();
g.drawRect(0,0,w,h);
g.drawLine(0,0,w,h);
g.drawLine(0,h,w,0);
}
};
public CanvasTest1(JPanel panel) { super(panel); }
@Override protected Component createComponent1() {
return new PlainCanvas();
}
@Override protected Component createComponent2() {
return new PlainCanvas();
}
public static void main(String[] args) {
JPanel panel = new JPanel();
CanvasTest1 test = new CanvasTest1(panel);
JFrame frame = new JFrame(test.getClass().getName());
frame.setContentPane(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
[將JSplitPane與AWT組件一起使用]可能的重複(http://stackoverflow.com/questions/3999437/using-jsplitpane-with-an-awt-component) – dogbane 2011-05-26 13:48:28
嗯,我想是的,但這些答案不是似乎沒有幫助我。 – 2011-05-26 14:04:25
爲什麼你需要一個Canvas?你不能直接在JPanel上繪圖嗎? – 2011-05-26 14:06:06