Home  >  Article  >  类库下载  >  java swing remove focus box around button text

java swing remove focus box around button text

高洛峰
高洛峰Original
2016-10-14 13:35:252179browse

I had nothing to do, so I wrote a swing interface. After running it, I saw that when the button is clicked, a small box will appear in the middle text that just surrounds the text. This is a sign that the button has gained focus. I think one word: ugly! How to remove it? The almighty Du Niang told me to set the setFocusPainted of the button to false. I tried it and it was ok. I will share the code with you below. You can comment out the sentence about setting the attributes to see the before and after effects.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class SwingDemo {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        JFrame jframe = new JFrame("Demo");
        JButton button = new JButton("JB");
        button.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("click JB");
            }
        });
        //去掉按钮文字周围的焦点框
        button.setFocusPainted(false);
        
        jframe.getContentPane().add(button);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        jframe.setBounds(100, 100, 200, 136);
        
        jframe.setVisible(true);
    }

}

It’s not enough for people who work in IT to just look at it. If you do more, you will gain something.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn