我有一个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方法中硬编码线条粗细数时最初的样子:
答案 0 :(得分: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。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将减少混淆和更好的响应