2015-10-14 110 views
0

這是我第一次使用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

回答

0

你從不添加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

相關問題