Java Learning

Saturday, September 8, 20120 comments


// here is code ,please try it!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowSwingColor extends JFrame implements ActionListener{
private JTextField tfr,tfg,tfb;
private JButton btshow;
private JTextArea tadisplay;
public ShowSwingColor(){
initGUI();
}
public void initGUI(){
JPanel p1=new JPanel();p1.setLayout(new GridLayout(3,2));
p1.add(new JLabel(“Red Value”,0));p1.add(tfr=new JTextField(12));
p1.add(new JLabel(“Green Value”,0));p1.add(tfg=new JTextField(12));
p1.add(new JLabel(“Blue Value”,0));p1.add(tfb=new JTextField(12));
JPanel p2=new JPanel();p2.setLayout(new FlowLayout());
p2.add(btshow=new JButton(“Show Color”));btshow.setPreferredSize(new Dimension(280,25));
JPanel p3=new JPanel();p3.setLayout(new FlowLayout());
p3.add(tadisplay=new JTextArea(5,25));
JPanel p=new JPanel();p.setLayout(new BorderLayout());
p.add(p1,”North”);
p.add(p2,”Center”);
p.add(p3,”South”);
getContentPane().add(p);
btshow.addActionListener(this);
pack();
setTitle(“Swing Colors”);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btshow) myShow();
}
public void myShow(){
int r,g,b;
if(tfr.getText().isEmpty()||tfg.getText().isEmpty()||tfb.getText().isEmpty()) {
r=g=b=0;
}
else{
r=Integer.parseInt(tfr.getText());
g=Integer.parseInt(tfg.getText());
b=Integer.parseInt(tfb.getText());
}
tadisplay.setBackground(new Color(r,g,b));
tadisplay.setText(“                   “+
“——-Color is display here!——–”);
}
public static void main(String []args){
new ShowSwingColor();
}
}
Share this article :

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. iShare2Khmer - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Premium Blogger Template