1. JAVA GUI
JRadioButton Example:
JRadioButton Example:
public class someClass
extends JFrame{
JTextField
tf;
Font
pf;
Font
bf;
Font
itf;
Font
bif;
JRadioButton
pb;
JRadioButton
bb;
JRadioButton
ib;
JRadioButton
bib;
ButtonGroup
group;
public someClass(){
super("The
Title");
setLayout(new FlowLayout());
tf = new JTextField("Bucky is
awesome and hot", 25);
add(tf);
pb = new JRadioButton("plain",true);
bb = new JRadioButton("bold", false);
ib = new JRadioButton("italic", false);
bib = new JRadioButton("bold and
italic", false);
add(pb);
add(bb);
add(ib);
add(bib);
group = new ButtonGroup();
group.add(pb);
group.add(bb);
group.add(ib);
group.add(bib);
pf = new Font("Serif", Font.PLAIN, 14);
bf = new Font("Serif", Font.BOLD, 14);
itf = new Font("Serif", Font.ITALIC, 14);
bif = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
tf.setFont(pf);
pb.addItemListener(new HandlerClass(pf));
bb.addItemListener(new HandlerClass(bf));
ib.addItemListener(new HandlerClass(itf));
bib.addItemListener(new HandlerClass(bif));
}
class HandlerClass implements ItemListener{
Font
font;
public
HandlerClass(Font f){
font = f;
}
@Override
public void
itemStateChanged(ItemEvent arg0) {
// TODO
Auto-generated method stub
tf.setFont(font);
}
}
}
mainClass:
public static void main(String[] args) {
// TODO
Auto-generated method stub
someClass
sc = new someClass();
sc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sc.setSize(300,
200);
sc.setVisible(true);
}
Result:
JList
Example:
public class someClass
extends JFrame{
JList
list;
static String[] colornames = {"black", "blue", "red", "white"};
static Color[] colors = {Color.BLACK, Color.BLUE, Color.RED, Color.WHITE};
public someClass(){
super("Title");
setLayout(new FlowLayout());
list = new JList(colornames);
list.setVisibleRowCount(4);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(new JScrollPane(list));
list.addListSelectionListener(new
ListSelectionListener() {
@Override
public void
valueChanged(ListSelectionEvent arg0) {
// TODO
Auto-generated method stub
getContentPane().setBackground(colors[list.getSelectedIndex()]);
}
});
}
}
Multiple Selection
List Example:
public class someClass
extends JFrame{
JList
leftlist;
JList
rightlist;
JButton
movebutton;
static String[] food = {"bacon", "wings", "ham", "beef", "orange","banana"};
public someClass(){
super("Title");
setLayout(new FlowLayout());
leftlist = new JList(food);
leftlist.setVisibleRowCount(4);
leftlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(leftlist));
movebutton = new JButton("Move--->");
movebutton.addActionListener(new
ActionListener() {
@Override
public void
actionPerformed(ActionEvent arg0) {
// TODO
Auto-generated method stub
rightlist.setListData(leftlist.getSelectedValues());
}
});
add(movebutton);
rightlist = new JList();
rightlist.setVisibleRowCount(3);
rightlist.setFixedCellWidth(100);
rightlist.setFixedCellHeight(15);
rightlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(rightlist));
}
}
Result:
Simple Adapter Class Example
MouseAdapter Class:
public class Gui extends JFrame {
private String details;
private JLabel statusbar;
public Gui(){
super("Title");
statusbar = new JLabel("This is
default");
add(statusbar, BorderLayout.SOUTH);
addMouseListener(new MouseClass());
}
private class MouseClass extends MouseAdapter{
public void
mouseClicked(MouseEvent ev){
details = String.format("You
clicked %d", ev.getClickCount());
if(ev.isMetaDown())
details +=" with
right mouse button";
else if(ev.isAltDown())
details +=" either
center mouse button";
else details +=" with
left mouse button";
statusbar.setText(details);
}
}
}
Main class:
public static void main(String[] args) {
// TODO
Auto-generated method stub
Gui
go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,
200);
go.setVisible(true);
}
Result:
FlowLayout:
public class someClass
extends JFrame{
JButton
a;
JButton
b;
JButton
c;
FlowLayout
layout;
Container
cont;
public someClass(){
super("Layout
tutorial");
layout = new FlowLayout();
cont =
getContentPane();
setLayout(layout);
a = new JButton("Left");
add(a);
a.addActionListener(new
ActionListener() {
@Override
public void
actionPerformed(ActionEvent arg0) {
// TODO
Auto-generated method stub
layout.setAlignment(FlowLayout.LEFT);
layout.layoutContainer(cont);
}
});
// center
b = new JButton("Center");
add(b);
b.addActionListener(new
ActionListener() {
@Override
public void
actionPerformed(ActionEvent arg0) {
// TODO
Auto-generated method stub
layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(cont);
}
});
// right
c = new JButton("Right");
add(c);
c.addActionListener(new
ActionListener() {
@Override
public void
actionPerformed(ActionEvent arg0) {
// TODO
Auto-generated method stub
layout.setAlignment(FlowLayout.RIGHT);
layout.layoutContainer(cont);
}
});
}
}
Main class:
public static void main(String[] args) {
// TODO
Auto-generated method stub
someClass
sc = new someClass();
sc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sc.setSize(300,
100);
sc.setVisible(true);
}
Drawing Graphics:
public class someClass
extends JPanel{
public void
paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.WHITE);
g.drawLine(10,
25, 200, 45);
g.setColor(Color.RED);
g.drawRect(10,
55, 100, 30);
g.setColor(Color.GREEN);
g.fillOval(10,
95, 100, 30);
g.setColor(Color.ORANGE);
g.fill3DRect(10,
160, 100, 50, true);
g.setColor(Color.BLACK);
g.drawString("This is
string", 15, 150);
}
}
Main class:
public static void main(String[] args) {
// TODO
Auto-generated method stub
JFrame
jf = new JFrame("Title");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
someClass
sc = new someClass();
sc.setBackground(Color.WHITE);
jf.add(sc);
jf.setSize(500,
300);
jf.setVisible(true);
}
Result:
No comments:
Post a Comment