名前:上原和樹
番号:065709D
所属:情報工学科1年次
GUIaa |
---|
import java.awt.*; import java.awt.event.*; public class GUIaa extends Frame { Button b0 = new Button("Even/Odd?"); Label x0 = new Label("Type a number and press..."); TextField t0 = new TextField(); public GUIaa() { setLayout(null); add(t0); t0.setBounds(10, 40, 90, 30); add(b0); b0.setBounds(110, 40, 80, 30); add(x0); x0.setBounds(10, 80, 180, 30); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int i = (new Integer(t0.getText())).intValue(); t0.setText(""); if(i % 2 == 0) { x0.setText(i + " is Even"); } else { x0.setText(i + " is Odd"); } } }); } public static void main(String[] args) { Frame win = new GUIaa(); win.setSize(200, 150); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } } |
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "a" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.という警告文が表示された(Integer.java:620) at GUIaa$1.actionPerformed(GUIaa.java:16) at java.awt.Button.processActionEvent(Button.java:388) at java.awt.Button.processEvent(Button.java:356) at java.awt.Component.dispatchEventImpl(Component.java:4021) at java.awt.Component.dispatchEvent(Component.java:3869) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
GUI部品を生成するとき 部品の種類 部品名 = new 部品の種類(); として、生成する GUI部品をウィンドウに追加する場合 add(部品名); で追加する。 ウィンドウ内で部品の位置を指定するとき 部品名.setBounds (x座標、y座標、幅、高さ) として指定される。xとyについては左上の隅が(0,0)となる。
GUI(Graphical User Interface) ユーザに対する情報の表示にグラフィックを多用して、ほとんどの操作はマウス などで行うようにできるインターフェース。
b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ int i = (new Integer(t0.getText())).intValue(); t0.setText("input the number"); if(i % 2 == 0) { x0.setText("Even"); }else { throw new ArithmeticException(); } }catch (ArithmeticException ex){ x0.setText("Odd"); }catch(NumberFormatException ex) { x0.setText("Error"); } finally{ t0.setText(""); } } }); } |
Gui5_1.java |
---|
import java.awt.*; import java.awt.event.*; public class Gui5_1 extends Frame { int a; String m, h, time; TextField t0 = new TextField("imput the seconds"); Choice ch0 = new Choice(); Button b0 = new Button("change"); TextField t1 = new TextField(); Label x0 = new Label("Change time"); public Gui5_1() { setLayout(null); add(x0); x0.setBounds(10,90,80,30); add(t0); t0.setBounds(10,30,150,30); add(ch0); ch0.setBounds(200,30,150,30); ch0.add("minutes"); m = "minutes"; ch0.add("hours"); h = "hours"; ch0.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent evt){ time = ch0.getSelectedItem(); if (time == m) {a = 1;} else if (time == h) {a = 0;} } }); add(b0); b0.setBounds(160,90,80,20); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if (a == 1){ try { int j = (new Integer(t0.getText())).intValue(); int d = (j/60); t1.setText("" + d + "minutes"); }catch(Exception ex) { t1.setText("Error"); } } else if (a == 0) { try { int j = (new Integer(t0.getText())).intValue(); int d = (j/360); t1.setText("" + d + "hours"); }catch(Exception ex){ t1.setText("Error"); } } } }); add(t1); t1.setBounds(50,150,100,30); } public static void main(String args[]){ Frame win = new Gui5_1(); win.setSize(400,200); win.setVisible(true); win.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt){ System.exit(0); } }); } } |
Gui5_2.java |
---|
import java.awt.*; import java.awt.event.*; public class Gui5_2 extends Frame { Label x0 = new Label("CelsiusFahrenheit"); TextField t0 = new TextField("input the Celsius"); TextField t1 = new TextField("input the Fahrenheit"); Button b0 = new Button("Change"); Button b1 = new Button("Change"); Label x1 = new Label(); Label x2 = new Label(); public Gui5_2(){ setLayout(null); add(x0); x0.setBounds(10,30,150,30); add(x1); x1.setBounds(260,80,60,30); add(x2); x2.setBounds(260,130,60,30); add(t0); t0.setBounds(10,80,150,30); add(t1); t1.setBounds(10,130,150,30); add(b0); b0.setBounds(180,80,70,30); b0.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { try { int i = (new Integer(t0.getText())).intValue(); i = (int)Math.rint(1.8*i+32); x1.setText(i+"F"); }catch(Exception err) {x1.setText("Err.");} } }); add(b1); b1.setBounds(180,130,70,30); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { try { int j = (new Integer(t1.getText())).intValue(); j = (int)Math.rint((j-32)/1.8); x2.setText(j+"C"); }catch(Exception err) {x2.setText("Err.");} } }); } public static void main(String[] args){ Frame win = new Gui5_2(); win.setSize(400,200); win.setVisible(true); win.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt){ System.exit(0); } }); } } |
Gui5_3.java |
---|
import java.awt.*; import java.awt.event.*; public class Gui5_3 extends Frame { Label x0 = new Label("calculator"); Label x1 = new Label("?"); Label x2 = new Label(); TextField t0 = new TextField(); TextField t1 = new TextField(); Button b1 = new Button("+"); Button b2 = new Button("-"); Button b3 = new Button("*"); Button b4 = new Button("/"); Button b5 = new Button("%"); public Gui5_3(){ setLayout(null); add(x0); x0.setBounds(10,30,100,30); add(t0); t0.setBounds(10,80,100,30); add(x1); x1.setBounds(120,85,30,30); add(t1); t1.setBounds(160,80,100,30); add(x2); x2.setBounds(300,80,100,30); add(b1); b1.setBounds(10,130,30,30); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ try{ int i = (new Integer(t0.getText())).intValue(); int j = (new Integer(t1.getText())).intValue(); int k = i + j; x2.setText("= " + k); x1.setText("+"); }catch(NumberFormatException ex) {x2.setText("Err.");} } }); add(b2); b2.setBounds(50,130,30,30); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ try{ int i1 = (new Integer(t0.getText())).intValue(); int j1 = (new Integer(t1.getText())).intValue(); int k1 = i1 - j1; x2.setText("= " + k1); x1.setText("-"); }catch(NumberFormatException ex) {x2.setText("Err.");} } }); add(b3); b3.setBounds(90,130,30,30); b3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ try{ int i2 = (new Integer(t0.getText())).intValue(); int j2 = (new Integer(t1.getText())).intValue(); int k2 = i2 * j2; x2.setText("= " + k2); x1.setText("*"); }catch(NumberFormatException ex) {x2.setText("Err.");} } }); add(b4); b4.setBounds(130,130,30,30); b4.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ try{ int i3 = (new Integer(t0.getText())).intValue(); int j3 = (new Integer(t1.getText())).intValue(); int k3 = i3 / j3; x2.setText("= " + k3); x1.setText("/"); }catch(NumberFormatException ex) {x2.setText("Err");} } }); add(b5); b5.setBounds(170,130,30,30); b5.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ try{ int i4 = (new Integer(t0.getText())).intValue(); int j4 = (new Integer(t1.getText())).intValue(); int k4 = i4 % j4; x2.setText("= " + k4); x1.setText("%"); }catch(NumberFormatException ex) {x2.setText("Err.");} } }); } public static void main(String[] args){ Frame win = new Gui5_3(); win.setSize(400,200); win.setVisible(true); win.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt){ System.exit(0); } }); } } |