简体   繁体   English

在多个JPanel内绘制

[英]Drawing inside multiple JPanels

I have three JPanels inside a main Frame. 我在一个主框架内有三个JPanels。 Clockwise from the left, in the first panel I plan to have some controls which dictate the drawing on the panel 2. The third bottom panel will show some informations. 我计划在第一个面板中从左向右顺时针方向移动一些控件,这些控件决定了面板2上的绘图。第三个底部面板将显示一些信息。

What I understand is, I have to override paintComponent so that I can achieve the desired effect on the second panel. 我了解的是,我必须重写paintComponent以便在第二个面板上实现所需的效果。 Right now I just want to test it, whether I can draw simple texts on this panel. 现在我只想测试一下,是否可以在此面板上绘制简单的文本。

But in fact, I am having problem to draw anything in any of the three panels. 但是实际上,我很难在三个面板中的任何一个上绘制任何东西。

The code is given below, I dont know what is the problem. 下面给出了代码,我不知道是什么问题。

    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);


    }
}

All your JPanel variables refer to plain vanilla JPanels, not to the overridden classes. 您所有的JPanel变量都引用普通的JPanels,而不引用重写的类。

so consider changing this: 因此请考虑更改此内容:

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

to this: 对此:

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

Try making following changes: 尝试进行以下更改:

In the constructors for your class - MyJPanel1 , MyJPanel2 , MyJPanel3 : 在您的类的构造函数MyJPanel1MyJPanel2MyJPanel3

  • remove these type of lines panel3 = new JPanel(); 删除这些类型的行panel3 = new JPanel();
  • and replace these type of lines panel3.set... with this.set... . 并将这些类型的线panel3.set...替换为this.set...

And change the following lines: 并更改以下行:

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM