Vor kurzem habe ich ein Problem begegnet, dass die paintcomponent Funktion nicht in der Funktion aufgerufen wird, und ich fand, dass, wenn ich splitpane Funktion zu verwenden, wird es das Deaktivieren male Funktion und gibt Fehler:Wie in einem Split-Panel in Java Swing malen
cannot add to layout: unknown constraint: null
ich die Farbe Funktion denken nicht an die richtigen Art und Weise hinzugefügt werden kann, unter meinem Code ist (teilweise):
Klasse: Test
public class Test extends JFrame{
public Test() throws IOException{
//JFrame jf = new JFrame("my frame");
this.add(new NewPanel(this));
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.setBounds(300,200,1050,600);
this.setVisible (true);
}
public static void main (String[] args) throws IOException{
Test test = new Test();
test.setTitle("Hello");
//frame.pack();
}
}
Klasse: newpanel
public class NewPanel extends JPanel{
public NewPanel(JFrame frame) throws IOException{
JTabbedPane jTabbedpane = new JTabbedPane();
JSplitPane splitPane = new JSplitPane();
JPanel p1 = new JPanel();
p1.setLayout(null);
p2.setLayout(new FlowLayout());
splitPane.setOneTouchExpandable(true);
splitPane.setContinuousLayout(true);
//splitPane.setPreferredSize(new Dimension (250,500));
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane.setLeftComponent(p1);
splitPane.setRightComponent (p2);
splitPane.setDividerSize(3);
splitPane.setDividerLocation(250); //balance two panels width
jTabbedpane.addTab("ABC",p2);
jTabbedpane.addTab("AB",p3);
jTabbedpane.addTab("AC",p4);
jTabbedpane.addTab("BC",p5);
frame.setContentPane(splitPane);
frame.add(jTabbedpane);
}
}
public void paintComponent(Graphics g){
super.paint(g);
g.setColor(Color.BLUE);
g.drawLine(303, 90, 303, 200);
g.drawLine(583, 90, 583, 200);
g.drawLine(863, 90, 863, 200);
}
}
Als ich frame.add(jTabbedpane)
Kommentar kann die Linie in der Platte gezogen könnte, ABER es ist nur in einem Panel zur Verfügung, ich kann es in einem anderen Split-Panel nicht zeichnen, I don‘ Ich weiß, warum ... Und wenn ich frame.add(jTabbedpane)
auskommentiere, erscheint der oben erwähnte Fehler.