在JFrame中创建JPanel并使用paintComponent()方法绘制面板

问题描述:

这是我第一次使用java swing,并且我不知道如何在JSplitPaneJPanel中绘制,我尝试创建一个新类实现了paintComponent方法,但它不能被Override覆盖。 有人可以帮我吗?在JFrame中创建JPanel并使用paintComponent()方法绘制面板

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); 
     } 
    } 
} 
+0

public void paintComponents(Graphics g){'should be' public void paintComponent(Graphics g){'(No ** S **)。进一步的'public void paintComponent(Graphics g){..'应该是'public void paintComponent(Graphics g){super.paintComponent(g); ..' –

+1

另请参见[我应该避免使用Java Swing中的set(Preferred | Maximum | Minimum)大小方法?](http://stackoverflow.com/q/7229226/418556)(是) –

+0

谢谢!它仍然不起作用,我在paintComponent函数中使用了system.out.print,但是在控制台中没有打印出来,所以我认为这个函数不能被覆盖。 – mcgG

你从不添加PaintPanel任何东西,例如...

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(); 
    // Just left hanging here, never added to anything...? 

} 

所以,如果我将其更改为类似...

private void initComponents(Container contentPane) { 
    mainPanel = new JPanel(); 
    leftPanel = new PaintPanel(); 
    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(); 

} 

现在显示.. 。

enter image description here

+0

非常感谢!它现在有效 – mcgG