-1

因此,我正在尝试做到这一点,以便选择“名字”或“姓氏”并在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;
}

...

4

1 回答 1

1
  1. palettePane当前处于布局管理器的控制之下,这意味着任何更改位置的尝试很可能会在重新验证容器时失败或被取代...将其布局管理器设置为null
  2. preferredSize当你想做你自己的布局控制时使用它不会有帮助。相反,您需要使用 componentssetSize方法...firstNameLabel.setSize(firstNameLabel.getPreferredSize());
  3. 你的String比较是错误的...

例如,你正在做...

if (e.getActionCommand() == "first") {
    firstOrLast = 1;
} else if (e.getActionCommand() == "last") {
    firstOrLast = 2;
} else if (e.getSource() == deleteButton) {
    firstOrLast = 3;
}

当你应该做...

if (e.getActionCommand().equals("first")) {
    firstOrLast = 1;
} else if (e.getActionCommand().equals("last")) {
    firstOrLast = 2;
} else if (e.getSource() == deleteButton) {
    firstOrLast = 3;
}

警告

null布局非常困难并且存在问题。非常确定这实际上是您想要做的。

例子

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class TestNameClick {

    public static void main(String[] args) {
        new TestNameClick();
    }

    public TestNameClick() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new BenNameFunc());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    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.setLayout(null);
            palettePane.setPreferredSize(new Dimension(800, 500));

            add(palettePane, BorderLayout.CENTER);

            firstNameLabel = new JLabel("Ben...");
            firstNameLabel.setSize(firstNameLabel.getPreferredSize());
//            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().equals("first")) {
                firstOrLast = 1;
            } else if (e.getActionCommand().equals("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;
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    }
}
于 2013-05-16T05:36:44.303 回答