高分求助Java e.getSource()的问题

2024-12-17 12:22:39
推荐回答(1个)
回答1:

java里管这种情况叫多点传送。
先给你一段代码
看看自己能对照着解决吗
不行的话hi我。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MulticastTest {
public static void main(String[] args) {
MulticastFrame frame = new MulticastFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class MulticastFrame extends JFrame {
public MulticastFrame() {
setTitle("MulticastTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

MulticastPanel panel = new MulticastPanel();
add(panel);
}

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}

class MulticastPanel extends JPanel {
public MulticastPanel() {
JButton newButton = new JButton("New");
add(newButton);
final JButton closeAllButton = new JButton("Close all");
add(closeAllButton);

ActionListener newListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
BlankFrame frame = new BlankFrame(closeAllButton);
frame.setVisible(true);
}
};

newButton.addActionListener(newListener);
}
}

class BlankFrame extends JFrame {
public BlankFrame(final JButton closeButton) {
counter++;
setTitle("Frame" + counter);
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocation(SPACING*counter, SPACING*counter);

closeListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
closeButton.removeActionListener(closeListener);
dispose();
}
};
closeButton.addActionListener(closeListener);
}

private ActionListener closeListener;
public static final int DEFAULT_WIDTH = 200;
public static final int DEFAULT_HEIGHT = 150;
public static final int SPACING = 40;
private static int counter = 0;
}