- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在为一个更大的项目测试 JSlider,但无法让它工作。 slider 应该调整圆的大小,但它不起作用。我想我可能在创建圆时遇到问题,我正在尝试使用 setFrame,但它给出了一个错误,指出它是“未定义”。谁能明白为什么吗?因为它应该接受 float 或 double 作为参数。或者,如果您能明白为什么它不调整形状的大小,那也会有很大帮助......这就是我所拥有的:
public class DrawShape extends JPanel{
private float width = 300;
private Shape circle = new Ellipse2D.Float(100, 20, width, 300);
public DrawShape() {
}
public DrawShape(float width) {
this.width = width;
}
public void setWidth(int w) {
this.width = w;
circle.setFrame(100, 20, width, 300);//This is where the error is
}
public void paintComponent (Graphics g) {
super.paintComponents(g);
Graphics2D graphics = (Graphics2D)g;
graphics.setColor(Color.black);
graphics.fill(circle);
}//end paintComponent
}//end class
主要类:
public class SliderTest extends JFrame{
private static DrawShape circle = new DrawShape();
JSlider slider;
JLabel label;
public SliderTest() {
setLayout(new FlowLayout());
slider = new JSlider(JSlider.HORIZONTAL, 150, 450, 300);//orientation, min val, max value, starting val
slider.setMajorTickSpacing(50);//every 5 integers will be a new tick position
slider.setPaintTicks(true);
add(slider);
label = new JLabel("Current value 300");
add(label);
event e = new event();
slider.addChangeListener(e);;
}//end cons
public class event implements ChangeListener{
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider)e.getSource();
int value = slider.getValue();
label.setText("Current Value " + value);
circle.setWidth(value);
repaint();
}//end stateChanged
}//end class event
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Circle");
frame.add(circle);
frame.setSize(500,400);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JFrame frame1 = new SliderTest ();
frame1.setTitle("Toolbar");
frame1.setSize(300,200);
frame1.setLocation(200,100);
frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame1.setVisible(true);
}
}
最佳答案
Shape
没有 setFrame
方法。 矩形形状
确实...
而不是
private Shape circle = new Ellipse2D.Float(100, 20, width, 300);
您可以尝试使用...
private Ellipse2D circle = new Ellipse2D.Float(100, 20, width, 300);
相反...
您的 public DrawShape(float width) {
构造函数也是错误的,因为它实际上没有执行任何操作。
您还应该考虑重写 getPreferredSize
方法,以便它可以返回形状的宽度作为首选尺寸的一部分。
我不确定您是否确实需要维护 width
引用,因为您可以直接从 circle
确定这一点...恕我直言
例如
我没有测试过这个...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
public class DrawShape extends JPanel {
private final Ellipse2D circle = new Ellipse2D.Float(100, 20, 300, 300);
public DrawShape() {
}
public DrawShape(float width) {
circle.setFrame(100, 20, width, 300);
}
public void setWidth(int w) {
circle.setFrame(100, 20, w, 300);
revalidate();
}
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width = circle.getBounds().width;
return size;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D graphics = (Graphics2D) g;
graphics.setColor(Color.black);
graphics.fill(circle);
}//end paintComponent
}//end class
关于java - setFrame、JSlider 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20039524/
我已从使用 JFrame 的项目中复制代码以显示文本字段和 JSlider 我的新项目使用了JInternalFrame,并且只绘制了 slider 。如果我注释掉代码,它将绘制文本字段,似乎它只会绘
我正在测试一个使用 JSlider 调整圆宽度的程序,并且 slider 的值正在工作,但它实际上并没有改变“宽度”变量。请帮忙!!!这是我到目前为止所拥有的: public class Slider
我的 JSlider 第一个值为 0,最后一个值为 8850。我想设置主要刻度间距,以便打印最后一个值 (8850)。例如,如果我将主刻度间距设置为 550,则最后打印的值将为 8800。 最佳答案
我想在单击按钮时动态添加文本字段,但要获取的值和按钮位于一个类中,而我想要添加与其相邻的文本框和 slider 的面板位于不同的类中。代码是- public class TipSplitting ex
我第一次尝试使用JSlider,并尝试咨询How to Use Sliders学习基础知识。但是当我尝试使刻度线显示为这样时: sliderInterior = new JSlider(JSlide
此 jSlider 代码导致应用程序挂起。 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //
我正在学习如何使用 oracle tutorials 中的 JSlider 。我想用它来更改代码中变量的值,而不是更改动画的速度/fps。我注意到我在电脑游戏中看到的“每秒帧数”并阅读了 here 。
我正在为模拟创建一个简单的“播放/倒带”界面,其中的 JSlider 既可用作进度条,也可用作 slider ,您可以将模拟移动到模拟范围内的任何时间点。滑动到时间点效果很好,但是当模拟“播放”时,我
这是我更新 jslider 的代码 try{ name=(String) jList1.getSelectedValue(); CanvasVideoSurface video
这个问题已经有答案了: Update JLabel repeatedly with results of long running task (2 个回答) 已关闭 8 年前。 我有一个 Java S
我有一个 wav 音频文件。我知道如何在java中使用AudioImputStream来播放它。我学会了创建 jslider。现在我要使用 slider 作为带有音频的进度条,比如音频播放,那么 sl
我刚刚开始使用 java :)。 我的问题是: 我想用 JSlider 制作一个 GUI,然后 slider 移动,我希望 JTextField 中的数字发生变化。我尝试了很多东西,但总是出错。 如果
是否可以在 JSlider 上显示两组具有相同刻度的标签,一组在顶部,一组在底部? 最佳答案 您可以实现自己的SliderUI,如图here ,并覆盖 paintTicks()。 或者,您可能试图表示
我在 Java 中使用 JSlider 时遇到问题 我画了一个圆 A,我想在第一个圆 A 内放置另一个圆 B。我想将第二个圆 B 的中心放在与圆心相同的坐标处第一个圆 A,然后我想使用 JSlider
在实现 JSlider 的 ChangeListener 时,只要值发生变化就会触发该事件。我需要获取 JSLider 的最后一个值:我不需要 slider 访问的每个值,只需要用户释放 slider
我有一个带有附加 ChangeListerner 的简单 JSlider。这是代码: JSlider slider = new JSlider(); slider.setMinorTickSpacin
我有一个非常简单的 JFrame,其北面板中只有一个 JSlider。我希望通过以下方法以最小尺寸显示 JSlider。 slider.putClientProperty("JComponent.si
我想为我 parent 的 Estate Agency 假期出租在线预订服务创建一个 Java 应用程序。 不幸的是,我还不能发布图片,但他们想要一种 slider 式的预订服务,用户可以在其中滑动条
我编写了一个小程序作为初学者练习,使用两个 JSlider 将摄氏度转换为华氏度,反之亦然。我让两个 JSliders 互相监听,目标是每当我移动一个 slider 时,Celcius slider
这是我的代码的一部分,但它不起作用,因为它一直说在我的 ActionListener 中找不到该符号。我不知道如何让它发挥作用。 所以基本上我想做的是让 1-8.png 中的图像根据 slider 着
我是一名优秀的程序员,十分优秀!