目前,我有一个 JLabel,我可以在绘图板(另一个面板)的任何位置单击并创建它的新实例,我希望将另一个 JLabel 作为连接器,我可以选择它来链接绘图板上的 JLabel 实例. 例如,连接器将在 2 个 JLabel 之间创建一条线,以模拟它们都已链接起来。我该怎么做呢?
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class SimulatorGUI extends JFrame {
private JPanel contentPane;
JPanel southPanel;
JPanel centerPanel;
JPanel eastPanel;
JPanel westPanel;
int clickCount = 0;
ByteArrayOutputStream baos;
ByteArrayInputStream bais;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SimulatorGUI frame = new SimulatorGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SimulatorGUI() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
setTitle("Network Simulator Prototype");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmNew = new JMenuItem("New");
mnFile.add(mntmNew);
JMenuItem mntmOpen = new JMenuItem("Open");
mnFile.add(mntmOpen);
JMenuItem mntmSave = new JMenuItem("Save");
mnFile.add(mntmSave);
JMenuItem mntmSaveAs = new JMenuItem("Save As");
mnFile.add(mntmSaveAs);
JSeparator separator = new JSeparator();
mnFile.add(separator);
JMenuItem mntmExit = new JMenuItem("Exit");
mnFile.add(mntmExit);
JMenu mnEdit = new JMenu("Edit");
menuBar.add(mnEdit);
JMenuItem mntmCopy = new JMenuItem("Copy");
mnEdit.add(mntmCopy);
JMenuItem mntmPaste = new JMenuItem("Paste");
mnEdit.add(mntmPaste);
JSeparator separator_1 = new JSeparator();
mnEdit.add(separator_1);
JMenuItem mntmNewMenuItem = new JMenuItem("Undo");
mnEdit.add(mntmNewMenuItem);
JMenuItem mntmRedo = new JMenuItem("Redo");
mnEdit.add(mntmRedo);
JMenu mnOptions = new JMenu("Options");
menuBar.add(mnOptions);
JMenu mnView = new JMenu("View");
menuBar.add(mnView);
JMenu mnTools = new JMenu("Tools");
menuBar.add(mnTools);
JMenu mnExtentions = new JMenu("Extentions");
menuBar.add(mnExtentions);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
southPanel = new JPanel();
JLabel lblNode = new JLabel("Node");
southPanel.add(lblNode);
JLabel lblWire = new JLabel("Wire");
southPanel.add(lblWire);
class MyMouseAdapter extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
clickCount = 1;
try {
copy((JLabel) e.getSource());
} catch (Exception ex) {
}
}
}
lblNode.addMouseListener(new MyMouseAdapter());
southPanel.setBorder(BorderFactory.createTitledBorder("Nodes"));
contentPane.add(southPanel, BorderLayout.SOUTH);
centerPanel = new JPanel();
centerPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createRaisedBevelBorder(), "Work Space", TitledBorder.CENTER, TitledBorder.TOP));
centerPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (clickCount == 1) {
try {
pasteLabel(e.getX(), e.getY());
} catch (Exception ex) {
}
}
}
});
contentPane.add(centerPanel, BorderLayout.CENTER);
centerPanel.setLayout(null);
eastPanel = new JPanel();
contentPane.add(eastPanel, BorderLayout.EAST);
eastPanel.setBorder(BorderFactory.createTitledBorder("Configuration"));
JButton btnSetting = new JButton("Settings");
btnSetting.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
GUI gui = new GUI();
}
});
eastPanel.add(btnSetting);
westPanel = new JPanel();
contentPane.add(westPanel, BorderLayout.WEST);
westPanel.setBorder(BorderFactory.createTitledBorder("Reset"));
JButton btnClear = new JButton("Clear");
btnClear.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
centerPanel.removeAll();
centerPanel.validate();
centerPanel.repaint();
}
});
westPanel.add(btnClear);
}
public void copy(JLabel label) throws Exception {
baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(label);
oos.close();
}
public void pasteLabel(int x, int y) throws Exception {
if (clickCount == 1) {
bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
JLabel obj = (JLabel) ois.readObject();
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
obj.addMouseListener(myMouseAdapter);
obj.addMouseMotionListener(myMouseAdapter);
centerPanel.add(obj);
obj.setBounds(x, y, obj.getWidth(), obj.getHeight());
clickCount = 0;
ois.close();
}
}
class MyMouseAdapter extends MouseAdapter {
private Point initialLoc;
private Point initialLocOnScreen;
@Override
public void mousePressed(MouseEvent e) {
Component comp = (Component) e.getSource();
initialLoc = comp.getLocation();
initialLocOnScreen = e.getLocationOnScreen();
}
@Override
public void mouseReleased(MouseEvent e) {
Component comp = (Component) e.getSource();
Point locOnScreen = e.getLocationOnScreen();
int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
comp.setLocation(x, y);
}
@Override
public void mouseDragged(MouseEvent e) {
Component comp = (Component) e.getSource();
Point locOnScreen = e.getLocationOnScreen();
int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
comp.setLocation(x, y);
}
}
}
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class GUI extends JFrame {
private JPanel contentPane;
JPanel centerPanel;
JPanel southPanel;
public GUI() {
setVisible(true);
setResizable(false);
setTitle("Settings");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 280, 167);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
centerPanel = new JPanel(new BorderLayout(3, 3));
contentPane.add(centerPanel, BorderLayout.CENTER);
JComboBox comboBoxConnection = new JComboBox();
comboBoxConnection.setModel(new DefaultComboBoxModel(new String[] { "Wired", "Wireless" }));
JComboBox comboBoxTransmission = new JComboBox();
comboBoxTransmission.setModel(new DefaultComboBoxModel(new String[] { "UDP", "TCP" }));
JComboBox comboBoxOrdering = new JComboBox();
comboBoxOrdering.setModel(new DefaultComboBoxModel(new String[] { "FIFO" }));
JTextField selectTrafficAns = new JTextField();
JPanel labels = new JPanel(new GridLayout(0, 1));
JPanel controls = new JPanel(new GridLayout(0, 1));
centerPanel.add(labels, BorderLayout.WEST);
centerPanel.add(controls, BorderLayout.CENTER);
labels.add(new JLabel("Connection Type:"));
controls.add(comboBoxConnection);
labels.add(new JLabel("Transmission Type:"));
controls.add(comboBoxTransmission);
labels.add(new JLabel("Ordering:"));
controls.add(comboBoxOrdering);
labels.add(new JLabel("Amount of Traffic:"));
controls.add(selectTrafficAns);
southPanel = new JPanel();
contentPane.add(southPanel, BorderLayout.SOUTH);
JButton btnOk = new JButton("Ok");
btnOk.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
dispose();
}
});
southPanel.add(btnOk);
JButton btnCancel = new JButton("Cancel");
btnCancel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
dispose();
}
});
southPanel.add(btnCancel);
}
}