AWT = Abstract Window Toolkit |
Home | Contents | KM | Articles | Members | Sponsors | About us |
ปรับปรุง : 2556-09-13 (ปรับคำอธิบายโค้ดใหม่) |
OOP :: intro ch1-12 :: keyword & sign :: method calling :: series #1 :: series #2 :: series #3 :: series #4 :: pro_pmy |
Introduction to frame
|
import java.awt.*; import java.awt.event.*; public class x implements ActionListener { public static void main(String[] args) { Frame s = new Frame("Screen"); s.setSize(150,150); s.setVisible(true); s.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); } // actionPerformed is abstract so it must be declared here public void actionPerformed(ActionEvent a) { } } | |
import java.awt.*; import java.awt.event.*; public class x implements ActionListener { static Button bclose = new Button("Exit"); public static void main(String[] args) { Frame s = new Frame("Screen"); s.setSize(150,150); s.setLayout(null); s.setVisible(true); s.setBackground(new Color(0, 255, 0)); bclose.setBounds(10,40,70,20); s.add(bclose); x myx = new x(); bclose.addActionListener(myx); } public void actionPerformed(ActionEvent a) { System.out.println(a.getSource() + "1"); // output : java.awt.Button[button0,10,40,70x20,label=Exit] System.out.println(bclose + "2"); if(a.getSource()==bclose) { System.exit(0); } } } | |
import java.awt.*; import java.awt.event.*; public class x implements ActionListener { static Button b1 = new Button("Exit"); Button b2 = new Button("Hello"); public static void main(String[] args) { Button b3 = new Button("World"); Frame s = new Frame("Screen"); s.setSize(150,150); s.setLayout(null); s.setVisible(true); s.setBackground(new Color(0, 255, 0)); x myx = new x(); b1.setBounds(10,40,70,20); s.add(b1); myx.b2.setBounds(10,70,70,20); s.add(myx.b2); b3.setBounds(10,100,70,20); s.add(b3); b1.addActionListener(myx); myx.b2.addActionListener(myx); b3.addActionListener(myx); } public void actionPerformed(ActionEvent a) { System.exit(0); } } | |
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class x implements ActionListener { JFrame s = new JFrame("Screen"); JButton b1 = new JButton(new ImageIcon("right.gif")); JButton b2 = new JButton(); Image im = Toolkit.getDefaultToolkit().getImage("right.png"); public static void main(String[] args) { new x().init(); } public void init( ) { s.setSize(200,300); // height, width s.setLayout(null); s.setVisible(true); s.setIconImage(im); s.setBackground(Color.yellow); b1.setBounds(4,30,70,20); // 30 is top, 4 is right s.add(b1); b1.addActionListener(this); b2.setBounds(14,60,100,100); s.add(b2); b2.addActionListener(this); s.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); } public void actionPerformed(ActionEvent a) { b1.setIcon(new ImageIcon("burin.jpg")); b2.setIcon(new ImageIcon("burin.jpg")); // .jpg .png .gif } } | ||
Right frame
|
import java.awt.*; import java.awt.event.*; public class x implements ActionListener { Frame s = new Frame("Screen"); Button bclose = new Button("Exit"); public static void main(String[] args) { new x().init(); } public void init( ) { s.setSize(150,150); s.setLayout(null); // ถ้าไม่มี setLayout และ setVisible จะกลายเป็นปุ่มเต็มจอ s.setVisible(true); s.setBackground(Color.yellow); bclose.setBounds(10,40,70,20); s.add(bclose); bclose.addActionListener(this); } public void actionPerformed(ActionEvent a) { if(a.getSource()==bclose) { System.exit(0); } } }// การ compile และ run ก็ใช้ javac และ java ตามปกติ // คำสั่ง System.exit(0) หมายถึง Stops Java Virtual Machine (JVM) | |
import java.awt.*; import java.awt.event.*; public class x implements ActionListener { Frame s = new Frame("Screen"); TextField txt = new TextField(20); public static void main(String[] args) { new x().burin(); } public void burin( ) { s.setSize(150,150); s.setLayout(null); s.setVisible(true); txt.setBounds(10,30,100,20); txt.setText("ข้อมูล"); s.add(txt); s.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); } public void actionPerformed(ActionEvent a) { } } | |
import java.awt.*; import java.awt.event.*; public class x implements ActionListener { Frame s = new Frame("Screen"); Button bclose = new Button("Exit"); Button badd = new Button("Add"); TextField txt = new TextField(10); public static void main(String[] args) { new x().init(); } public void init( ) { s.setSize(150,150); s.setLayout(null); s.setVisible(true); s.setBackground(Color.yellow); txt.setBounds(10,40,70,20); badd.setBounds(10,70,70,20); bclose.setBounds(10,100,70,20); s.add(txt); s.add(badd); s.add(bclose); badd.addActionListener(this); bclose.addActionListener(this); txt.setText("0"); } public void actionPerformed(ActionEvent a) { String aa= Double.toString(Double.parseDouble(txt.getText())+5); //String aa= Integer.toString(Integer.parseInt(txt.getText()) +5); if(a.getSource()==badd) { txt.setText(aa); } if(a.getSource()==bclose) { System.exit(0); } } } | |
import java.awt.*; import java.awt.event.*; public class x implements ActionListener { Frame s = new Frame("Screen"); Button b1 = new Button("Add"); Button b2 = new Button("Welcome"); TextField txt = new TextField(10); String tmp; public static void main(String[] args) { new x().init(); } public void init( ) { s.setSize(150,150); s.setLayout(null); s.setVisible(true); s.setBackground(Color.yellow); txt.setBounds(10,40,70,20); b1.setBounds(10,70,70,20); b2.setBounds(10,100,90,30); s.add(txt); s.add(b1); s.add(b2); b1.addActionListener(this); txt.setText("0"); s.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); } public void actionPerformed(ActionEvent a) { String aa= Integer.toString(Integer.parseInt(txt.getText()) + 1); if(a.getSource()==b1) { txt.setText(aa); b1.setLabel(aa); tmp = b1.getLabel(); b2.setLabel(tmp); } if(tmp.equals("2")) b2.setEnabled(false); } } | |
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class x implements ActionListener { Frame s = new Frame("Screen"); Button b = new Button("Add"); TextField txt = new TextField(10); JButton br = new JButton(new ImageIcon("right.gif")); JLabel jl = new JLabel("wow", new ImageIcon("right.gif"), JLabel.RIGHT); JCheckBox jc = new JCheckBox("checkbox"); JRadioButton jr = new JRadioButton("radio"); public static void main(String[] args) { new x().init(); } public void init( ) { s.setSize(200,300); // height, width s.setLayout(null); s.setVisible(true); s.setBackground(Color.yellow); txt.setBounds(10,40,180,20); // left,top,width,height b.setBounds(10,70,70,20); br.setBounds(10,100,70,20); jl.setBounds(10,130,70,20); jc.setBounds(10,160,70,20); jc.setSelected(true); jr.setBounds(10,190,70,20); jr.setSelected(true); s.add(txt); s.add(b); s.add(br); s.add(jl); s.add(jc); s.add(jr); b.addActionListener(this); txt.setText("0"); s.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); } public void actionPerformed(ActionEvent a) { if(a.getSource()==b) { txt.setText(b.getLabel() + jc.isSelected() + jr.isSelected()); } } } | |
New frame
|
import java.awt.*; import java.awt.event.*; public class x implements ActionListener { Frame s1 = new Frame("Screen1"); Frame s2 = new Frame("Screen2"); Button bclose = new Button("Exit"); Button bshow2 = new Button("show2"); public static void main(String[] args) { new x().init(); } public void init( ) { s1.setSize(150,150); s1.setLayout(null); s1.setBackground(Color.yellow); s2.setSize(150,150); s2.setLayout(null); s2.setBackground(Color.green); bshow2.setBounds(10,70,70,20); bclose.setBounds(10,100,70,20); s1.add(bshow2); s1.add(bclose); bshow2.addActionListener(this); bclose.addActionListener(this); s1.setVisible(true); } public void actionPerformed(ActionEvent a) { if(a.getSource()==bshow2) { s1.setVisible(false); s2.add(bclose); bclose.addActionListener(this); s2.setVisible(true); } if(a.getSource()==bclose) System.exit(0); } } | |
JTable
|
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class x implements ActionListener{ Frame f = new Frame("JTable"); public static void main(String args[]) { new x().init(); } void init() { f.setSize(700,400); f.setLocation(80,50); f.setLayout(null); f.setResizable(true); f.setBackground(Color.pink); showJtable(); // it should be before setVisible f.setVisible(true); } public void actionPerformed(ActionEvent a) { } public void showJtable() { String TableHeader[] = {"no","name"}; String TableArr[][]=new String[50][2]; // row=6,column=2 try { TableArr[0][0]=Integer.toString(1); // row=0 column=0 TableArr[1][0]=Integer.toString(2); // row=1 column=0 TableArr[0][1]="jack"; // row=0 column=1 TableArr[1][1]="jojo"; // row=1 column=1 JTable TitleTable; JScrollPane DataInTable; TitleTable = new JTable(TableArr,TableHeader); TitleTable.setVisible(true); TitleTable.setEnabled(false); DataInTable = new JScrollPane(TitleTable); DataInTable.setBounds(5,50,600,200); // left, top,width,height f.add(DataInTable); } catch (Exception e){ System.err.println(e); } } } | |
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class y implements ActionListener{ Frame f = new Frame("JTable"); JTable TitleTable; TextField txt = new TextField(10); public static void main(String args[]) { new y().init(); } void init() { f.setSize(700,400); f.setLocation(80,50); f.setLayout(null); f.setBackground(Color.pink); txt.setBounds(1,40,70,20); f.add(txt); showJtable(); // it should be before setVisible f.setVisible(true); } public void actionPerformed(ActionEvent a) { } public void showJtable() { String TableHeader[] = {"no","name"}; // not show in header String TableArr[][]=new String[3][2]; // row=6,column=2 try { TableArr[0][0]=Integer.toString(1); // row=0 column=0 TableArr[1][0]=Integer.toString(2); // row=1 column=0 TableArr[0][1]=ThaiUtil.ASCII2Unicode("ที่นี่"); // row=0 column=1 TableArr[1][1]=ThaiUtil.ASCII2Unicode("ดูนี่"); // row=1 column=1 TitleTable = new JTable(TableArr,TableHeader); TitleTable.setVisible(true); TitleTable.setEnabled(true); // true = can change TitleTable.setBounds(5,80,600,300); f.add(TitleTable); TitleTable.getSelectionModel().addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { System.out.println(TitleTable.getSelectedRow()); System.out.println(TitleTable.getSelectedColumn()); System.out.println(TitleTable.getModel().getValueAt(0, 1)); txt.setText(Integer.toString(TitleTable.getSelectedRow())); } } }); } catch (Exception e){ System.err.println(e); } } } | |
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; // @see http://stackoverflow.com/q/12301923/230513 public class z extends JPanel { private static final String SHOW = "Show"; private DefaultTableModel model = new DefaultTableModel(); private JTable table = new JTable(model); private JButton button = new JButton(new AbstractAction(SHOW) { public void actionPerformed(ActionEvent e) { System.out.println(table.getSelectedRow()); } }); public z() { model.addColumn("Column"); for (int i = 0; i < 16; i++) { model.addRow(new Object[]{i}); } table.setPreferredScrollableViewportSize(new Dimension(160, 100)); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.getSelectionModel().addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { button.setText(SHOW + " " + table.getSelectedRow()); } } }); this.add(new JScrollPane(table)); table.setRowSelectionInterval(3, 3); } private void display() { JFrame f = new JFrame("TableSelection"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this, BorderLayout.CENTER); f.add(button, BorderLayout.SOUTH); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { new z().display(); } }); } } | |
Panel
|
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class x extends JFrame { JFrame frame; JPanel p; JButton b1,b2,b3,b4,b5,b6; public static void main(String[] args) { new x();} public x() { frame = new JFrame("test"); p = new JPanel(); p.setLayout(new GridLayout(3,2)); // row, column b1 = new JButton("one"); b2 = new JButton("two"); b3 = new JButton("three"); b4 = new JButton("four"); b5 = new JButton("five"); b6 = new JButton("six"); GridBagConstraints gc = new GridBagConstraints(); gc.fill = GridBagConstraints.HORIZONTAL; p.add(b1, gc); p.add(b2, gc); p.add(b3, gc); p.add(b4, gc); p.add(b5, gc); p.add(b6, gc); frame.add(p, BorderLayout.NORTH); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 200); frame.setVisible(true); } } | |
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class x extends JFrame { JFrame frame; JPanel p1,p2,p3; JLabel l1,l2; JTextArea t1; JTextField t2; JButton b1; public static void main(String[] args) { new x();} public x() { frame = new JFrame("test"); p1 = new JPanel(); p1.setLayout(new GridLayout()); p2 = new JPanel(); p2.setLayout(new GridLayout()); p3 = new JPanel(); p3.setLayout(new GridLayout()); l1 = new JLabel("test of panel"); t1 = new JTextArea(10,10); t1.setText("ทดสอบ"); l2 = new JLabel("Message"); t2 = new JTextField(10); t2.setText("ทดลอง"); b1 = new JButton("Send"); GridBagConstraints gc = new GridBagConstraints(); gc.fill = GridBagConstraints.HORIZONTAL; p1.add(l1, gc); p2.add(l2, gc); p2.add(t1, gc); p3.add(t2, gc); p3.add(b1, gc); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ev){ t1.setText(t2.getText()); } }); frame.add(p1, BorderLayout.NORTH); frame.add(p2, BorderLayout.CENTER); frame.add(p3, BorderLayout.SOUTH); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setVisible(true); } } | |
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class x extends JFrame implements ActionListener { JFrame frame; JPanel p,p2; JButton b1,b2,b3,b4,b5,b6; JTextField t = new JTextField(10); String tmp = ""; public static void main(String[] args) { new x().init();} public void init() { frame = new JFrame("test"); p = new JPanel(); p2 = new JPanel(); p.setLayout(new GridLayout(3,2)); // row, column b1 = new JButton("1"); b2 = new JButton("2"); b3 = new JButton("3"); b4 = new JButton("4"); b5 = new JButton("+"); b6 = new JButton("="); GridBagConstraints gc = new GridBagConstraints(); gc.fill = GridBagConstraints.HORIZONTAL; p.add(b1, gc); p.add(b2, gc); p.add(b3, gc); p.add(b4, gc); p.add(b5, gc); p.add(b6, gc); t.setText(""); p2.add(t, gc); frame.add(p, BorderLayout.NORTH); frame.add(p2, BorderLayout.SOUTH); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 200); frame.setVisible(true); b1.addActionListener(this); b2.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); } public void actionPerformed(ActionEvent a) { JButton o = new JButton(""); o = (JButton)(a.getSource()); if(a.getSource()==b1) { t.setText(t.getText() + o.getText()); } if(a.getSource()==b2) { t.setText(t.getText() + o.getText()); } if(a.getSource()==b5) { tmp = t.getText(); t.setText(""); b3.setEnabled(false); b4.setEnabled(false); } if(a.getSource()==b6) { t.setText(Double.toString(Double.parseDouble(tmp) + Double.parseDouble(t.getText()))); } } } | ||
ThaiUtil
|
public class ThaiUtil { /* Creates a new instance of ThaiUtil http://www.narisa.com/forums/index.php?showtopic=2738 */ public static String Unicode2ASCII(String unicode) { StringBuffer ascii = new StringBuffer(unicode); int code; for(int i = 0; i < unicode.length(); i++) { code = (int)unicode.charAt(i); if ((0xE01<=code) && (code <= 0xE5B )) ascii.setCharAt( i, (char)(code - 0xD60)); } return ascii.toString(); } public static String ASCII2Unicode(String ascii) { StringBuffer unicode = new StringBuffer(ascii); int code; for(int i = 0; i < ascii.length(); i++) { code = (int)ascii.charAt(i); if ((0xA1 <= code) && (code <= 0xFB)) unicode.setCharAt( i, (char)(code + 0xD60)); } return unicode.toString(); } } | |
| |
import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TextareaList implements ActionListener{ Connection connection; Statement statement; TextArea text =new TextArea(); String sourceURL = "jdbc:odbc:student"; Frame f = new Frame("select"); Button bexit = new Button("Exit"); // 01- main public static void main(String args[]) { new TextareaList().init(); } // 02 - init void init() { f.setSize(700,400); f.setLocation(80,50); f.setLayout(null); f.setResizable(true); f.setBackground(Color.pink); f.add(bexit); f.add(text); text.setBounds(115,30,200,300); bexit.setBounds(10,30,100,30); bexit.addActionListener(this); showRegisRecord(); f.setVisible(true); } // 03 - rpt01 public TextareaList() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection = DriverManager.getConnection(sourceURL); statement = connection.createStatement(); } catch (SQLException sqle) { System.err.println("Error creating connection"); } catch (ClassNotFoundException cnfe) { System.err.println(cnfe.toString()); } } // 04 - actionPerformed public void actionPerformed(ActionEvent a) { if (a.getSource()== bexit) System.exit(0); } // 05 - showRegisRecord public void showRegisRecord() { try { statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet ShowAll = statement.executeQuery("SELECT * FROM reg_student"); String tmp; ShowAll.last(); int crow=ShowAll.getRow(); ShowAll.first(); if (crow>=1) while (!ShowAll.isAfterLast()) { tmp=ShowAll.getString(3); text.setText(tmp.toString()+ "\n" + text.getText() ); ShowAll.next(); } } catch (SQLException sqle) { System.err.println("\nSQLException:\n"); } } // end showRegisRecord } // end class | ตัวอย่างข้อมูลไม่ค่อยสวย เพราะไม่ได้แต่งก่อน print screen | |
import java.sql.*; import java.awt.*; import java.awt.event.*; public class myselect implements ActionListener{ Connection connection; Statement statement; TextField text_result =new TextField(); TextField text_key =new TextField(); String sourceURL = "jdbc:odbc:student"; Frame f = new Frame("select"); Button bsearch = new Button("search"); String k; // 01- main public static void main(String args[]) { new myselect().init(); } // 02 - init void init() { f.setSize(400,150); f.setLocation(80,50); f.setLayout(null); f.setVisible(true); f.add(text_key); text_key.setBounds(10,30,200,30); text_key.setText("civi 101"); f.add(text_result); text_result.setBounds(10,70,200,30); f.add(bsearch); bsearch.setBounds(10,100,100,30); bsearch.addActionListener(this); } // 03 - constructor public myselect() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection = DriverManager.getConnection(sourceURL); } catch (SQLException sqle) { System.err.println("Error creating connection"); } catch (ClassNotFoundException cnfe) { System.err.println(cnfe.toString()); } } // 04 - actionPerformed public void actionPerformed(ActionEvent a) { if (a.getSource()== bsearch) { try { statement = connection.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); // required for parameters k = text_key.getText(); String sql = "SELECT * FROM subject where subid = '" + k + "'"; ResultSet ShowAll = statement.executeQuery(sql); if (ShowAll.first()) { text_result.setText(ShowAll.getString(2)); } else { text_result.setText("not found"); } } catch (SQLException sqle) { System.err.println("Error creating connection"); } // end try ..catch } // end if } // end actionPerformed } // end class | ถ้า select ไม่พบ ให้ตรวจข้อมูล ที่จัดเก็บในตาราง แล้วนำมาค้น | |
import java.sql.*; import java.awt.*; import java.awt.event.*; public class myinsert implements ActionListener{ Connection connection; Statement statement; TextField text_key1 =new TextField(); TextField text_key2 =new TextField(); TextField text_result =new TextField(); String sourceURL = "jdbc:odbc:student"; Frame f = new Frame("insert"); Button binsert = new Button("insert"); String k1,k2; // 01- main public static void main(String args[]) { new myinsert().init(); } // 02 - init void init() { f.setSize(500,200); f.setLocation(80,50); f.setLayout(null); f.setVisible(true); f.add(text_key1); text_key1.setBounds(10,30,200,30); f.add(text_key2); text_key2.setBounds(10,70,200,30); f.add(text_result); text_result.setBounds(10,110,450,30); text_result.setEnabled(false); f.add(binsert); binsert.setBounds(10,150,100,30); binsert.addActionListener(this); } // 03 - constructor public myinsert() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection = DriverManager.getConnection(sourceURL); } catch (SQLException sqle) { System.err.println("Error creating connection"); } catch (ClassNotFoundException cnfe) { System.err.println(cnfe.toString()); } } // 04 - actionPerformed public void actionPerformed(ActionEvent a) { if (a.getSource()== binsert) { try { statement = connection.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); // required for parameters k1 = text_key1.getText(); k2 = text_key2.getText(); String sql = "insert into subject(subid,subname)"; sql += "values ('" + k1 + "','" + k2 + "')"; statement.executeUpdate(sql); text_key1.setText(""); text_key2.setText(""); text_result.setText(sql); } catch (SQLException sqle) { System.err.println("Error creating connection : " + sqle.toString()); } // end try ..catch } // end if } // end actionPerformed } // end class | ตามตัวอย่างนี้เชื่อมต่อผ่าน ODBC ไม่จำเป็นต้องปรับ permission | |
"Imagination is more important than knowledge" - Albert Einstein |