GUI project - Weekly Practical exam. Practise question. PDF

Title GUI project - Weekly Practical exam. Practise question.
Author Krish Bista
Course Programming Fundamentals I
Institution University of Winnipeg
Pages 4
File Size 44.1 KB
File Type PDF
Total Downloads 50
Total Views 118

Summary

Weekly Practical exam. Practise question. ...


Description

/** *Text genereted by Simple GUI Extension for BlueJ */ import javax.swing.UIManager.LookAndFeelInfo; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.border.Border; import javax.swing.*; public class GUI_Project extends JFrame { private Students students; private JMenuBar menuBar; private JButton buttonFindStudent; private JLabel label1; private JLabel label2; private JLabel label3; private JTextField textfieldName; private JTextField textfieldId; //Constructor public GUI_Project(){ this.students = new Students(); this.setTitle("GUI_Project"); this.setSize(500,400); //menu generate method generateMenu(); this.setJMenuBar(menuBar); //pane with null layout JPanel contentPane = new JPanel(null); contentPane.setPreferredSize(new Dimension(500,400)); contentPane.setBackground(new Color(192,192,192)); buttonFindStudent = new JButton(); buttonFindStudent.setBounds(180,94,90,35); buttonFindStudent.setBackground(new Color(214,217,223)); buttonFindStudent.setForeground(new Color(0,0,0)); buttonFindStudent.setEnabled(true); buttonFindStudent.setFont(new Font("sansserif",0,12)); buttonFindStudent.setText("Find"); buttonFindStudent.setVisible(true); //Set action for button click //Call defined method buttonFindStudent.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) { findStudent(evt); } }); label1 = new JLabel(); label1.setBounds(104,12,300,20); label1.setBackground(new Color(214,217,223)); label1.setForeground(new Color(0,0,0)); label1.setEnabled(true); label1.setFont(new Font("SansSerif",0,24)); label1.setText("Students"); label1.setVisible(true); label2 = new JLabel(); label2.setBounds(12,93,90,35); label2.setBackground(new Color(214,217,223)); label2.setForeground(new Color(0,0,0)); label2.setEnabled(true); label2.setFont(new Font("sansserif",0,12)); label2.setText("Id"); label2.setVisible(true); label3 = new JLabel(); label3.setBounds(15,159,90,35); label3.setBackground(new Color(214,217,223)); label3.setForeground(new Color(0,0,0)); label3.setEnabled(true); label3.setFont(new Font("sansserif",0,12)); label3.setText("Name"); label3.setVisible(true); textfieldName = new JTextField(); textfieldName.setBounds(68,160,90,35); textfieldName.setBackground(new Color(255,255,255)); textfieldName.setForeground(new Color(0,0,0)); textfieldName.setEnabled(true); textfieldName.setFont(new Font("sansserif",0,12)); textfieldName.setText(""); textfieldName.setVisible(true); textfieldId = new JTextField(); textfieldId.setBounds(62,93,90,35); textfieldId.setBackground(new Color(255,255,255)); textfieldId.setForeground(new Color(0,0,0)); textfieldId.setEnabled(true); textfieldId.setFont(new Font("sansserif",0,12)); textfieldId.setText(""); textfieldId.setVisible(true); //adding components to contentPane panel contentPane.add(buttonFindStudent); contentPane.add(label1);

contentPane.add(label2); contentPane.add(label3); contentPane.add(textfieldName); contentPane.add(textfieldId); //adding panel to JFrame and seting of window position and close operation this.add(contentPane); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.pack(); this.setVisible(true); } //Method actionPerformed for buttonFindStudent private void findStudent (ActionEvent evt) { int id = Integer.parseInt(textfieldId.getText()); Student s = students.findStudent(id); if (s == null) textfieldName.setText("NOT FOUND"); else textfieldName.setText(s.getFullName()); } //method for generate menu public void generateMenu(){ menuBar = new JMenuBar(); JMenu file = new JMenu("File"); JMenu tools = new JMenu("Tools"); JMenu help = new JMenu("Help"); JMenuItem JMenuItem JMenuItem JMenuItem JMenuItem

open = new JMenuItem("Open "); save = new JMenuItem("Save "); exit = new JMenuItem("Exit "); preferences = new JMenuItem("Preferences about = new JMenuItem("About ");

file.add(open); file.add(save); file.addSeparator(); file.add(exit); tools.add(preferences); help.add(about); menuBar.add(file); menuBar.add(tools); menuBar.add(help); }

public static void main(String[] args){

");

System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new GUI_Project(); } }); } }...


Similar Free PDFs