I am messing around with some ideas for a side project and I would like to create a GUI using Java swing that doesn't look like it is from Windows95. One of the ideas I was kicking around was to use JLabels as buttons instead of the standard JButton. This would allow me to customize hover, drag, and movement effects as I like.
Research into the MouseAdapter class
should allow me to do everything I intend, unfortunately I am having some trouble implementing the hover effect as I wanted as the JLabel
does not appear to update. I have tried updating the Frame directly by calling frame.update(getGraphics());
but that does not appear to work as I think it does.
Can I get some advice on how to update the label properly.
Note: This is just an example with no effort put in to organize the code efficiently
public class Window extends JFrame {
/**
*
*/
private static final long serialVersionUID = 5259700796854880162L;
private JTextField textField;
private JLabel lblNewLabel;
static Window frame;
int i = 0;
public Window() {
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
lblNewLabel = new JLabel("New label");
lblNewLabel.setBackground(Color.LIGHT_GRAY);
lblNewLabel.setBounds(137, 38, 114, 70);
panel.add(lblNewLabel);
lblNewLabel.addMouseListener(new LabelAdapter());
textField = new JTextField();
textField.setBounds(122, 119, 86, 20);
panel.add(textField);
textField.setColumns(10);
}
private class LabelAdapter extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(String.valueOf(i));
i++;
}
@Override
public void mouseEntered(MouseEvent e) {
lblNewLabel.setBackground(Color.CYAN);
}
@Override
public void mouseExited(MouseEvent e) {
lblNewLabel.setBackground(Color.LIGHT_GRAY);
}
}
/**
* @param args
*/
public static void main(String[] args) {
frame = new Window();
frame.setSize(900, 700);
frame.setVisible(true);
}
}