侦听器的实现不起作用
Implement of listener does not work
public class ShapeDisplayer implements ActionListener {
private static final int Width = 50;
private static final int Frame_Width = 400;
private static final int Frame_Height = 600;
static JPanel DrawPanel;
static JButton carButton;
static JButton eclipseButton;
static JButton compButton;
public static void main (String args[])
{
// create frame
JFrame frame = new JFrame("Drawing Applet");
// create panels
JPanel ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
DrawPanel = new JPanel(new BorderLayout());
// create Icons
CarIcon Acar = new CarIcon(Width);
EclipseIcon doubleCircle = new EclipseIcon(20);
CompositeIcon compIcon = new CompositeIcon(50, 60);
// create buttons
carButton = new JButton(Acar);
eclipseButton = new JButton(doubleCircle);
compButton = new JButton(compIcon);
// panel config
ButtonPanel.setBounds(0,0,Frame_Width,100);
DrawPanel.setBounds(0,100,Frame_Width,Frame_Height-100);
// add panels'components
ButtonPanel.add(eclipseButton);
ButtonPanel.add(carButton);
ButtonPanel.add(compButton);
// add frames' components
frame.add(ButtonPanel);
frame.add(DrawPanel);
// frame config
frame.setSize(Frame_Width, Frame_Height);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == carButton)
{
DrawPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
CarIcon car = new CarIcon(Width, e.getX(), e.getY());
JLabel lable = new JLabel (car);
DrawPanel.add(lable);
}
});
}
}
}
我正在尝试实现此程序以在鼠标单击时在面板上绘制,例如,如果按钮是 carButton,则每当我单击面板时,都会绘制一个汽车图标。我实现了侦听器,但是当我单击面板时,panel.I 上没有任何内容出现在 Jframe 上添加侦听器,我是否需要将其添加到 Jpanel 上才能使程序正常工作?有人可以帮助修复此程序,在此先感谢。
单击按钮时不会调用 actionPerformed 方法。因为它没有绑定到任何按钮。
例如,carButton 可以添加一个动作侦听器,如下所示。
carButton = new JButton(Acar);
carButton.addActionListener(new ShapeDisplayer());
请注意,由于此代码位于 main 方法中,因此 "this" 不能用于添加动作侦听器。需要实例化 ShapeDisplayer class 的新对象。
此外,DrawPanel 可能需要重新绘制才能使更改生效。调用 revalidate 方法可以做到这一点。
DrawPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
CarIcon car = new CarIcon(Width, e.getX(), e.getY());
JLabel lable = new JLabel (car);
DrawPanel.add(lable);
DrawPanel.revalidate();
}
});
public class ShapeDisplayer implements ActionListener {
private static final int Width = 50;
private static final int Frame_Width = 400;
private static final int Frame_Height = 600;
static JPanel DrawPanel;
static JButton carButton;
static JButton eclipseButton;
static JButton compButton;
public static void main (String args[])
{
// create frame
JFrame frame = new JFrame("Drawing Applet");
// create panels
JPanel ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
DrawPanel = new JPanel(new BorderLayout());
// create Icons
CarIcon Acar = new CarIcon(Width);
EclipseIcon doubleCircle = new EclipseIcon(20);
CompositeIcon compIcon = new CompositeIcon(50, 60);
// create buttons
carButton = new JButton(Acar);
eclipseButton = new JButton(doubleCircle);
compButton = new JButton(compIcon);
// panel config
ButtonPanel.setBounds(0,0,Frame_Width,100);
DrawPanel.setBounds(0,100,Frame_Width,Frame_Height-100);
// add panels'components
ButtonPanel.add(eclipseButton);
ButtonPanel.add(carButton);
ButtonPanel.add(compButton);
// add frames' components
frame.add(ButtonPanel);
frame.add(DrawPanel);
// frame config
frame.setSize(Frame_Width, Frame_Height);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == carButton)
{
DrawPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
CarIcon car = new CarIcon(Width, e.getX(), e.getY());
JLabel lable = new JLabel (car);
DrawPanel.add(lable);
}
});
}
}
}
我正在尝试实现此程序以在鼠标单击时在面板上绘制,例如,如果按钮是 carButton,则每当我单击面板时,都会绘制一个汽车图标。我实现了侦听器,但是当我单击面板时,panel.I 上没有任何内容出现在 Jframe 上添加侦听器,我是否需要将其添加到 Jpanel 上才能使程序正常工作?有人可以帮助修复此程序,在此先感谢。
单击按钮时不会调用 actionPerformed 方法。因为它没有绑定到任何按钮。
例如,carButton 可以添加一个动作侦听器,如下所示。
carButton = new JButton(Acar);
carButton.addActionListener(new ShapeDisplayer());
请注意,由于此代码位于 main 方法中,因此 "this" 不能用于添加动作侦听器。需要实例化 ShapeDisplayer class 的新对象。
此外,DrawPanel 可能需要重新绘制才能使更改生效。调用 revalidate 方法可以做到这一点。
DrawPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
CarIcon car = new CarIcon(Width, e.getX(), e.getY());
JLabel lable = new JLabel (car);
DrawPanel.add(lable);
DrawPanel.revalidate();
}
});