我有一个具有空布局的 JPanel,我正在尝试添加一个 JSlider:
slider = new JSlider();
slider.setSize(300,30);
slider.setPreferredSize(new Dimension(300,30));
int x = (Constants.getScreenWidth()/2)-(slider.getWidth()/2);
int y = (Constants.getScreenHeight()/2)-(slider.getHeight()/2);
y = y - 100;
slider.setLocation(x,y);
slider.setBounds(x-(slider.getWidth()/2),y-(slider.getHeight()/2),300,30);
add(slider);
但是滑块没有显示出来。我已经打印了滑块,它是:
javax.swing.JSlider[,420,320,300x30,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=288,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=300,height=30],isInverted=false,majorTickSpacing=0,minorTickSpacing=0,orientation=HORIZONTAL,paintLabels=false,paintTicks=false,paintTrack=true,snapToTicks=false,snapToValue=true]
为什么我看不到滑块?