2015-04-04 49 views
2

我的任務是通過單擊標籤來更改jlabel,但我們還沒有進入鼠標適配器和鼠標處理過程,但我們只重寫了5個鼠標事件,因此全部我們可以用。繼承人到目前爲止,但它所做的只是更改我的名字的字體,我希望它也可以在單擊我的名字時將JLabel的顏色更改爲紅色。嘗試使用MouseEvent更改JLabel

class FontFrame extends JFrame implements ActionListener{ 
JButton FirstFont; 
JButton SecondFont; 
JLabel myName; 

public FontFrame(){ 
setSize(600, 200); 
setLocation(300, 200); 
Container contentpane = getContentPane(); 
myName = new JLabel("Aly"); 
FirstFont = new JButton("Chalkboard 14"); 
SecondFont = new JButton("Harrington 18"); 
JPanel panel = new JPanel(); 
panel.add(FirstFont); 
panel.add(SecondFont); 
panel.add(myName); 
FirstFont.addActionListener(this); 
SecondFont.addActionListener(this); 
contentpane.add(panel, "Center"); 
} 

public void actionPerformed(ActionEvent e){ 
Object source = e.getSource(); 
Font f1 = new Font("Chalkboard", Font.PLAIN, 14); 
Font f2 = new Font("Harrington", Font.PLAIN, 18); 
if(source == FirstFont){ 
    myName.setFont(f1); 
} 
else if(source == SecondFont){ 
     myName.setFont(f2); 
    } 
} 
} 

class mouseFrame extends FontFrame implements MouseListener{ 

public mouseFrame(){ 
    addMouseListener(this); 
    myName.addMouseListener(this); 
    FirstFont.addMouseListener(this); 
    SecondFont.addMouseListener(this); 
    add(myName); 
    add(FirstFont); 
    add(SecondFont); 
    Container contentpane = getContentPane(); 
    contentpane.add(new FontFrame()); 
} 

public void mouseClicked(MouseEvent e) { 
    int fontbutton = e.getButton(); 
    if(fontbutton == MouseEvent.BUTTON1){ 
     myName.setForeground(Color.red); 
    } 
} 

public void mouseEntered(MouseEvent e) { 
    } 

public void mouseExited(MouseEvent e) { 
    } 

public void mousePressed(MouseEvent e) { 

} 
public void mouseReleased(MouseEvent e) { 
    } 
} 

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

回答

1
  1. 你從來沒有真正建立或顯示的MouseFrame
  2. MouseFrame實例不會調用FontFrame的構造(super()),而是嘗試的FontFrame一個新的實例添加到它,這是非法操作

開始通過改變MouseFrame所以它調用super構造函數,只增加了MouseListenerJLabel

public static class MouseFrame extends FontFrame implements MouseListener { 

    public MouseFrame() { 
     super(); 
     myName.addMouseListener(this); 
    } 

    public void mouseClicked(MouseEvent e) { 
     System.out.println("..."); 
     int fontbutton = e.getButton(); 
     if (fontbutton == MouseEvent.BUTTON1) { 
      myName.setForeground(Color.red); 
     } 
    } 

    public void mouseEntered(MouseEvent e) { 
    } 

    public void mouseExited(MouseEvent e) { 
    } 

    public void mousePressed(MouseEvent e) { 

    } 

    public void mouseReleased(MouseEvent e) { 
    } 

} 

接下來,在main方法,創建MouseFrame實例...

public static void main(String[] args) { 
    JFrame frame = new MouseFrame(); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
} 
+0

謝謝你這麼多,我確實做到了,你說什麼,它完美地工作了!我真的很感謝幫助:) – 2015-04-05 19:36:34

+0

很高興它可以幫助... – MadProgrammer 2015-04-05 21:18:09