1

カスタム JButton を作成しようとするこの小さなプログラムを作成しましたが、残念ながら境界線を削除できません。button.setBorder(null); と思いました。削除しますが、これは効果がありませんでした。ボタンから境界線を削除して単なるアイコンにする方法を知っている人はいますか? どんな助けでも大歓迎です。

私のコードは次のとおりです。

package custombuttons;

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.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CustomButtons {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        CustomButtons h = new CustomButtons();
        h.setUp();
    }

    JFrame frame;
    JPanel panel;
    JButton button;
    BufferedImage b;
    String toolTip = "Configure";

    public void setUp() {
        frame = new JFrame("Custom Buttons");
        try {
            b = ImageIO.read(CustomButtons.class.getResource("/images/config.png"));
        } catch (IOException ex) {
            Logger.getLogger(CustomButtons.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }

        Image b1 = (Image) b;
        ImageIcon iconRollover = new ImageIcon(b1);
        int w = iconRollover.getIconWidth();
        int h = iconRollover.getIconHeight();
        GraphicsConfiguration gc = frame.getGraphicsConfiguration();
        Image image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.drawImage(iconRollover.getImage(), 0, 0, null);
        g.dispose();

        ImageIcon iconDefault = new ImageIcon(b1);
        image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
        g = (Graphics2D) image.getGraphics();
        g.drawImage(iconRollover.getImage(), 2, 2, null);
        g.dispose();
        ImageIcon iconPressed = new ImageIcon(b1);

        JButton button = new JButton();
        button.setIgnoreRepaint(true);
        button.setFocusable(false);
        button.setToolTipText(toolTip);
        button.setBorder(null);
        button.setContentAreaFilled(false);
        button.setIcon(iconDefault);
        button.setRolloverIcon(iconRollover);
        button.setPressedIcon(iconPressed);

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        panel = new JPanel();
        panel.setOpaque(false);
        panel.add(button);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
    }
}
4

2 に答える 2

3

ここでJButtonの詳細を見てくださいbutton.setBorderPainted(false)

于 2012-06-22T18:48:03.823 に答える
0

実際に私は自分の Netbeans IDE であなたのコードをテストしましたが、あなたが望むようにbutton.setBorder(null);orbutton.setBorderPainted(false);またはのみを使用して境界線を取得both of themしませんでしたが、元のイメージに実際に境界線がないことを確認する必要があると思います

于 2012-06-22T19:00:57.047 に答える