因此,我正在尝试做到这一点,以便选择“名字”或“姓氏”并在Palettepane中按下鼠标时,鼠标将出现在鼠标X或Mousey上。这是据我所知,我很难过。有什么想法吗?
这是设置:
public class benNameFunc extends JPanel implements ActionListener, MouseListener{
private final JRadioButton firstName;
private final JRadioButton lastName;
private final JButton deleteButton;
private JLabel firstNameLabel;
int x;
int y;
int firstOrLast = 1;
public benNameFunc(){
lastName = new JRadioButton("Last Name");
lastName.setPreferredSize(new Dimension(100, 50));
lastName.setForeground(Color.RED);
lastName.setActionCommand("last");
lastName.setSelected(false);
firstName = new JRadioButton("First Name");
firstName.setPreferredSize(new Dimension(100, 50));
firstName.setForeground(Color.RED);
firstName.setActionCommand("first");
firstName.setSelected(true);
JPanel palettePane = new JPanel(new BorderLayout());
palettePane.setPreferredSize(new Dimension (800, 500));
add(palettePane, BorderLayout.CENTER);
firstNameLabel = new JLabel("Ben");
firstNameLabel.setPreferredSize(new Dimension(100, 10));
firstNameLabel.setForeground(Color.WHITE);
firstNameLabel.setVisible(true);
palettePane.add(firstNameLabel);
buttonPane.setBackground(Color.BLACK);
firstName.addActionListener(this);
lastName.addActionListener(this);
deleteButton.addActionListener(this);
palettePane.addMouseListener(this);
}
然后是动作代码:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "first") {
firstOrLast = 1;
} else if (e.getActionCommand() == "last") {
firstOrLast = 2;
} else if (e.getSource() == deleteButton){
firstOrLast = 3;
}
}
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
switch (firstOrLast) {
case 1:
firstNameLabel.setLocation(x, y);
break;
case 2:
break;
case 3:
break;
default:
break;
}
...