-1

为什么这里没有出现“绘图”?我将它添加到另一个 JPanel,然后将所有内容添加到另一个 JPanel 并返回。然而,我看到的只是轨迹球

public class Draw extends JFrame
{
    private JSplitPane itemPane;

    private Point position = null;

        public Draw()
        {
            // Set the layout to a grid
            setLayout ( new BorderLayout (5,5));
            // Set the properties of the window
            setTitle ("Emulator");
            setSize ( 900, 700);
            setDefaultCloseOperation (EXIT_ON_CLOSE);
            setBackground ( new Color (15, 255, 10));

            // Add the components
            addComponents ();
        }


        public static void startWindowsGui() 
        { 
            // We are in the static main, set the form to invoke later
            SwingUtilities.invokeLater ( new Runnable() 
            {
                public void run() 
                {
                    // Create a new instance of server and set it to visible
                    Draw gui = new Draw();
                    gui.setVisible (true);
                }
            } );
        }

        private void addComponents() 
        {
            // Create the main and an itemPane
            JSplitPane mainPane = new JSplitPane ( JSplitPane.HORIZONTAL_SPLIT );
            setItemPane(new JSplitPane ( JSplitPane.VERTICAL_SPLIT ));

            mainPane.add ( createPane ( ), JSplitPane.LEFT );
            mainPane.add ( getItemPane(), JSplitPane.RIGHT );
            mainPane.setOneTouchExpandable ( true );

            getItemPane().setOpaque(true);
            getItemPane().setBackground(new Color(0xffffffc0));

            JPanel p = new JPanel();
            this.getItemPane().add(p);

            add ( mainPane, BorderLayout.CENTER );
        }

        public static void main(final String[] args) 
        {
            Runnable gui = new Runnable() 
            {
                @Override
                public void run() 
                {
                    new Draw().setVisible(true);
                }
            };
            SwingUtilities.invokeLater(gui);
        }

        private class Drawing extends JPanel {

            private static final long serialVersionUID = 1L;
            private final Point position;

            public Drawing(Point position) {
                this.position = position;
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setColor(Color.RED);
                g2.fillOval(160 + position.x, 160 - position.y, 15, 15);
            }
        }

        private JPanel createPane() 
        {
            // Create the feedPanel
            JPanel panel = new JPanel ( );
            JPanel panel2 = new JPanel ( );
            JPanel panel3 = new JPanel ( );
            this.position = new Point();

            TrackBall myJoystick = new TrackBall(150, position, 100);
            panel.add(myJoystick, BorderLayout.PAGE_END);
            Drawing drawing = new Drawing(position);

            panel2.add(drawing, BorderLayout.PAGE_START);

            panel3.add(panel2);
            panel3.add(panel);
            return panel3;
        }
        public JSplitPane getItemPane() {return itemPane;}
        public void setItemPane(JSplitPane itemPane) {this.itemPane = itemPane;}
}
4

1 回答 1

2

3个问题

  • panel2需要而不是使用约束BorderLayout的默认值FlowLayoutBorderLayout.PAGE_START
  • 该类Drawing需要覆盖getPreferredSize
  • Dimension指定的 by应该getPreferredSize足够大以容纳position在构造函数中指定的Drawing

阅读:更改 Swing 组件的首选大小

于 2013-09-29T21:37:45.037 回答