我有一个JSlider,它指定在JPanel上绘制的线条的粗细。我向其中添加了一个ChangeListener,因此,在更改滑块时,它还应该更改线条的粗细。但是,当我这样做时,厚度不会改变,而是给了我一条淡出的线条。当我没有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);
}
}
这是结果:
这是当我在paintComponent方法中硬编码线宽数字时,线的原始外观:
最佳答案
对于我来说似乎工作正常,我想问题出在您未向我们显示的代码中的其他地方
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来演示您的问题。这不是代码转储,而是您正在执行的操作的一个示例,突出显示了您所遇到的问题。这将减少混乱并改善响应