修
现在,导入在此处显示为下划线
import javax.swing.event.ChangeListener;
并且下面的行显示带下划线
slider.addChangeListener(的ChangeListener());
我对此并不清楚
完整代码
package areaofrectangleslider;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;
public class Areaofrectangleslider extends JFrame implements
ActionListener, ChangeListener {
private JPanel panel;
private Random random;
private javax.swing.Timer timer;
private JSlider slider;
private JTextField gapField;
private JLabel gapLabel;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
EventQueue.invokeLater(new Runnable() {
public void run(){
Areaofrectangleslider frame = new Areaofrectangleslider();
frame.setSize(250,300);
frame.createGUI();
frame.setVisible(true);
}
});
}
public void actionPerformed(ActionEvent e) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
int x, y, size;
Graphics paper = panel.getGraphics();
x = random.nextInt(200);
y = random.nextInt(200);
size = random.nextInt(20);
paper.fillOval(x, y, size, size);
}
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
int timeGap = slider.getValue();
gapField.setText(Integer.toString(timeGap));
timer.setDelay(timeGap);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
random = new Random();
panel = new JPanel();
panel.setPreferredSize(new Dimension(200,200));
panel.setBackground(Color.white);
window.add(panel);
gapLabel = new JLabel("TIme Gap: ");
window.add(gapLabel);
gapField = new JTextField(10);
window.add(gapField);
slider = new JSlider(JSlider.HORIZONTAL,200,2000,1000);
window.add(slider);
slider.addChangeListener(ChangeListener());
gapField.setText(Integer.toString(slider.getValue()));
timer = new javax.swing.Timer(1000, this);
timer.start();
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
旧
我似乎无法解决为什么我在这里得到错误
slider.addChangeListener(this);
(this
)以红色下划线标出
我得到的控制台读数是:
Exception in thread "main" java.lang.ClassCastException: areaofrectangleslider.Areaofrectangleslider cannot be cast to javax.swing.event.ChangeListener
at areaofrectangleslider.Areaofrectangleslider.createGUI(Areaofrectangleslider.java:91)
at areaofrectangleslider.Areaofrectangleslider.main(Areaofrectangleslider.java:46)
Java Result:
以下完整代码
package areaofrectangleslider;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
public class Areaofrectangleslider extends JFrame implements
ActionListener, ChangeListener {
private JPanel panel;
private Random random;
private javax.swing.Timer timer;
private JSlider slider;
private JTextField gapField;
private JLabel gapLabel;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Areaofrectangleslider frame = new Areaofrectangleslider();
frame.setSize(250,300);
frame.createGUI();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
int x, y, size;
Graphics paper = panel.getGraphics();
x = random.nextInt(200);
y = random.nextInt(200);
size = random.nextInt(20);
paper.fillOval(x, y, size, size);
}
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
int timeGap = slider.getValue();
gapField.setText(Integer.toString(timeGap));
timer.setDelay(timeGap);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
random = new Random();
panel = new JPanel();
panel.setPreferredSize(new Dimension(200,200));
panel.setBackground(Color.white);
window.add(panel);
gapLabel = new JLabel("TIme Gap: ");
window.add(gapLabel);
gapField = new JTextField(10);
window.add(gapField);
slider = new JSlider(JSlider.HORIZONTAL,200,2000,1000);
window.add(slider);
slider.addChangeListener(this);
gapField.setText(Integer.toString(slider.getValue()));
timer = new javax.swing.Timer(1000, this);
timer.start();
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
答案 0 :(得分:5)
javafx.beans.value.ChangeListener;
与javax.swing.event.ChangeListener
永远不要使用getGraphics
来尝试和执行自定义绘画,这不是绘画在Swing中的工作原理。有关自定义绘画如何工作的详细信息,请参阅Painting in AWT and Swing和Performing Custom Painting
另外(如下所述),所有UI创作和修改都应在事件调度线程的上下文中执行...
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
Areaofrectangleslider frame = new Areaofrectangleslider();
frame.setSize(250,300);
frame.createGUI();
frame.setVisible(true);
}
});
}
有关详细信息,请参阅Initial Threads
答案 1 :(得分:0)
JSlider中addChangeListner的方法签名是
public void addChangeListener(ChangeListener l)
其中ChangeListner为javax.swing.event.ChangeListener
。因此,在您的代码中,您传递的javafx.beans.value.ChangeListener
类型错误且无法投放。