我刚刚在Java应用程序中实现了Windows外观,但是所有按钮都失去了背景色。经过一番研究,我发现我不得不调用方法“ buttonName.setContentAreaFilled(false);”。随后是“ buttonName.setBackground(Color.blue);”。一切按预期进行,直到我意识到按钮失去了输入点击。
package f.h.projects;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
//import control.Control;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
//import negocios.Usuario;
public class FHProjects extends javax.swing.JFrame implements ActionListener {
private JLabel lCanvas, lLogo, lUsername, lPassword;
private JTextField jUsername, jPassword;
private JButton btnLogin;
private BufferedImage img = null;
//private ProjectsMenu proye;
//private Control ctrl;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new FHProjects().setVisible(true);
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
}
});
}
public FHProjects() throws ClassNotFoundException {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (InstantiationException ex) {
Logger.getLogger(FHProjects.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(FHProjects.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(FHProjects.class.getName()).log(Level.SEVERE, null, ex);
}
//ctrl = new Control();
/**
// try {
/**
setContentPane(lCanvas = new JLabel());
add(lLogo = new JLabel());
lLogo.setBounds(250, 50, 200, 200);
img = ImageIO.read(new File("src/src/mainLogo.png"));
Image dimg1 = img.getScaledInstance(lLogo.getWidth(), lLogo.getHeight(), Image.SCALE_SMOOTH);
ImageIcon imageIcon1 = new ImageIcon(dimg1);
lLogo.setIcon(imageIcon1);
} catch (IOException e) {
System.out.println(e.getMessage());
}
*/
lCanvas.setOpaque(true);
lCanvas.setBackground(java.awt.Color.WHITE);
lCanvas.add(lUsername = new JLabel());
lCanvas.add(lPassword = new JLabel());
lCanvas.add(jUsername = new JTextField());
lCanvas.add(jPassword = new JPasswordField());
lCanvas.add(btnLogin = new JButton());
lUsername.setText("User: ");
lPassword.setText("Password: ");
btnLogin.setText("Login");
btnLogin.setFocusPainted(false);
btnLogin.setBorderPainted(false);
btnLogin.setContentAreaFilled(false);
btnLogin.setOpaque(true);
lUsername.setFont(new java.awt.Font("Tahoma", 1, 20));
lPassword.setFont(new java.awt.Font("Tahoma", 1, 20));
btnLogin.setFont(new java.awt.Font("Tahoma", 1, 15));
btnLogin.setBackground(java.awt.Color.red);
btnLogin.setForeground(java.awt.Color.black);
btnLogin.setBorderPainted(false);
lUsername.setBounds(200, 280, 100, 30);
lUsername.setHorizontalAlignment(SwingConstants.RIGHT);
lPassword.setBounds(153, 330, 147, 30);
lPassword.setHorizontalAlignment(SwingConstants.RIGHT);
jUsername.setBounds(350, 280, 200, 30);
jPassword.setBounds(350, 330, 200, 30);
btnLogin.setBounds(300, 400, 100, 30);
btnLogin.addActionListener(this);
jUsername.setText("developer");
jUsername.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
login();
}
}
});
jPassword.setText("123");
jPassword.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
login();
}
}
});
setTitle("F&H Projects");
setSize(700, 500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//setIconImage(Toolkit.getDefaultToolkit().getImage("src/src/mainLogo.png"));
}
public void login() {
boolean accedio = false;
/**if (ctrl.login(jUsername.getText(), jPassword.getText()) != null) {
Usuario user = ctrl.login(jUsername.getText(), jPassword.getText());
setVisible(false);
proye = new ProjectsMenu(this, true, user, ctrl);
accedio = true;
} else if (accedio == false) {
JOptionPane.showMessageDialog(this, "Username or password is incorrect!", "Error!", JOptionPane.ERROR_MESSAGE);
}
*/
System.out.println("Success!");
}
@Override
public void actionPerformed(ActionEvent e) {
JButton obj = (JButton) e.getSource();
if (obj == btnLogin) {
login();
}
}
}
按钮可以按预期工作,但令我感到困扰的是没有单击该输入。如果我记录方法“ buttonName.setContentAreaFilled(false);”或将参数设置为true,则输入的点击会再次出现,但我会松开背景色。
是否可以在JButton中同时具有背景色和输入单击?