0

如何使用 java 中的链接更改 JLABEL 的颜色?

    import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class XXX extends JFrame {
    XXX(){

        final JLabel lab1=new JLabel("Username:");
        final JTextField text1=new JTextField(20);
           lab1.setBounds(20,140,65,20);
           text1.setBounds(85,141,185,20);
           add(lab1);
           add(text1);
           lab1.setForeground(Color.white);

        final JLabel lab2=new JLabel("Password:");
        final JPasswordField text2=new JPasswordField(20);
           lab2.setBounds(20,165,65,20);
           text2.setBounds(85,166,185,20);
           add(lab2);
           add(text2);
           lab2.setForeground(Color.white);

        final JButton a=new JButton("Sign In");
        a.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            //Code

            }
        });
        a.addMouseListener(new MouseAdapter() {
        public void mouseEntered(MouseEvent me) {
           a.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }
        public void mouseExited(MouseEvent me) {
           a.setCursor(Cursor.getDefaultCursor());
        }
        public void mouseClicked(MouseEvent me)
        {
            a.setEnabled(false);
            text1.setEditable(false);
            text2.setEditable(false);
          try {

            }
            catch(Exception e) {
               System.out.println(e);
            }
        }
        });
           a.setBounds(85,192,80,20);
           add(a);

        final String strURL = "http://www.yahoo.com";
        final JLabel lab3 = new JLabel("<html><a href=\" " + strURL + "\">Register</a></html>");
        lab3.setBounds(170,192,52,20);
        add(lab3);
        lab3.addMouseListener(new MouseAdapter() {
        public void mouseEntered(MouseEvent me) {
           lab3.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }
        public void mouseExited(MouseEvent me) {
           lab3.setCursor(Cursor.getDefaultCursor());
        }
        public void mouseClicked(MouseEvent me)
        {
           text2.setEditable(false);
          try {

            }
            catch(Exception e) {
               System.out.println(e);
            }
        }
        });


        final JLabel map = new JLabel(new ImageIcon(getClass().getResource("XXXBG.png")));
        map.setBounds(0,0,300,250);
        add(map);





            setTitle("XXX");
            setSize(300,250);
            setResizable(false);
            setCursor(DEFAULT_CURSOR);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocation(8, 8);
            setLayout(null);
            toFront();
            setVisible(true);
    }

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

如您所见,我无法更改 JLABEL lab3 的前景色。如果可能的话,我还想改变 jframe 的边框颜色。有人可以帮忙吗?

4

1 回答 1

3

是的,这是可能的。简单地提供foreground您想要使用的颜色...

lab3.setForeground(Color.BLUE);

您也不需要鼠标侦听器。lab3.setCursor(new Cursor(Cursor.HAND_CURSOR));当鼠标移到标签上时,简单地使用会自动更改鼠标光标......神奇地:D

更新

public class TestLabel01 {

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

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

                JLabel link = new JLabel("Linked in");
                link.setForeground(Color.BLUE);
                link.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));


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

}
于 2013-03-20T02:58:22.503 回答