Created
December 22, 2013 23:18
-
-
Save resarahadian/8089614 to your computer and use it in GitHub Desktop.
Membuat Transparan JTextField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JPanel; | |
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
@SuppressWarnings("serial") | |
public class ClPanelTransparant extends JPanel | |
{ | |
private Color clr; | |
public ClPanelTransparant() | |
{ | |
setOpaque(false); | |
clr = new Color(getBackground().getRed(), getBackground().getRed(), getBackground().getGreen(),50); | |
} | |
public void setBackground(Color bg) | |
{ | |
super.setBackground(bg); | |
clr = new Color(getBackground().getRed(), getBackground().getRed(), getBackground().getGreen(),50); | |
repaint(); | |
} | |
protected void paintComponent(Graphics graph) | |
{ | |
super.paintComponent(graph); | |
Graphics2D g2d = (Graphics2D) graph.create(); | |
g2d.setColor(clr); | |
g2d.fillRect(0, 0, getWidth(), getHeight()); | |
g2d.dispose(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.AlphaComposite; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import javax.swing.JTextField; | |
@SuppressWarnings("serial") | |
public class ClTransparanTextField extends JTextField { | |
public ClTransparanTextField(String text) { | |
super(text); | |
init(); | |
} | |
public ClTransparanTextField(int columns) { | |
super(columns); | |
init(); | |
} | |
public ClTransparanTextField(String text, int columns) { | |
super(text, columns); | |
init(); | |
} | |
protected void init() { | |
setOpaque(false); | |
} | |
@Override | |
public void paint(Graphics g) { | |
Graphics2D g2d = (Graphics2D) g.create(); | |
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f)); | |
super.paint(g2d); | |
g2d.dispose(); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
Graphics2D g2d = (Graphics2D) g.create(); | |
g2d.setColor(getBackground()); | |
g2d.fillRect(0, 0, getWidth(), getHeight()); | |
super.paintComponent(g2d); | |
g2d.dispose(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.EventQueue; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.UIManager; | |
import javax.swing.border.EmptyBorder; | |
import javax.swing.border.TitledBorder; | |
import javax.swing.JLabel; | |
import javax.swing.JTextField; | |
import javax.swing.JComboBox; | |
import javax.swing.DefaultComboBoxModel; | |
import javax.swing.JButton; | |
import javax.swing.ImageIcon; | |
import javax.swing.SwingConstants; | |
import java.awt.Font; | |
import java.awt.Color; | |
@SuppressWarnings("serial") | |
public class frameTextTransparan extends JFrame { | |
private JPanel contentPane; | |
private JPanel panel; | |
private JLabel lblIdBuku; | |
private JTextField txtId; | |
private JTextField txtJudul; | |
@SuppressWarnings("rawtypes") | |
private JComboBox cbKategori; | |
private JLabel lblJudul; | |
private JLabel lblKategori; | |
private JLabel lblStock; | |
private JTextField txtStock; | |
private JButton btnSimpan; | |
private JButton btnUbah; | |
private JButton btnHapus; | |
private JLabel lblWall; | |
/** | |
* Create the frame. | |
*/ | |
@SuppressWarnings({ "rawtypes", "unchecked" }) | |
public frameTextTransparan() | |
{ | |
setTitle("TextField Transparan"); | |
setResizable(false); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setBounds(100, 100, 654, 412); | |
contentPane = new JPanel(); | |
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | |
setContentPane(contentPane); | |
contentPane.setLayout(null); | |
panel = new ClPanelTransparant(); | |
panel.setBorder(new TitledBorder(null, "Data Buku", TitledBorder.RIGHT, TitledBorder.TOP, null, new Color(255, 255, 255))); | |
panel.setBounds(12, 88, 453, 237); | |
contentPane.add(panel); | |
panel.setLayout(null); | |
lblIdBuku = new JLabel("Id Buku : "); | |
lblIdBuku.setForeground(new Color(255, 255, 255)); | |
lblIdBuku.setFont(new Font("Comic Sans MS", Font.BOLD, 12)); | |
lblIdBuku.setBounds(16, 24, 60, 15); | |
panel.add(lblIdBuku); | |
txtId = new ClTransparanTextField(""); | |
txtId.setFont(new Font("Comic Sans MS", Font.BOLD, 12)); | |
txtId.setBounds(112, 18, 155, 27); | |
panel.add(txtId); | |
txtId.setColumns(10); | |
lblJudul = new JLabel("Judul : "); | |
lblJudul.setForeground(new Color(255, 255, 255)); | |
lblJudul.setFont(new Font("Comic Sans MS", Font.BOLD, 12)); | |
lblJudul.setBounds(16, 62, 60, 15); | |
panel.add(lblJudul); | |
txtJudul = new ClTransparanTextField(""); | |
txtJudul.setFont(new Font("Comic Sans MS", Font.BOLD, 12)); | |
txtJudul.setBounds(112, 56, 324, 27); | |
panel.add(txtJudul); | |
txtJudul.setColumns(10); | |
lblKategori = new JLabel("Kategori : "); | |
lblKategori.setForeground(new Color(255, 255, 255)); | |
lblKategori.setFont(new Font("Comic Sans MS", Font.BOLD, 12)); | |
lblKategori.setBounds(16, 105, 72, 15); | |
panel.add(lblKategori); | |
cbKategori = new JComboBox(); | |
cbKategori.setModel(new DefaultComboBoxModel(new String[] {"== Pilih ==", "Novel", "Cerpen"})); | |
cbKategori.setBounds(112, 94, 187, 36); | |
panel.add(cbKategori); | |
lblStock = new JLabel("Stock : "); | |
lblStock.setForeground(new Color(255, 255, 255)); | |
lblStock.setFont(new Font("Comic Sans MS", Font.BOLD, 12)); | |
lblStock.setBounds(16, 155, 60, 15); | |
panel.add(lblStock); | |
txtStock = new ClTransparanTextField(""); | |
txtStock.setFont(new Font("Comic Sans MS", Font.BOLD, 12)); | |
txtStock.setBounds(112, 149, 122, 27); | |
panel.add(txtStock); | |
txtStock.setColumns(10); | |
btnSimpan = new ClButtonTransparant("Simpan"); | |
btnSimpan.setFont(new Font("Comic Sans MS", Font.BOLD, 12)); | |
btnSimpan.setHorizontalTextPosition(SwingConstants.CENTER); | |
btnSimpan.setVerticalTextPosition(SwingConstants.BOTTOM); | |
btnSimpan.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingJava/src/transparanTextField/simpan.png")); | |
btnSimpan.setBounds(12, 6, 117, 56); | |
contentPane.add(btnSimpan); | |
btnUbah = new ClButtonTransparant("Ubah"); | |
btnUbah.setFont(new Font("Comic Sans MS", Font.BOLD, 12)); | |
btnUbah.setHorizontalTextPosition(SwingConstants.CENTER); | |
btnUbah.setVerticalTextPosition(SwingConstants.BOTTOM); | |
btnUbah.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingJava/src/transparanTextField/ubah.png")); | |
btnUbah.setBounds(155, 6, 125, 56); | |
contentPane.add(btnUbah); | |
btnHapus = new ClButtonTransparant("Hapus"); | |
btnHapus.setFont(new Font("Comic Sans MS", Font.BOLD, 12)); | |
btnHapus.setHorizontalTextPosition(SwingConstants.CENTER); | |
btnHapus.setVerticalTextPosition(SwingConstants.BOTTOM); | |
btnHapus.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingJava/src/transparanTextField/hapus.png")); | |
btnHapus.setBounds(308, 6, 125, 56); | |
contentPane.add(btnHapus); | |
lblWall = new JLabel(""); | |
lblWall.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingJava/src/transparanTextField/wall.jpg")); | |
lblWall.setBounds(0, -11, 652, 397); | |
contentPane.add(lblWall); | |
setLocationRelativeTo(null); | |
} | |
/** | |
* Launch the application. | |
*/ | |
public static void main(String[] args) { | |
EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
try { | |
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); | |
frameTextTransparan frame = new frameTextTransparan(); | |
frame.setVisible(true); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment