I am pretty new to java swing and not familiar with paint(). I want to create a button in java swing with above look. Can anyone help me to do this. Any guidance would be grateful. Thanks in advance
First, set your look and feel to something cool, like 'metal'. Then do something like this: my_jbutton. setBackground(new Color(240,240,241);
Now, to use this method in your example code: JFrame frame2 = new JFrame("Tauler Joc"); JPanel panell = new JPanel(); ImageIcon icon = new ImageIcon("king. jpg"); JButton jb= new JButton(); jb. setBounds(200,200,700,700); panell.
I googled the Facebook blue RGB: 59, 89, 182/Hex Code is #3B5998 and Font family: Tahoma.
using that here is what I got with a few calls like setFocusPainted(false)
,setBackground(new Color(59, 89, 182))
and setFont(new Font("Tahoma", Font.BOLD, 12))
:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Test {
public Test() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JButton b = new JButton("Log In");//http://www.chacha.com/question/what-are-the-rgb-values-for-the-background-color-of-comments-on-facebook
b.setBackground(new Color(59, 89, 182));
b.setForeground(Color.WHITE);
b.setFocusPainted(false);
b.setFont(new Font("Tahoma", Font.BOLD, 12));//http://answers.yahoo.com/question/index?qid=20070906133202AAOvnIP
frame.add(b);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Test();
}
});
}
}
unless you are looking for identical (which IMO this is about as best as it gets without using actual image)... than setting the image of the button would be the best way
If you want to completely override the look of your button, the most general solution is to create your own ButtonUI
:
class MyButton extends BasicButtonUI {
@Override
public void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
...
}
}
You can then paint whatever you want, taking into account the state of your button (rollover, focused, armed, pressed, etc). Take a look at the superclass implementation for basic ideas on how to do this.
Then just set the UI of the button you want to change:
button.setUI(new MyButton());
To create a customized button like your example, I think the best way is preparing a graphics document (image etc.) and then setting it as a property of your button:
JButton button = new JButton();
button.setIcon(new ImageIcon("yourButtonImage.jpg"));
On Oracle javadoc, you can see jbutton javadoc.
Jbutton java method setIcon(Icon)
with ImageIcon
implementation will do the trick !
To create a customized button shown in your example, I think use the following code:-
JButton button = new JButton("Log In");
button.setFont(new Font("Serif",Font.BOLD,20));
button.setBackground(new Color(0,51,204));//import java.awt.Color;
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setBorderPainted(false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With