Resizing issue with canvas within jscrollpane within jsplitpane

后端 未结 3 968
忘了有多久
忘了有多久 2020-11-22 03:28

I\'m creating an application using the NetBeans GUI Editor, in which I want to have a JSplitPane, the top component of which will be a Canvas withi

相关标签:
3条回答
  • 2020-11-22 03:50

    As I said in my comments, you should not mix AWT and Swing components. I think you are not using the components in the correct way. Take a look, it is a simple example of how to use a JSplitPane.

    import java.awt.*; // it is necessary to use the Dimension and BorderLayout classes
    import javax.swing.*;
    
    public class Foo extends JFrame {
    
        public Foo() {
    
            setTitle( "Splits" );
            setDefaultCloseOperation( EXIT_ON_CLOSE );
            setSize( 400, 400 );
    
            JSplitPane split = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
            split.setDividerLocation( 200 );
            add( split );
    
            JPanel panel1 = new JPanel();
            panel1.setLayout( new BorderLayout() );
            panel1.add( new JLabel( "top panel" ), BorderLayout.NORTH );
    
            JPanel myDrawPanel = new JPanel();
            myDrawPanel.setPreferredSize( new Dimension( 100, 100 ) );
            myDrawPanel.add( new JLabel( "draw panel here!" ) );
            panel1.add( new JScrollPane( myDrawPanel ), BorderLayout.CENTER );
    
            split.setTopComponent( panel1 );
    
    
            JPanel panel2 = new JPanel();
            panel2.add( new JLabel( "bottom panel" ) );
            split.setBottomComponent( panel2 );
    
    
            setVisible( true );
    
        }
    
        public static void main( String[] args ) {
            new Foo();
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 03:51

    Instead of setPreferredSize(), let your components calculate their own preferred size and pack() the enclosing Window to accommodate. The example below adds an instance of draw.GraphPanel to the top and a corresponding control panel to the bottom.

    image

    import draw.GraphPanel;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    
    /**
     * @see https://stackoverflow.com/q/11942961/230513
     */
    public class SplitGraph extends JPanel {
    
        public SplitGraph() {
            super(new GridLayout());
            JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            GraphPanel graphPanel = new GraphPanel();
            split.setTopComponent(new JScrollPane(graphPanel));
            split.setBottomComponent(graphPanel.getControlPanel());
            this.add(split);
        }
    
        private void display() {
            JFrame f = new JFrame("SplitGraph");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new SplitGraph().display();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-22 04:02

    After reading the comment by davidbuzatto I googled about mixing AWT and Swing components and I was a little surprissed to find out that it is such a bad practice.

    I found the most accurate answer to my question here

    Heavyweight components have their own Z-ordering. This is the reason why Swing and AWT cannot be combined in a single container. If they are, the AWT components will be drawn on TOP of Swing components.

    For example: When AWT components are used with JtabbedPane, they do not disappear when the tabs are switched.

    Thanks davidbuzatto for showing me the way :-)

    0 讨论(0)
提交回复
热议问题