2013-03-12 55 views
-1

我有一種情況,我把JLabel放入JButton中,並調整JButton的大小。使JButton可點擊而不是JLabel

這裏的問題是每次點擊按鈕時,JLabel都會捕獲大部分事件。

當我試圖將ActionListener添加到JButton時,它不起作用。

但是,當我試圖將MouseListener添加到JLabel,所有的事件處理程序工作。

我想讓JButton的ActionListener工作。我不希望JLabel捕獲所有事件而不破壞我的默認配置。

我試着將JLabel focusable屬性設置爲false,但它也不起作用。

那麼我該怎麼做呢?

+0

爲什麼你需要一個在一個按鈕標籤? – MadProgrammer 2013-03-12 07:12:21

+3

你可以在'JButton'上調用'setText',它可以讓你做任何你可以用標籤做的事情。你爲什麼需要標籤? – DaoWen 2013-03-12 07:15:16

+0

要擺脫圍繞文本框點擊按鈕和UI修改 – 2013-03-12 07:23:51

回答

2

我有一種情況,我把JLabel放在JButton裏面,並且調整了JButton的尺寸 。

這是基本的屬性,默認情況下頂奠定JComponent消耗的所有事件從Mouse來到& Keyboard

有兩種方式

  • (不知道爲什麼會出現JLabel)如果有可能使用原始JButton與API中實施的方法相反

  • 添加MouseListener(也許有沒有理由覆蓋所有MouseEvents只添加MouseAdapter),以JLabelmouseClicked調用JButton.doClick()

編輯

@Mad,

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.util.Random; 
import javax.swing.*; 

public class JButtonAndIcon { 

    private JLabel label = new JLabel(); 
    private Random random = new Random(); 
    private ImageIcon image1; // returns null don't worry about in Swing 
    private ImageIcon image2; // returns null don't worry about in Swing 
    private Timer backTtimer; 
    private int HEIGHT = 300, WEIGHT = 200; 


    public JButtonAndIcon() { 
     label.setPreferredSize(new Dimension(HEIGHT, WEIGHT)); 
     final JButton button = new JButton("Push"); 
     button.setBorderPainted(false); 
     button.setBorder(null); 
     button.setFocusable(false); 
     button.setMargin(new Insets(0, 0, 0, 0)); 
     button.setContentAreaFilled(false); 
     button.setLayout(new BorderLayout()); 
     button.add(label); 
     button.setMultiClickThreshhold(1000); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (button.getIcon() == image1) { 
        label.setIcon(image2); 
       } else { 
        label.setIcon(image1); 
        if(backTtimer.isRunning()){ 
         backTtimer.restart(); 
        }     
       } 
      } 
     }); 
     JFrame frame = new JFrame("Test"); 
     frame.add(button); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     startBackground(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) throws IOException { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JButtonAndIcon t = new JButtonAndIcon(); 
      } 
     }); 
    } 

    private void startBackground() { 
     backTtimer = new javax.swing.Timer(1500, updateBackground()); 
     backTtimer.start(); 
     backTtimer.setRepeats(true); 
    } 

    private Action updateBackground() { 
     return new AbstractAction("Background action") { 

      private final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       label.setIcon(new ImageIcon(getImage())); 
      } 
     }; 
    } 

    public BufferedImage getImage() { 
     int w = label.getWidth(); 
     int h = label.getHeight(); 
     GradientPaint gp = new GradientPaint(0f, 0f, new Color(
       127 + random.nextInt(128), 
       127 + random.nextInt(128), 
       127 + random.nextInt(128)), 
       w, w, 
       new Color(random.nextInt(128), random.nextInt(128), random.nextInt(128))); 
     BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
     Graphics2D g2d = bi.createGraphics(); 
     g2d.setPaint(gp); 
     g2d.fillRect(0, 0, w, h); 
     g2d.setColor(Color.BLACK); 
     return bi; 
    } 
} 
+0

如果1-標籤有聽衆註冊(或工具提示)並且2-不透明 – MadProgrammer 2013-03-12 07:18:22

+0

@MadProgrammer默認情況下(如果使用LayoutManager),那麼情況不是這樣,沒有任何問題,請參閱我的編輯,全部事件被註冊,分發給普通的JButton,我懶得要求OP的努力...... – mKorbel 2013-03-12 07:34:21