//:swing/SwingApplication.java package swing; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Simple Swing Application with two Buttons, two Labels, * two TextFields * Application has Actions * @author Shpatserman Maria */ public class SwingApplication implements ActionListener{ JTextField myTextField; JLabel myLabel; JTextField myTextField2; JLabel myLabel2; SwingApplication(){ JFrame myFrame = new JFrame("SwingApplication"); myFrame.setLayout(new GridLayout(0,2,5,5)); myFrame.setSize(350, 150); myLabel = new JLabel(""); //set name for cheking myLabel.setName("LeftLabel"); myFrame.add(myLabel); myLabel2 = new JLabel(""); //set name for cheking myLabel2.setName("RightLabel"); myFrame.add(myLabel2); myTextField = new JTextField(20); myFrame.add(myTextField); myTextField.setActionCommand("field1"); myTextField.addActionListener(this); myTextField.setName("LeftField"); myTextField2 = new JTextField(20); myTextField2.setName("RightField"); myFrame.add(myTextField2); myTextField2.setActionCommand("field2"); myTextField2.addActionListener(this); JButton myBut = new JButton("First"); myBut.addActionListener(this); myFrame.add(myBut); JButton myBut2 = new JButton("Second"); myBut2.addActionListener(this); myFrame.add(myBut2); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setVisible(true); } /** * Test main method ( Tests created application) * */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run(){ new SwingApplication(); } }); } /** * Method for starting SwingApplication */ public static void startApp(){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ new SwingApplication(); } }); } /** * Method performs actions of the Buttons, TextFields .. * @param e Event */ public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("First")){ myTextField.setText("First pressed"); } else if(e.getActionCommand().equals("field1")){ myLabel.setText(myTextField.getText()); } else if(e.getActionCommand().equals("Second")){ myTextField2.setText("Second pressed"); } else if(e.getActionCommand().equals("field2")){ myLabel2.setText(myTextField2.getText()); } } }