簡體   English   中英

使用paintComponent()方法在JFrame中創建JPanel並繪制面板

[英]Creating a JPanel in a JFrame, and drawing the panel, using paintComponent() method

這是我第一次使用java Swing,並且我不知道如何在JSplitPane中的特定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);
        }
    }
}

您永遠不會將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();

}

現在顯示...

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM