Rich JButton没有使用图像

时间:2015-11-02 11:34:27

标签: java jbutton material

我花了大部分时间试图模仿Material Design外观(我非常喜欢),如按钮here所示:

我正在努力使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);
}
}

1 个答案:

答案 0 :(得分:0)

你需要扩展一个JButton类,然后扩展calss覆盖方法paint(Graphics g)。

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

相关问题