Java 他的答案是什么?你把OP所说的导致问题的两行注释掉了?所以我不确定这一点answer@DavidKroukamp:在发表此评论时,问题是“如何在自定义按钮上设置文本?”我在自定义按钮上显示了文本。如果OP希望我使用图像。OP必须提供图像。无论如何,我的答案
Java 他的答案是什么?你把OP所说的导致问题的两行注释掉了?所以我不确定这一点answer@DavidKroukamp:在发表此评论时,问题是“如何在自定义按钮上设置文本?”我在自定义按钮上显示了文本。如果OP希望我使用图像。OP必须提供图像。无论如何,我的答案,java,swing,text,jbutton,Java,Swing,Text,Jbutton,他的答案是什么?你把OP所说的导致问题的两行注释掉了?所以我不确定这一点answer@DavidKroukamp:在发表此评论时,问题是“如何在自定义按钮上设置文本?”我在自定义按钮上显示了文本。如果OP希望我使用图像。OP必须提供图像。无论如何,我的答案是正确的。测试,测试,测试,测试。啊,我明白了。。。好吧,酷,我只是好奇,伙计!谢谢你的澄清。是的,测试、调试、注释和测试更多谢谢你的帮助!只是出于兴趣,这如何回答老年退休金计划的问题?你把OP所说的导致问题的两行注释掉了?所以我不确定这一点a
他的答案是什么?你把OP所说的导致问题的两行注释掉了?所以我不确定这一点answer@DavidKroukamp:在发表此评论时,问题是“如何在自定义按钮上设置文本?”我在自定义按钮上显示了文本。如果OP希望我使用图像。OP必须提供图像。无论如何,我的答案是正确的。测试,测试,测试,测试。啊,我明白了。。。好吧,酷,我只是好奇,伙计!谢谢你的澄清。是的,测试、调试、注释和测试更多谢谢你的帮助!只是出于兴趣,这如何回答老年退休金计划的问题?你把OP所说的导致问题的两行注释掉了?所以我不确定这一点answer@DavidKroukamp:在发表此评论时,问题是“如何在自定义按钮上设置文本?”我在自定义按钮上显示了文本。如果OP希望我使用图像。OP必须提供图像。无论如何,我的答案是正确的。测试,测试,测试,测试。啊,我明白了。。。好吧,酷,我只是好奇,伙计!谢谢你的澄清。是的,测试、调试、注释和测试更多谢谢你的帮助!答案很好,不需要像我一样扩展类+我用这个很容易申请。谢谢你的代码和解释,你真的帮了我很多!答案很好,不需要像我一样扩展类+我用这个很容易申请。谢谢你的代码和解释,你真的帮了我很多!非常感谢!非常感谢!
JButton Savebtn = new JButton();//("Save");
Savebtn.setFont(btnFont);
Savebtn.setOpaque(false);
Savebtn.setContentAreaFilled(false);
Savebtn.setBorder(null);
Savebtn.setMargin(new Insets(0, 0, 0, 0));
Savebtn.setIcon(new ImageIcon("src/Pic2/menubtn0.png"));
Savebtn.setPressedIcon(new ImageIcon("src/Pic2/menubtn1.png"));
//Savebtn.setText("Save");
JButton Savebtn = new JButton();//("Save");
Savebtn.setFont(btnFont);
Savebtn.setOpaque(false);
Savebtn.setContentAreaFilled(false);
Savebtn.setBorder(null);
Savebtn.setMargin(new Insets(0, 0, 0, 0));
Savebtn.setIcon(new ImageIcon("src/Pic2/menubtn0.png"));
Savebtn.setPressedIcon(new ImageIcon("src/Pic2/menubtn1.png"));
Savebtn.setText("Save");
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CustomButtonGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new CustomButtonGUI());
}
@Override
public void run() {
JFrame frame = new JFrame("Custom Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createButtonPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(300, 100));
Font font = panel.getFont().deriveFont(48f);
JButton saveButton = new JButton("Save");
saveButton.setFont(font);
saveButton.setOpaque(false);
saveButton.setContentAreaFilled(false);
saveButton.setBorder(null);
saveButton.setMargin(new Insets(0, 0, 0, 0));
// saveButton.setIcon(new ImageIcon("src/Pic2/menubtn0.png"));
// saveButton.setPressedIcon(new ImageIcon("src/Pic2/menubtn1.png"));
//
panel.add(saveButton, BorderLayout.CENTER);
return panel;
}
}
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
public class CustomJButton extends JButton {
private final BufferedImage normalIcon;
private final BufferedImage selectedIcon;
public CustomJButton(BufferedImage normalImage, BufferedImage selectedImage) {
super();
setOpaque(false);
setContentAreaFilled(false);
setBorder(null);
setFocusPainted(false);
setMargin(new Insets(0, 0, 0, 0));
this.normalIcon = normalImage;
this.selectedIcon = selectedImage;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(normalIcon.getWidth(), normalIcon.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// lets anti-alias for better quality
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// lets anti-alias for text too
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// lets draw the correct image depending on if the button is pressed or not
if (getModel().isPressed()) {
g2d.drawImage(selectedIcon, 0, 0, getWidth(), getHeight(), this);
} else {
g2d.drawImage(normalIcon, 0, 0, getWidth(), getHeight(), this);
}
// calc string x and y position
Font font = getFont();
String text = getText();
FontMetrics metrics = g2d.getFontMetrics(font);
int textWidth = metrics.stringWidth(text);
int textHeight = metrics.getHeight();
int textY = getWidth() / 2 - textWidth / 2;
int textX = getHeight() / 2 - textHeight / 2 + metrics.getAscent();
// draw the text
g2d.drawString(text, textY, textX);
}
}
CustomJButton button = new CustomJButton(ImageIO.read(new URL("https://i.stack.imgur.com/xCGQQ.png")), ImageIO.read(new URL("https://i.stack.imgur.com/R9i1s.png")));
button.setFont(new Font("Jokerman", Font.BOLD, 22));
button.setForeground(Color.WHITE);
button.setText("Press Me!");
button.addActionListener((ActionEvent e) -> {
JOptionPane.showMessageDialog(frame, "You pressed me");
});
JButton button = new JButton( "Save" );
button.setIcon(...);
button.setPressedIcon(...);
button.setHorizontalTextPosition(JLabel.CENTER);
button.setVerticalTextPosition(JLabel.CENTER);