[英]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.