AWT = Abstract Window Toolkit |
Home | Contents | KM | Articles | Members | Sponsors | About us |
ปรับปรุง : 2556-07-27 (ปรับคำอธิบายโค้ดใหม่) |
OOP :: intro ch1-12 :: keyword & sign :: method calling :: series #1 :: series #2 :: series #3 :: 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"); // java.awt.Button[button0,10,40,70x20,label=Exit] System.out.println(bclose + "2"); if(a.getSource()==bclose) { System.exit(0); } } } | |
![]() | ![]() | |
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); } } } | |
![]() | ![]() | |
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(); } }); } } | |
![]() | ![]() | |
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 x 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 x().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 x() { 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 | |
"Imagination is more important than knowledge" - Albert Einstein |