a. Create a new project and name Crazy TextField
b. Create a new Package and name paketTextfield, and now we already have two Package / Folder different, right ?
Screenshot
c. Now, create a class for Transparent textField component, just create a new Java Class in package paketTextField and name TextBoxTransparan.
d. On TextBoxTransparan.java source tab, enter the syntax / code, like this !
package paketTextfield;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
/**
*
* @author CodeMaster
*/ // MASUNDUH II
public class TextBoxTransparan extends JTextField{
public TextBoxTransparan()
{
setOpaque(false);
setBorder(new EmptyBorder(5, 10, 5, 10));
setHorizontalAlignment(0);
addMouseListener(new MouseAdapter()
{
@Override
public void mouseEntered(MouseEvent e)
{
TextBoxTransparan.this.setCursor(Cursor.getPredefinedCursor(2));
}
@Override
public void mouseExited(MouseEvent e)
{
TextBoxTransparan.this.setCursor(Cursor.getPredefinedCursor(0));
}
});
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (isEnabled()) {
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color dark = new Color(1.0F, 1.0F, 1.0F, 0.0F);
Color light = new Color(1.0F, 1.0F, 1.0F, 0.3F);
GradientPaint paint = new GradientPaint(0.0F, 0.0F, light, 0.0F, getHeight() / 2, dark);
g2.setPaint(paint);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), getHeight(), getHeight());
g2.setColor(Color.BLACK);
g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getHeight(), getHeight());
g2.dispose();
} else {
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color light = new Color(1.0F, 1.0F, 1.0F, 0.3F);
g2.setColor(light);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), getHeight(), getHeight());
g2.setColor(Color.BLACK);
g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getHeight(), getHeight());
g2.dispose();
}
}
}
e. And now, again to create a new class JTextField component Gradient, just create a new Java Class in package paketTextField and name TextBoxGradient.
f. On TextBoxGradient.java source tab, and enter the syntax / code, like this !
f. On TextBoxGradient.java source tab, and enter the syntax / code, like this !
package paketTextfield;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
/**
*
* @author CodeMaster
*/ // MASUNDUH II
public class TextBoxTransparan extends JTextField{
public TextBoxTransparan()
{
setOpaque(false);
setBorder(new EmptyBorder(5, 10, 5, 10));
setHorizontalAlignment(0);
addMouseListener(new MouseAdapter()
{
@Override
public void mouseEntered(MouseEvent e)
{
TextBoxTransparan.this.setCursor(Cursor.getPredefinedCursor(2));
}
@Override
public void mouseExited(MouseEvent e)
{
TextBoxTransparan.this.setCursor(Cursor.getPredefinedCursor(0));
}
});
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (isEnabled()) {
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color dark = new Color(1.0F, 1.0F, 1.0F, 0.0F);
Color light = new Color(1.0F, 1.0F, 1.0F, 0.3F);
GradientPaint paint = new GradientPaint(0.0F, 0.0F, light, 0.0F, getHeight() / 2, dark);
g2.setPaint(paint);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), getHeight(), getHeight());
g2.setColor(Color.BLACK);
g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getHeight(), getHeight());
g2.dispose();
} else {
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color light = new Color(1.0F, 1.0F, 1.0F, 0.3F);
g2.setColor(light);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), getHeight(), getHeight());
g2.setColor(Color.BLACK);
g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getHeight(), getHeight());
g2.dispose();
}
}
}
h. Create new JFrame Form crazytextfield package, and named MainForm
i. On the design tab, pull / drag TextBoxTransparan.java class, and
TextBoxGradient.java,
j. Set up your display. lol
k. And Run Program or press Shift + F6,
* screenshot below, there are some components Glass Panel (panelKaca) class, such as tutorials ever before, and use JTattoe theme, the next post I will share it.
its easy guys, hope it is useful !
please write comment and share this !
I'm confused about the g. and i. steps! How do they work on Eclipse? it's possible to apply this to a JFormattedTextField?
ReplyDelete