gpt4 book ai didi

java - 使用 JSlider 上的 ChangeListener 更改 JPanel 上绘制线条的粗细

转载 作者:行者123 更新时间:2023-12-02 03:37:44 27 4
gpt4 key购买 nike

我有一个 JSlider,它指定在 JPanel 上绘制的线条的粗细。我向其中添加了一个 ChangeListener,因此当 slider 更改时,它也应该更改线条的粗细。但是当我这样做时,粗细并没有改变,相反,我得到了一条淡出的线。当我没有 ChangeListener 时,初始颜色是紫色线。我不明白发生了什么事。

这是在我的主 GUI 类中,它包括 JSlider 和 ChangeListener:

    final JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 0, 20, 1);

slider.setMajorTickSpacing(5);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setPaintLabels(true);

slider.addChangeListener(new ChangeListener() {
public void stateChanged(final ChangeEvent theEvent) {
final int stroke = slider.getValue();
myDrawPanel.setStroke(stroke);
myDrawPanel.repaint();
}
});

这是在另一个类中绘制线的:

public void setStroke(final int theStroke) {
myStroke = theStroke;
}

public void paintComponent(final Graphics theGraphics) {
super.paintComponent(theGraphics);
final Graphics2D g2d = (Graphics2D) theGraphics;

// for better graphics display
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setPaint(new Color(51, 0, 111));
g2d.setStroke(new BasicStroke(myStroke));

g2d.draw(new Line2D.Double(myX, myY, myXEnd, myYEnd));

for (Line2D l : myLines) {
g2d.draw(l);
}
}

这就是结果:1

这就是我在 PaintComponent 方法中硬编码线条粗细数字时线条最初的样子:2]

最佳答案

似乎对我来说工作正常,我想问题出在您没有向我们展示的代码中的其他地方

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

public class Example {

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

public Example() {
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 ControlPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class ControlPane extends JPanel {

public ControlPane() {
setLayout(new BorderLayout());

DrawPane pane = new DrawPane();
add(pane);

JSlider slider = new JSlider(0, 20, 1);
add(slider, BorderLayout.SOUTH);

slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
pane.setStroke(slider.getValue());
}
});
slider.setValue(1);
}

}

public class DrawPane extends JPanel {

private int myStroke = 1;

public DrawPane() {
}

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

public void setStroke(int myStroke) {
this.myStroke = myStroke;
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(new BasicStroke(myStroke));
g2d.drawLine(0, 0, getWidth(), getHeight());
g2d.dispose();
}

}

}

考虑提供 runnable example这说明了你的问题。这不是代码转储,而是您正在执行的操作的示例,它突出显示了您遇到的问题。这将减少困惑并获得更好的响应

关于java - 使用 JSlider 上的 ChangeListener 更改 JPanel 上绘制线条的粗细,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37232124/

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