gpt4 book ai didi

java - setFrame、JSlider 错误

转载 作者:行者123 更新时间:2023-12-01 13:49:59 26 4
gpt4 key购买 nike

所以我正在为一个更大的项目测试 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com