在多个JPanel中绘图

时间:2011-12-28 22:23:25

标签: java swing jframe jpanel paintcomponent

我在主框架内有三个JPanel。从左侧顺时针方向,在第一个面板中,我计划使用一些控制器来控制面板2上的图形。第三个底部面板将显示一些信息。

我的理解是,我必须覆盖paintComponent,以便我可以在第二个面板上实现所需的效果。现在我只想测试它,我是否可以在这个面板上绘制简单的文本。

但实际上,我在三个面板中的任何一个中都有问题。

下面给出了代码,我不知道是什么问题。

    public class Demo extends JFrame{
    private JSplitPane splitPaneV;
    private JSplitPane splitPaneH;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;

    public Demo(String string) {
        // TODO Auto-generated constructor stub
        setTitle(string);
        setBackground( Color.gray );

        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );

        // Create the panels
        new MyJPanel1();
        new MyJPanel2();
        new MyJPanel3();


        // Create a splitter pane
        splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
        topPanel.add( splitPaneV, BorderLayout.CENTER );

        splitPaneH = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT );
        splitPaneH.setLeftComponent( panel1 );
        splitPaneH.setRightComponent( panel2 );

        splitPaneV.setLeftComponent( splitPaneH );
        splitPaneV.setRightComponent( panel3 );
        /*setContentPane(new MyJPanel1());
        setContentPane(new MyJPanel2());
        setContentPane(new MyJPanel3());*/

    }

    private class MyJPanel1 extends JPanel{

        public MyJPanel1(){
            panel1 = new JPanel();
            panel1.setLayout( new BorderLayout() );
            panel1.setPreferredSize(new Dimension(200,500));
        }

        @Override 
        public void paintComponent(Graphics g) { 
            //super.paintComponent(g); 

            g.drawString("BLAH1", 20, 20); 
            g.drawRect(200, 200, 200, 200); 
            //setVisible(true);
        } 


    }

    private class MyJPanel2 extends JPanel{

        public MyJPanel2(){
            panel2 = new JPanel();
            panel2.setLayout( new FlowLayout() );
            panel2.setPreferredSize(new Dimension(1000,500));
        }

        @Override 
        public void paintComponent(Graphics g) {
            super.paintComponent(g); 

            g.drawString("BLAH2", 20, 20); 
            g.drawRect(200, 200, 200, 200); 
    } 


    }


    private class MyJPanel3 extends JPanel{

        public MyJPanel3(){
            panel3 = new JPanel();
            panel3.setLayout( new BorderLayout() );
            panel3.setPreferredSize( new Dimension( 400, 100 ) );
            panel3.setMinimumSize( new Dimension( 100, 50 ) );

        }

        @Override 
        public void paintComponent(Graphics g) { 
            //super.paintComponent(g); 

            g.drawString("BLAH3", 20, 20); 
            g.drawRect(200, 200, 200, 200); 
        } 


    }




    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        Demo frame = new Demo("Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(50, 50, 1200, 700);
        frame.pack();
        frame.setVisible(true);


    }
}

2 个答案:

答案 0 :(得分:2)

所有JPanel变量都是指普通的JPanel,而不是被重写的类。

所以考虑改变一下:

private JPanel panel1;
private JPanel panel2;
private JPanel panel3;

public Demo(String string) {
    // ...

    JPanel topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    getContentPane().add( topPanel );

    // Create the panels
    new MyJPanel1();
    new MyJPanel2();
    new MyJPanel3();


    // ...
}

private class MyJPanel1 extends JPanel{

    public MyJPanel1(){
        panel1 = new JPanel();
        panel1.setLayout( new BorderLayout() );
        panel1.setPreferredSize(new Dimension(200,500));
    }

    @Override 
    public void paintComponent(Graphics g) { 
        //super.paintComponent(g); 

        g.drawString("BLAH1", 20, 20); 
        g.drawRect(200, 200, 200, 200); 
        //setVisible(true);
    } 


}
//.... etc

到此:

private JPanel panel1;
private JPanel panel2;
private JPanel panel3;

public Demo(String string) {
    // ...

    JPanel topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    getContentPane().add( topPanel );

    // Create the panels
    panel1 = new MyJPanel1();
    panel2 = new MyJPanel2();
    panel3 = new MyJPanel3();


    // ...
}

private class MyJPanel1 extends JPanel{

    public MyJPanel1(){
        // panel1 = new JPanel();
        setLayout( new BorderLayout() );
        setPreferredSize(new Dimension(200,500));
    }

    @Override 
    public void paintComponent(Graphics g) { 
        //super.paintComponent(g); 

        g.drawString("BLAH1", 20, 20); 
        g.drawRect(200, 200, 200, 200); 
        //setVisible(true);
    } 


}
//.... do the same with the other JPanel classes

答案 1 :(得分:2)

尝试进行以下更改:

在班级的构造函数中 - MyJPanel1MyJPanel2MyJPanel3

  • 删除这些类型的行panel3 = new JPanel();
  • 并将这些类型的行panel3.set...替换为this.set...

并更改以下行:

new MyJPanel1();
new MyJPanel2();
new MyJPanel3();

panel1 = new MyJPanel1();
panel2 = new MyJPanel2();
panel3 = new MyJPanel3();