我在 netbeans 中开发它,这意味着,当我创建我的 jFrame 表单时,那里已经有一些预先生成的代码。这里是:
import java.awt.Color;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Svist
*/
public class OsvetlenieForm extends javax.swing.JFrame {
/**
* Creates new form OsvetlenieForm
*/
public OsvetlenieForm() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel5 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton4 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Osvetlenie");
setMinimumSize(new java.awt.Dimension(900, 600));
setResizable(false);
getContentPane().setLayout(null);
jLabel5.setFont(new java.awt.Font("Tunga", 0, 18)); // NOI18N
jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText("Pohodlie");
getContentPane().add(jLabel5);
jLabel5.setBounds(10, 320, 60, 20);
jLabel4.setFont(new java.awt.Font("Tunga", 0, 18)); // NOI18N
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("Bezpecnost");
getContentPane().add(jLabel4);
jLabel4.setBounds(10, 280, 90, 20);
jLabel3.setFont(new java.awt.Font("Tunga", 0, 18)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("Kúrenie");
getContentPane().add(jLabel3);
jLabel3.setBounds(10, 240, 70, 20);
jLabel2.setFont(new java.awt.Font("Tunga", 0, 18)); // NOI18N
jLabel2.setForeground(new java.awt.Color(55, 117, 121));
jLabel2.setText("Osvetlenie");
getContentPane().add(jLabel2);
jLabel2.setBounds(10, 200, 80, 14);
jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/nic.png"))); // NOI18N
jButton4.setBorder(null);
jButton4.setBorderPainted(false);
jButton4.setContentAreaFilled(false);
jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton4MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton4MouseExited(evt);
}
});
getContentPane().add(jButton4);
jButton4.setBounds(0, 310, 180, 40);
jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/nic.png"))); // NOI18N
jButton3.setBorder(null);
jButton3.setBorderPainted(false);
jButton3.setContentAreaFilled(false);
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton3MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton3MouseExited(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(0, 270, 180, 40);
jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/nic.png"))); // NOI18N
jButton5.setBorder(null);
jButton5.setBorderPainted(false);
jButton5.setContentAreaFilled(false);
jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton5MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton5MouseExited(evt);
}
});
getContentPane().add(jButton5);
jButton5.setBounds(0, 190, 177, 39);
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/nic.png"))); // NOI18N
jButton2.setBorder(null);
jButton2.setBorderPainted(false);
jButton2.setContentAreaFilled(false);
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton2MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton2MouseExited(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(0, 230, 177, 39);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/menu.jpg"))); // NOI18N
getContentPane().add(jLabel1);
jLabel1.setBounds(0, 0, 900, 600);
pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();
setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
}// </editor-fold>
private void jButton2MouseEntered(java.awt.event.MouseEvent evt) {
jButton2.setIcon(new ImageIcon(getClass().getResource("resources/menu2.png")));
}
private void jButton2MouseExited(java.awt.event.MouseEvent evt) {
jButton2.setIcon(new ImageIcon(getClass().getResource("resources/nic.png")));
}
private void jButton3MouseEntered(java.awt.event.MouseEvent evt) {
jButton3.setIcon(new ImageIcon("src/menu_over.png"));
}
private void jButton3MouseExited(java.awt.event.MouseEvent evt) {
jButton3.setIcon(new ImageIcon("src/menu2.png"));
}
private void jButton4MouseEntered(java.awt.event.MouseEvent evt) {
jButton4.setIcon(new ImageIcon("src/menu_over.png"));
}
private void jButton4MouseExited(java.awt.event.MouseEvent evt) {
jButton4.setIcon(new ImageIcon("src/menu2.png"));
}
private void jButton5MouseEntered(java.awt.event.MouseEvent evt) {
jButton5.setIcon(new ImageIcon(getClass().getResource("resources/menu2.png")));
jLabel1.setForeground(Color.RED);
}
private void jButton5MouseExited(java.awt.event.MouseEvent evt) {
jButton5.setIcon(new ImageIcon(getClass().getResource("resources/nic.png")));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(OsvetlenieForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(OsvetlenieForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(OsvetlenieForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(OsvetlenieForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new OsvetlenieForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
// End of variables declaration
private static class ComponentImpl extends Component {
public ComponentImpl() {
}
}
}
我的按钮有一张“图标”图片。我希望此代码在“MouseEntered”时更改按钮的“图标”图像(这很好用),同时更改我的“jLabel1”的文本颜色,即“foreground(color)”参数。(这部分不起作用)。