I am currently doing GUI development in JavaSwing
.
I am trying to create a button
like the HTML
code described below using JButton
, which was added to getContentPane()
, but I don't know how to do it and am at a loss.
Current Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("TITLE");
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 800);
frame.getContentPane().setLayout(new FlowLayout());
JButton button = new JButton("Click Here");
frame.getContentPane().add(button);
button.setBackground(Color.RED);
button.setForeground(Color.WHITE);
button.setOpaque(true);
button.setPreferredSize(new Dimension(150, 50));
}
}
The entity I am trying to create is a little hard to explain by mouth, so I have tried to reproduce what I am trying to create using HTML. It is as follows:
HTML CODE:
<div class="btn">Click Here</div>
<style>
.btn {
background-color: red;
display: inline-block;
height: 70px;
width: 150px;
text-align: center;
color: white;
padding-top: 35px;
font-size: 25px;
border-radius: 50px;
box-shadow: 0px 5px #b70d0b;
opacity: 0.7;
cursor: pointer;
}
.btn:hover {
opacity: 1;
}
.btn:active{
position: relative;
top: 5px;
box-shadow: none;
}
</style>
This is what it looks like when executed: The following image is my ideal in JavaSwing
.
The question this time is:
Setting
opacity
toJButton
inJavaSwing
To set
border-radius
tobutton
To set
opacity
to 1 only when the mousehovers
over thebutton
andMouseEvent
detects it.Set
box-shadow
to5px
, and useActionListener
to detect the interval of active, and make it look like abutton
is being pressed.To reproduce the
cursor:pointer
code while the mouse is hovering over thebutton
.
I am not sure if this is too much to ask or not. I am fully aware that there are too many questions, so I would appreciate it if you could answer only the ones that I can answer.
In the meantime, I have done my own Google research on the question, but none of it fits my ideal, so I am asking it here.
As for what I did, here's what I did:
I looked here, but when I enter a decimal point, like setOpacity(0.5)
, I get an error that setOpacity(double) is undefined
.
I referred to the border-radius here, but nothing happened when I ran it. Perhaps there is a problem with my writing style or version. I think there is a problem with the way I wrote it or the version, but I could not find out the cause.
I tried copying the MouseEvent
for the hover here, but the version may be different, or an error occurred.
So, what code should I write to achieve what I am asking? I would appreciate it if you could help me.
Thank you.