为什么我的 JSlider 没有显示在我的 java GUI 中?

Why isn't my JSlider Displaying in my java GUI?

我真的很沮丧,因为 GUI 显示但没有 JSlider!好的,所以 GUI 应该显示一个圆圈,JTextfield(移动滑块时更新)。文本字段假定显示半径、直径等。并在滑块移动时自动更新。一旦滑块移动,圆圈的大小也应该增加或减小。这是我的代码。没有错误,运行良好,并编译。可能是我坐标错了。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import javafx.stage.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Circle1 extends JFrame 
{

   private final CircleCanvas theCanvas;

   private final JTextArea display; 

   public Circle1()

   {

      super( "Circle1" );

      theCanvas = new CircleCanvas();

      display = new JTextArea( 4, 30 );



      display.setText( "The Radius: " + theCanvas.getRadius() + "\nThe Diameter: " + theCanvas.getDiameter() + "\nThe Area: " + theCanvas.getArea() +

                              "\nThe Circumference: " + theCanvas.getCircumference() );



      getContentPane().add( theCanvas, BorderLayout.CENTER );

      getContentPane().add( display, BorderLayout.SOUTH );

      setSize( 200, 200 );
      setVisible(true);

   }



   public static void main( String args[] )

   {

      Circle1 app = new Circle1();


      app.addWindowListener(

         new WindowAdapter() 
         {

            public void WindowClosing( WindowEvent e )

            {

            }
         }
      );
   }

    void setDiameter(int value) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

} 

class CircleCanvas extends JPanel 
{

   private final int radius; 

   public CircleCanvas()

   {

      radius = ( int )( 1 + Math.random() * 100 );

      setSize (100, 100);

   }

   @Override
   public void paintComponent( Graphics g )

           {  g.drawOval( 0, 0, radius, radius );  }



   public int getDiameter()  
            {  return ( 2 * radius );  }



   public int getCircumference()

           {  return ( int )( 2 * Math.PI * radius );  }



   public int getArea()

           {  return ( int )( radius * radius * Math.PI );  }



   public int getRadius()  
            {  return radius;  }

}

class SliderFrame extends JFrame
{
      private JSlider diameterJSlider;
      private Circle1 myPanel;

      public SliderFrame()
      {

        super("HW2");

        myPanel = new Circle1();


        diameterJSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 300, 10);
        diameterJSlider.setMajorTickSpacing(10);
        diameterJSlider.setPaintTicks(true);
        diameterJSlider.addChangeListener(new ChangeListener()
        {
        public void stateChanged(ChangeEvent e)
        {
          myPanel.setDiameter(diameterJSlider.getValue());
        }
      }
    );
        add(diameterJSlider, BorderLayout.NORTH);
        add(myPanel, BorderLayout.CENTER);
     }
  }

Circle1是一个window,不能加到另一个window,所以你的SliderFrame已经做错了,这就是为什么一般不鼓励直接从像 JFrame 这样的顶级容器扩展,相反,你应该从像 JPanel

这样的东西开始

所以,您可以从...开始...

public class Circle1 extends JPanel {

    private final CircleCanvas theCanvas;

    private final JTextArea display;

    public Circle1() {

        setLayout(new BorderLayout());

        theCanvas = new CircleCanvas();

        display = new JTextArea(4, 30);

        display.setText("The Radius: " + theCanvas.getRadius() + "\nThe Diameter: " + theCanvas.getDiameter() + "\nThe Area: " + theCanvas.getArea()
                        + "\nThe Circumference: " + theCanvas.getCircumference());

        add(theCanvas, BorderLayout.CENTER);
        add(display, BorderLayout.SOUTH);

    }

    void setDiameter(int value) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    class CircleCanvas extends JPanel {

        private final int radius;

        public CircleCanvas() {

            radius = (int) (1 + Math.random() * 100);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawOval(0, 0, radius, radius);
        }

        public int getDiameter() {
            return (2 * radius);
        }

        public int getCircumference() {
            return (int) (2 * Math.PI * radius);
        }

        public int getArea() {
            return (int) (radius * radius * Math.PI);
        }

        public int getRadius() {
            return radius;
        }

    }
}

然后你可以使用类似...

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }

        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Circle1());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
});

显示。

然后您可以将 SliderFrame 更改为更像...

public class SliderPane extends JPanel {

    private JSlider diameterJSlider;
    private Circle1 myPanel;

    public SliderPane() {

        setLayout(new BorderLayout());
        
        myPanel = new Circle1();

        diameterJSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 300, 10);
        diameterJSlider.setMajorTickSpacing(10);
        diameterJSlider.setPaintTicks(true);
        diameterJSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                myPanel.setDiameter(diameterJSlider.getValue());
            }
        }
        );
        add(diameterJSlider, BorderLayout.NORTH);
        add(myPanel, BorderLayout.CENTER);
    }
}

并使用类似...

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }

        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SliderPane());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
});

显示它。

可运行示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // Change these two lines to switch between the panels
                //frame.add(new Circle1());
                frame.add(new SliderPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Circle1 extends JPanel {

        private final CircleCanvas theCanvas;

        private final JTextArea display;

        public Circle1() {

            setLayout(new BorderLayout());

            theCanvas = new CircleCanvas();

            display = new JTextArea(4, 30);

            display.setText("The Radius: " + theCanvas.getRadius() + "\nThe Diameter: " + theCanvas.getDiameter() + "\nThe Area: " + theCanvas.getArea()
                            + "\nThe Circumference: " + theCanvas.getCircumference());

            add(theCanvas, BorderLayout.CENTER);
            add(display, BorderLayout.SOUTH);

        }

        void setDiameter(int value) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        class CircleCanvas extends JPanel {

            private final int radius;

            public CircleCanvas() {

                radius = (int) (1 + Math.random() * 100);

            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(100, 100);
            }

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawOval(0, 0, radius, radius);
            }

            public int getDiameter() {
                return (2 * radius);
            }

            public int getCircumference() {
                return (int) (2 * Math.PI * radius);
            }

            public int getArea() {
                return (int) (radius * radius * Math.PI);
            }

            public int getRadius() {
                return radius;
            }

        }
    }

    public class SliderPane extends JPanel {

        private JSlider diameterJSlider;
        private Circle1 myPanel;

        public SliderPane() {

            setLayout(new BorderLayout());

            myPanel = new Circle1();

            diameterJSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 300, 10);
            diameterJSlider.setMajorTickSpacing(10);
            diameterJSlider.setPaintTicks(true);
            diameterJSlider.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    myPanel.setDiameter(diameterJSlider.getValue());
                }
            }
            );
            add(diameterJSlider, BorderLayout.NORTH);
            add(myPanel, BorderLayout.CENTER);
        }
    }

}