2012年6月1日 星期五

Java Basic(69)-Drop Down List Program


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Gui extends JFrame{
private JComboBox box;
private JLabel picture;
private static String[] filename = {"b.png", "g.png"};
private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])),new ImageIcon(getClass().getResource(filename[1]))};
public Gui(){
super("the title");
setLayout(new FlowLayout());
box = new JComboBox(filename);
box.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
if(event.getStateChange()==ItemEvent.SELECTED)
picture.setIcon(pics[box.getSelectedIndex()]);
}
}
);
add(box);
picture=new JLabel(pics[0]);
add(picture);
}
}



Java Basic(68)-JcomboBox


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Gui extends JFrame{
private JComboBox box;
private JLabel picture;
private static String[] filename = {"b.png", "g.bng"}; →你的照片陣列
private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])),new ImageIcon(getClass().getResource(filename[1]))};

public Gui(){
super("the title");
setLayout(new FlowLayout());
box = new JComboBox(filename);
}
}

Java Basic(67)-JRadioButton Final Program


Continue...
//等待事件要發生
pb.addItemListener(new HandlerClass(pf));
bb.addItemListener(new HandlerClass(bf));
ib.addItemListener(new HandlerClass(itf));
bib.addItemListener(new HandlerClass(bif));
}
private class HandlerClass implements ItemListener{
private Font font;
public HandlerClass(Font f){
font = f;
}
public void itemStateChanged(ItemEvent event){
tf.setFont(font);
}
}
}


Java Basic(66)-JradioButton


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class Gui extends JFrame{
private JTextField tf;
private Font pf;
private Font bf;
private Font itf;
private Font bif;
private JRadioButton pb;
private JRadioButton bb;
private JRadioButton ib;
private JRadioButton bib;
private ButtonGroup group;
public Gui(){
super("the title");
setLayout(new FlowLayout());
tf = new JTextField("Bucky is awesome", 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);
}
}



Java Basic(65)-The Final Check Box Program


Continue Java Basic(64)



private class HandlerClass implements ItemListener{
public void itemStateChanged(ItemEvent event){
Font font = null;
if(boldbox.isSelected() && italicbox.isSelected())
font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
else if(boldbox.isSelected())
font = new Font("Serif", Font.BOLD, 14);
else if(italicbox.isSelected())
font = new Font("Serif", Font.ITALIC, 14);
else
font = new Font("Serif", Font.PLAIN, 14);
tf.setFont(font);
}
}

The apples.java is same:
import javax.swing.JFrame;

class apples{
public static void main(String[] args){
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400, 200);
go.setVisible(true);
}
}


然後運行,你可以checkbox,並改變字體為粗體和斜體