这是我的JButton代码。我在设置此登录按钮的背景颜色时遇到问题
JButton btnlogin = new JButton("Log in");
btnlogin.setFont(new Font("Lucida Grande", Font.BOLD, 14));
btnlogin.setAlignmentX(Component.CENTER_ALIGNMENT);
btnlogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EmailValidator emailValidator = new EmailValidator();
if(!emailValidator.validate(txtEmail.getText().trim())) {
System.out.print("Invalid Email ID");
validationtext.setText("Invalid Email");
}
}
});
btnlogin.setBounds(210, 432, 200, 48);
btnlogin.setBackground(new Color(66, 185, 217));
frame.getContentPane().add(btnlogin);
我已经尝试将opaque值设置为true,但仍然无效。
您只需在设置背景颜色之前添加两行以获得预期结果:
btnlogin.setOpaque(true);
btnlogin.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
然后,您将获得预期的结果。