JAVA Programming 1
GUI 2 2 1. GUI! GUI! GUI.! GUI! GUI 2. GUI!,,,!! GUI! GUI 11 : GUI 12 : GUI
3
4, JComponent
11-1 :, JComponent 5 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JComponentEx extends JFrame { public JComponentEx() { super("jcomponent "); Container c = getcontentpane(); c.setlayout(new FlowLayout()); JButton b1 = new JButton("Magenta/Yellow Button"); JButton b2 = new JButton(" Disabled Button "); JButton b3 = new JButton("getX(), gety()"); b1.setbackground(color.yellow); // b1.setforeground(color.magenta); // b1.setfont(new Font("Arial", Font.ITALIC, 20)); // Arial, 20 b2.setenabled(false); // b3.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { JButton b = (JButton)e.getSource(); JComponentEx frame = (JComponentEx)b.getTopLevelAncestor(); frame.settitle(b.getx() + "," + b.gety()); // ); c.add(b1); c.add(b2); c.add(b3); // setsize(260,200); setvisible(true); public static void main(string[] args) { new JComponentEx();
JLabel, 6 JLabel!
7 JLabel textlabel = new JLabel("");! ImageIcon! : png, gif, jpg sunset.jpg "images/sunset.jpg" ImageIcon image = new ImageIcon("images/sunset.jpg"); JLabel imagelabel = new JLabel(image); ImageIcon image = new ImageIcon("images/sunset.jpg"); JLabel label = new JLabel("", image, SwingConstants.CENTER);
11-2 : JLabel 8 import javax.swing.*; import java.awt.*; public class LabelEx extends JFrame { public LabelEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); JLabel textlabel = new JLabel("."); ImageIcon beauty = new ImageIcon("images/beauty.jpg"); JLabel imagelabel = new JLabel(beauty); ImageIcon normalicon = new ImageIcon("images/normalIcon.gif"); JLabel label = new JLabel(" ", normalicon, SwingConstants.CENTER); c.add(textlabel); c.add(imagelabel); c.add(label); setsize(400,600); setvisible(true); public static void main(string [] args) { new LabelEx();
JButton, 9!! Action! "hello" JButton btn = new JButton("hello");
10 3! 3! 3 1. : JButton seticon(icon image) 2. : JButton.setRolloverIcon(Icon); 3. : JButton.setPressedIcon(Icon)! new ImageIcon( ); ) new ImageIcon("images/normalIcon.gif);
11-3 : JButton 11 import javax.swing.*; import java.awt.*; public class ButtonEx extends JFrame { public ButtonEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); ImageIcon normalicon = new ImageIcon("images/normalIcon.gif"); ImageIcon rollovericon = new ImageIcon("images/rolloverIcon.gif"); ImageIcon pressedicon = new ImageIcon("images/pressedIcon.gif"); JButton btn = new JButton("call~~", normalicon); btn.setpressedicon(pressedicon); btn.setrollovericon(rollovericon); (normalicon.gif) (rollovericon.gif) c.add(btn); setsize(250,150); setvisible(true); public static void main(string [] args) { new ButtonEx(); (pressedicon.gif)
(Alignment) 12 : :
JCheckBox, 13 JCheckBox! (selected) (deselected)
14! "" JCheckBox c = new JCheckBox("");! "" JCheckBox c = new JCheckBox("", true);!!! cherry.jpg "" ImageIcon cherryicon = new ImageIcon("images/cherry.jpg"); ImageIcon selectedcherryicon = new ImageIcon("images/ selectedcherry.jpg"); JCheckBox cherry = new JCheckBox("", cherryicon); cherry.setselectedicon(selectedcherryicon); //
11-4 : 15 import javax.swing.*; import java.awt.*; public class CheckBoxEx extends JFrame { public CheckBoxEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); ImageIcon cherryicon = new ImageIcon("images/cherry.jpg"); ImageIcon selectedcherryicon = new ImageIcon( "images/selectedcherry.jpg"); JCheckBox apple = new JCheckBox(""); JCheckBox pear = new JCheckBox("", true); JCheckBox cherry = new JCheckBox("", cherryicon); cherry.setborderpainted(true); cherry.setselectedicon(selectedcherryicon); cherry.jpg( ) c.add(apple); c.add(pear); c.add(cherry); setsize(250,150); setvisible(true); public static void main(string [] args) { new CheckBoxEx(); selectedcherry.jpg( )
JCheckBox Item 16 Item! JCheckBox c = new JCheckBox(""); c.setselected(true); // ItemListener ItemEvent
11-5 : ItemEvent import javax.swing.*; import java.awt.event.*; import java.awt.*; public class CheckBoxItemEventEx extends JFrame { private JCheckBox [] fruits = new JCheckBox [3]; private String [] names = {"", "", ""; private JLabel sumlabel; public CheckBoxItemEventEx() { settitle(" ItemEvent "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); c.add(new JLabel(" 100, 500, 20000")); MyItemListener listener = new MyItemListener(); for(int i=0; i<fruits.length; i++) { fruits[i] = new JCheckBox(names[i]); fruits[i].setborderpainted(true); c.add(fruits[i]); fruits[i].additemlistener(listener); sumlabel = new JLabel(" 0."); c.add(sumlabel); setsize(250,200); setvisible(true); class MyItemListener implements ItemListener { private int sum = 0; // public void itemstatechanged(itemevent e) { if(e.getstatechange() == ItemEvent.SELECTED) { if(e.getitem() == fruits[0]) sum += 100; else if(e.getitem() == fruits[1]) sum += 500; else sum += 20000; else { if(e.getitem() == fruits[0]) sum -= 100; else if(e.getitem() == fruits[1]) sum -= 500; else sum -= 20000; sumlabel.settext(" "+ sum + "."); public static void main(string [] args) { new CheckBoxItemEventEx(); 17
JRadioButton, 18 JRadioButton!?,! /!
19 1. 2. 3. 4. ButtonGroup group = new ButtonGroup(); JRadioButton apple= new JRadioButton(""); JRadioButton pear= new JRadioButton(""); JRadioButton cherry= new JRadioButton(""); group.add(apple); group.add(pear); group.add(cherry); container.add(apple); container.add(pear); container.add(cherry);
11-6 : ( ) g import javax.swing.*; import java.awt.*; public class RadioButtonEx extends JFrame { public RadioButtonEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); ImageIcon cherryicon = new ImageIcon("images/cherry.j pg" ); ImageIcon selectedcherryicon = new ImageIcon("images/selectedCherry.jpg"); ButtonGroup g = new ButtonGroup(); JRadioButton apple = new JRadioButton(""); JRadioButton pear = new JRadioButton("", true); JRadioButton cherry = new JRadioButton("", cherryicon); cherry.setborderpainted(true); cherry.setselectedicon(selectedcherryicon); g.add(apple); g.add(pear); g.add(cherry); 20 c.add(apple); c.add(pear); c.add(cherry); setsize(250,150); setvisible(true); public static void main(string [] args) { new RadioButtonEx();
11-7 : JRadioButton Item import javax.swing.*; import java.awt.event.*; import java.awt.*; public class RadioButtonItemEventEx extends JFrame { private JRadioButton [] radio = new JRadioButton [3]; private String [] text = {"", "", ""; private ImageIcon [] image = { new ImageIcon("images/apple.jpg"), new ImageIcon("images/pear.jpg"), new ImageIcon("images/cherry.jpg"); private JLabel imagelabel = new JLabel(); public RadioButtonItemEventEx() { settitle(" Item Event "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new BorderLayout()); JPanel radiopanel = new JPanel(); radiopanel.setbackground(color.gray); ButtonGroup g = new ButtonGroup(); class MyItemListener implements ItemListener { public void itemstatechanged(itemevent e) { if(e.getstatechange() == ItemEvent.DESELECTED) return; if(radio[0].isselected()) imagelabel.seticon(image[0]); else if(radio[1].isselected()) imagelabel.seticon(image[1]); else imagelabel.seticon(image[2]); public static void main(string [] args) { new RadioButtonItemEventEx(); 21 for(int i=0; i<radio.length; i++) { radio[i] = new JRadioButton(text[i]); g.add(radio[i]); radiopanel.add(radio[i]); radio[i].additemlistener(new MyItemListener()); radio[2].setselected(true); c.add(radiopanel, BorderLayout.NORTH); c.add(imagelabel, BorderLayout.CENTER); imagelabel.sethorizontalalignment(swingconstants.center); setsize(250,200); setvisible(true); setselected(true) Item
11-7 22 "" ""
JTextField, 23 JTextField!? () <Enter> Action
11-8 : 24 import javax.swing.*; import java.awt.*; public class TextFieldEx extends JFrame { public TextFieldEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); c.add(new JLabel(" ")); c.add(new JTextField(20)); c.add(new JLabel(" ")); c.add(new JTextField(" ", 20)); c.add(new JLabel(" ")); c.add(new JTextField("...", 20)); setsize(300,150); setvisible(true); public static void main(string [] args) { new TextFieldEx();
JTextField 25! JTextField.setEditable(false);! JTextField.setText("hello");! JTextField.setFont(new Font("", Font.ITALIC, 20);
TextArea, 26 JTextArea!? JScrollPane
27 7 20 new JTextArea( hello, 7, 20); new JScrollPane(new JTextArea( hello, 7, 20));
11-9 : JTextArea import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TextAreaEx extends JFrame { private JTextField tf = new JTextField(20); private JTextArea ta = new JTextArea(7, 20); public TextAreaEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); c.add(new JLabel(" <Enter> ")); c.add(tf); c.add(new JScrollPane(ta)); 28 <Enter> Action <Enter> tf.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { JTextField t = (JTextField)e.getSource(); ta.append(t.gettext() + "\n"); t.settext(""); ); setsize(300,300); setvisible(true); public static void main(string [] args) { new TextAreaEx(); <Enter> tf ta
JList<E>, 29 JList<E>!? JComboBox<E> JList<String> JScrollPane Jlist<E>! JList<E> JDK7 <E>
1. String [] fruits= {"apple", "banana", "kiwi", "mango", "pear", "peach", "berry", "strawberry", "blackberry"; JList<String> strlist = new JList<String>(fruits); 2. Vector Vector v = new Vector(); v.add("apple"); v.add("banana"); v.add("kiwi"); JList<String> vlist = new JList<String>(v); 3. JList setlistdata() ImageIcon [] images = {new ImageIcon("images/icon1.png"), new ImageIcon("images/icon2.png"), new ImageIcon("images/icon3.png"), new ImageIcon("images/icon4.png") ; JList<ImageIcon> imagelist = new JList<ImageIcon>(); imagelist.setlistdata(images); 4. 30 JList<String> scrolllist = new JList<String>(fruits); new JScrollPane(scrollList);
11-10 : import javax.swing.*; import java.awt.*; public class ListEx extends JFrame { private String [] fruits= {"apple", "banana", "kiwi", "mango", "pear", "peach", "berry", "strawberry", "blackberry"; private ImageIcon [] images = { new ImageIcon("images/icon1.png"), new ImageIcon("images/icon2.png"), new ImageIcon("images/icon3.png"), new ImageIcon("images/icon4.png") ; public ListEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); JList<String> strlist = new JList<String>(fruits); c.add(strlist); JList<ImageIcon> imagelist = new JList<ImageIcon>(); imagelist.setlistdata(images); c.add(imagelist); JList<String> scrolllist = new JList<String>(fruits); c.add(new JScrollPane(scrollList)); 31 setsize(300,300); setvisible(true); public static void main(string [] args) { new ListEx();
32 JList<E>! JList<E>(Vector listdata) JList<E>(Object [] listdata)! JList<E> setlistdata() Vector<String> v = new Vector<String>(); v.add(""); v.add(""); JList<String> namelist = new JList<String>(v); // v, v v.add(""); namelist.setlistdata(v);
11-11 : 33 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class ListChangeEx extends JFrame { private JTextField tf = new JTextField(10); private Vector<String> v = new Vector<String>(); private JList<String> namelist = new JList<String>(v); public ListChangeEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); c.add(new JLabel(" <Enter> ")); c.add(tf); v.add(""); v.add(""); namelist.setvisiblerowcount(5); namelist.setfixedcellwidth(100); c.add(new JScrollPane(nameList)); setsize(300,300); setvisible(true); // JTextField ActionLister. <Enter> tf.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { JTextField t = (JTextField)e.getSource(); v.add(t.gettext()); t.settext(""); namelist.setlistdata(v); ); public static void main(string [] args) { new ListChangeEx(); <Enter>,
JComboBox<E>, 34 JComboBox<E>!?, JComboBox<String>
11-12 : 35 import javax.swing.*; import java.awt.*; public class ComboBoxEx extends JFrame { private String [] fruits = {"apple", "banana", "kiwi", "mango", "pear", "peach", "berry", "strawberry", "blackberry"; private String [] names = {"kitae", "jaemoon", "hyosoo", "namyun"; public ComboBoxEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); JComboBox<String> strcombo = new JComboBox<String>(fruits); c.add(strcombo); additem() JComboBox<String> namecombo = new JComboBox<String>(); for(int i=0; i<names.length; i++) namecombo.additem(names[i]); c.add(namecombo); setsize(300,300); setvisible(true); public static void main(string [] args) { new ComboBoxEx(); additem()
JComboBox<E> Action 36 Action! JComboBox<E>
11-13 : Action import javax.swing.*; import java.awt.event.*; import java.awt.*; public class ComboActionEx extends JFrame { private String [] fruits = {"apple", "banana", "kiwi", "mango"; private ImageIcon [] images = { new ImageIcon("images/apple.jpg"), new ImageIcon("images/banana.jpg"), new ImageIcon("images/kiwi.jpg"), new ImageIcon("images/mango.jpg"); private JLabel imglabel = new JLabel(images[0]); private JComboBox<String> strcombo = new JComboBox<String>(fruits); 37 public ComboActionEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); c.add(strcombo); c.add(imglabel); strcombo.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { JComboBox<String> cb = (JComboBox<String>)e.getSource(); int index = cb.getselectedindex(); imglabel.seticon(images[index]); ); setsize(300,250); setvisible(true); public static void main(string [] args) { new ComboActionEx();
JSlider, 38 JSlider!?
39 track /! void setorientation(int orientation)! void setpainttrack(boolean b) orientation : JSlider.HORIZONTAL, JSlider.VERTICAL! void setmaximum(int max)! void setminimum(int min) label /! void setpaintlabels(boolean b) b true track! void setmajortickspacing(int space)! void setminortickspacing(int space) b true label tick /! void setvaule(int n)! void setpaintticks(boolean b) b true n.
11-14 : JSlider 40 import javax.swing.*; import java.awt.*; 0~200 public class SliderEx extends JFrame { public SliderEx() { settitle(" "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 200, 100); slider.setpaintlabels(true); slider.setpaintticks(true); slider.setpainttrack(true); slider.setmajortickspacing(50); slider.setminortickspacing(10); c.add(slider); setsize(300,100); setvisible(true); public static void main(string [] args) { new SliderEx();
JSlider Change 41 Change! JSlider (value) JSlider setvalue(int n) (value ) ChangeListener
11-15 : JSlider Change import javax.swing.*; import java.awt.*; import javax.swing.event.*; public class SliderChangeEx extends JFrame { private JLabel colorlabel; private JSlider [] sl = new JSlider [3]; public SliderChangeEx() { settitle(" ChangeEvent "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); colorlabel = new JLabel(" SLIDER EXAMPLE "); for(int i=0; i<sl.length; i++) { sl[i] = new JSlider(JSlider.HORIZONTAL, 0, 255, 128); sl[i].setpaintlabels(true); sl[i].setpaintticks(true); sl[i].setpainttrack(true); sl[i].setmajortickspacing(50); sl[i].setminortickspacing(10); sl[i].addchangelistener(new MyChangeListener()); c.add(sl[i]); sl[0].setforeground(color.red); sl[1].setforeground(color.green); sl[2].setforeground(color.blue); int r = sl[0].getvalue(); int g = sl[1].getvalue(); int b = sl[2].getvalue(); class MyChangeListener implements ChangeListener { public void statechanged(changeevent e) { int r = sl[0].getvalue(); int g = sl[1].getvalue(); int b = sl[2].getvalue(); colorlabel.setbackground(new Color(r,g,b)); public static void main(string [] args) { new SliderChangeEx(); 42 colorlabel.setopaque(true); colorlabel.setbackground(new Color(r,g,b)); c.add(colorlabel); setsize(300,230); setvisible(true);
43