This is my first time using java Swing, and I don't know how to paint in the specific JPanel
in JSplitPane
, and I try to create a new class to implement the paintComponent
method, but it can't be Override.
Can someone help me?
import javax.swing.*;
import java.awt.*;
public class SplitPane extends JPanel{
private JPanel mainPanel;
private JPanel leftPanel;
private JPanel rightPanel;
public SplitPane() {
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SplitPane().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
initComponents(frame.getContentPane());
frame.setVisible(true);
}
private void initComponents(Container contentPane) {
mainPanel = new JPanel();
leftPanel = new JPanel();
rightPanel = new JPanel();
leftPanel.add(new JLabel("left"));
rightPanel.add(new JLabel("right"));
leftPanel.setPreferredSize(new Dimension(200, 40));
rightPanel.setPreferredSize(new Dimension(280, 400));
leftPanel.setBackground(Color.WHITE);
rightPanel.setBackground(Color.WHITE);
JSplitPane mainJsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
mainJsp.add(leftPanel, JSplitPane.TOP);
mainJsp.add(rightPanel, JSplitPane.BOTTOM);
mainJsp.setOneTouchExpandable(true);
mainJsp.setDividerLocation(150);
mainPanel.add(mainJsp);
contentPane.add(mainPanel);
leftPanel = new PaintPanel();
}
public class PaintPanel extends JPanel {
public PaintPanel() {
System.out.println("PaintPanel");
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(300, 300));
}
@Override
public void paintComponent(Graphics g) {
System.out.println("12345678");
super.paintComponent(g);
//g.setColor(Color.black);
g.drawRect(3, 3, 20, 20);
}
}
}