Swing: Two frames | |
| |
import java.awt.*; import java.awt.event.*; import javax.swing.*; class Students { private String FirstName; private String LastName; Students (){} Students (String FirstName, String LastName){ this.FirstName = FirstName; this.LastName = LastName; } void SetFirstName (String FirstName){ this.FirstName = FirstName; } void SetLastName (String LastName){ this.LastName = LastName; } String GetFirstName (){ return FirstName; } String GetLastName (){ return LastName; } int GetLastLength (){ return LastName.length(); } }//end of class Students public class RegisterStudents extends JFrame implements ActionListener {//public Students Student = new Students (); JButton NewStudent = new JButton ("Add a Student"); JButton DisplayStudent = new JButton ("Display a Student"); JButton Quit = new JButton ("Quit"); public RegisterStudents (){// public super("Register Students");//of JFrame Container Contains = getContentPane(); Contains.setLayout (new GridLayout(5,1,0,40)); setSize (400,500); setLocation (150,150); Contains.add (NewStudent); Contains.add (DisplayStudent); Contains.add (Quit); Contains.add (new JLabel("")); NewStudent.addActionListener(this); DisplayStudent.addActionListener(this); Quit.addActionListener(this); setResizable(false); setVisible (true); } public Insets getInsets (){ return new Insets(40,40,30,40);} public static void main (String [] arg){ new RegisterStudents();} public void actionPerformed (ActionEvent e){ Object Phys = e.getSource(); if (Phys == NewStudent){ setVisible (false); new Registration (Student, this, "Registartion"); setVisible (true); }else if (Phys == DisplayStudent){ setVisible (false); new Display (Student, this, "Display information"); setVisible (true); }else if (Phys == Quit){ System.exit(0); } } }//end of class Register Students class Registration extends JDialog implements ActionListener { Students Student; JTextField FirstName = new JTextField (); JTextField LastName = new JTextField (); JButton Success = new JButton ("Confirm"); Registration (Students Student, JFrame Table, String Info){ super(Table,Info,true); JLabel LabelFirstName = new JLabel ("First name:"); JLabel LabelLastName = new JLabel ("Last name:"); Container Cont = getContentPane(); Cont.setLayout(null); this.Student = Student; Cont.add(LabelFirstName ); Cont.add(FirstName); Cont.add(LabelLastName ); Cont.add(LastName); FirstName.addActionListener(this); LastName.addActionListener(this); LabelFirstName.setSize(100,30); LabelFirstName.setLocation(20,150); LabelFirstName.setBackground(Color.green); FirstName.setSize(100,30); FirstName.setLocation(180,150); LabelLastName.setSize(100,30); LabelLastName.setLocation(20,220); LabelLastName.setBackground(Color.green); LastName.setSize(100,30); LastName.setLocation(180,220); Success.setSize(150,30); Success.setLocation(150,420); add(Success); Success.addActionListener(this); setSize(420,530); setLocation(150,150); setVisible (true); } public void actionPerformed (ActionEvent e){ if(e.getSource() == Success){ Student.SetFirstName(FirstName.getText()); Student.SetLastName(LastName.getText()); dispose();} } }//end of class Registration class Display extends JDialog implements ActionListener { JButton Success = new JButton ("That's OK"); Display ( Students Student, JFrame Table, String Info){ super(Table,Info,true); Container Cont = getContentPane(); JTextField FirstName = new JTextField(); FirstName.setText(Student.GetFirstName()); FirstName.setEditable (false); JTextField LastName = new JTextField(); //LastName.setText(Student.GetLastLength()); LastName.setText(Student.GetLastName()); LastName.setEditable (false); JLabel LabelFirstName = new JLabel ("First name:"); JLabel LabelLastName = new JLabel ("Last name:"); Cont.setLayout (null); add (LabelFirstName); add (FirstName); add (LabelLastName); add (LastName); LabelFirstName.setSize(100,30); LabelFirstName.setLocation(20,150); LabelFirstName.setBackground(Color.green); FirstName.setSize(100,30); FirstName.setLocation(180,150); LabelLastName.setSize(100,30); LabelLastName.setLocation(20,220); LabelLastName.setBackground(Color.green); LastName.setSize(100,30); LastName.setLocation(180,220); Success.setSize(150,30); Success.setLocation(150,420); add(Success); Success.addActionListener(this); setSize(420,530); setLocation(150,150); setVisible (true); } public void actionPerformed (ActionEvent e){ if(e.getSource() == Success){ dispose();} } }//end of class Display /* //----------------------------- import javax.swing.*; import java.awt.*; public class GridLayout extends JFrame { public static void main(String[] args) { GridLayout fl = new GridLayout(); } public flowlayout() { JFrame jf1=new JFrame("FlowLayout 1"); jf1.setSize(250, 150); jf1.setLayout(new FlowLayout()); JButton b1 = new JButton("Button1"); JButton b2 = new JButton("Button2"); JButton b3 = new JButton("Button3"); JButton b4 = new JButton("Button4"); JButton b5 = new JButton("Button5"); jf1.add(b1); jf1.add(b2); jf1.add(b3); jf1.add(b4); jf1.add(b5); jf1.setVisible(true); } } */ © The Scientific Sentence. 2007 |