繁体   English   中英

不使用图像的丰富 JButton

[英]Rich JButton without using images

我花了我大部分的时间试图模仿材料的设计外观(因为我真的很喜欢它),如图所示的按钮,在这里

我正在努力使Jbutton边框变圆为按钮添加阴影 我找到了一种处理圆形边框的方法,但是 JButton 的背景,但它并没有停留在这个范围内。

public class LoginView {  
private Component mainPanel;
public static void main(String[] args){

    JFrame frame = new JFrame("Sign In to eVenture Books");
    frame.setSize(350, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    frame.add(panel);
    placeComponents(panel);
    frame.setVisible(true);  

    }//End of Main()

private static class RoundedBorder implements Border {
private int radius;
RoundedBorder(int radius) {
    this.radius = radius;
}

public Insets getBorderInsets(Component c) {
    return new Insets(this.radius+1, this.radius+1, this.radius+1, this.radius+1);
}

public boolean isBorderOpaque() {
    return true;
}


public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    g.drawRoundRect(x, y, width-1, height-1, radius, radius);
}
}//End of Class RoundedBorder


private static void placeComponents(JPanel panel) {

    panel.setLayout(null);

    JLabel userLabel = new JLabel("Username :");
    userLabel.setBounds(10, 10, 80, 25);
    panel.add(userLabel);

    JTextField userText = new JTextField(20);
    userText.setBounds(100, 10, 160, 25);
    panel.add(userText);

    JLabel passwordLabel = new JLabel("Password :");
    passwordLabel.setBounds(10, 40, 80, 25);
    panel.add(passwordLabel);

    JPasswordField passwordText = new JPasswordField(20);
    passwordText.setBounds(100, 40, 160, 25);
    panel.add(passwordText);

    JButton loginButton = new JButton("LOGIN");
    loginButton.setBounds(10, 80, 80, 25);
            loginButton.setBackground(new Color(0xF06292));
            //loginButton.setBorder(new RoundedBorder(5));
            loginButton.setMargin(new Insets(0, 0, 0, 0));
            loginButton.setFont(new Font("Arial", Font.PLAIN, 15));
            //loginButton.setForeground(new Color(0xFAFAFA));
            //loginButton.setBorderPainted(false);
    panel.add(loginButton);

    JButton registerButton = new JButton("Register");
    registerButton.setBounds(180, 80, 80, 25);
            registerButton.setBackground(new Color(0xF06292));
            registerButton.setBorder(new RoundedBorder(5));
    panel.add(registerButton);

            JButton quitButton = new JButton("Quit");
    quitButton.setBounds(95, 80, 80, 25);
            quitButton.setBackground(new Color(0x757575));
            //quitButton.setBorder(BorderFactory.createSoftBevelBorder(BevelBorder.RAISED)); 
            //quitButton.setBorder(new RoundedBorder(5));
            // quitButton.setOpaque(false);
    panel.add(quitButton);

            JLabel titleLabel = new JLabel("Sign in to eVenture Books");
            titleLabel.setBounds(10, 120, 200, 25);
            titleLabel.setFont(new java.awt.Font("Freestyle Script", 1, 18)); 
            titleLabel.setText("Sign In to eVenture Books");
            panel.add(titleLabel);
}
}

您需要扩展一个 JButton 类,然后在扩展的类中重写方法paint(Graphics g)。

在此方法中,使用 Graphics g 对象绘制按钮。 您可以将 g Graohics 转换为 Graphics2D 以获得更多绘图方法。 在paint() 中,您可以对按钮的尺寸使用getWidth() 和getHeight() 方法。 要绘制阴影和/或圆形按钮,您需要绘制比他的边界更小的按钮,并在周围的自由空间中绘制一些阴影,以便阴影保持按钮的一部分。 JButton 的边界总是矩形,您可以通过在 JButton 内部绘制实心圆来使 JButton 看起来更圆润。 不要在你的油漆中调用 super.paint() 。 此外,您可能需要在 Button 上调用 setOpaque(false),这确保在绘制 JButton 之前重新绘制他的背景,以便您可以绘制一些透明部分(阴影和圆圈需要瞬态区域)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM