为什么不是';我的JSlider是否显示在java GUI中?

为什么不是';我的JSlider是否显示在java GUI中?,java,swing,paintcomponent,jslider,Java,Swing,Paintcomponent,Jslider,我真的很沮丧,因为GUI显示但没有JSlider!好的,假设GUI显示一个圆,JTextfield(滑块移动时更新)。文本字段应显示半径、直径等。。并在滑块移动时自动更新。移动滑块后,圆的大小也会增加或减小。这是我的密码。无错误,运行正常,可编译。可能是我的坐标出了问题 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.event.WindowAdapter

我真的很沮丧,因为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
是一个窗口,无法将其添加到另一个窗口,因此您的
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);
    }
});
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);
    }
});
然后你可以使用类似于

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);
    }
});
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);
    }
});
展示

然后你可以把你的
幻灯片框架
改成更像

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);
    }
}
使用类似于

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);
    }
});
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);
    }
});
来展示它

可运行示例
据我所知,
SliderFrame
从未在您的代码中使用过您还应该在执行任何自定义绘制之前调用
super.paintComponent
,否则,这将产生一些讨厌的随机绘制工件。我正在使用
diameterJSlider
,当我在单独的GUI上使用它并显示滑块时,它工作得很好。我如何称呼“super.paintComponent”是的,但在示例代码中,您从未实际创建/调用/显示
SliderFrame
。您使用
super.paintComponent(g)从内部调用
super.paintComponent
我会在底部添加主类吗?你是我的救命恩人!我一定会接受你的回答!你可以在有意义的地方添加
main
,我假设每个类都在它自己的类文件中,我只是把它们都放在一个类中,将一个类命名为public,并将main放在末尾。看起来它对“frame.setDefaultCloseOperation(JFrame.EXIT\u ON\u CLOSE)”很挑剔;frame.add(新的SliderPane());frame.pack();frame.setLocationRelativeTo(空);frame.setVisible(true);“”frame.set“您使用的值”。你改变了什么?