已修改
现在,导入在此处加下划线
导入javax.swing.event.ChangeListener;
并且下面的行也带有下划线
slide.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.
}
}
最佳答案
javafx.beans.value.ChangeListener;
与javax.swing.event.ChangeListener
不同
切勿使用getGraphics
尝试执行自定义绘画,这不是Swing中绘画的工作方式。有关自定义绘画如何工作以及应在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
关于java - 这似乎用红色下划线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27642258/