0% found this document useful (0 votes)
49 views

Fişă de Laborator: Java - Principalele Componente Vizuale

The document provides 8 Java programming exercises that demonstrate the use of common visual components in Java GUI applications, including JButton, JLabel, JTextField, JComboBox, JCheckBox, JRadioButton, JMenuBar, JMenu, and JMenuItem. Each exercise includes sample code to create a simple window or form using one or more of these components. The exercises can be run and analyzed to understand how the different components work and how they can be used to build graphical user interfaces in Java. The final exercise asks students to review the previous examples and take notes on how each of the main visual components can be implemented.

Uploaded by

Victoria Oprea
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Fişă de Laborator: Java - Principalele Componente Vizuale

The document provides 8 Java programming exercises that demonstrate the use of common visual components in Java GUI applications, including JButton, JLabel, JTextField, JComboBox, JCheckBox, JRadioButton, JMenuBar, JMenu, and JMenuItem. Each exercise includes sample code to create a simple window or form using one or more of these components. The exercises can be run and analyzed to understand how the different components work and how they can be used to build graphical user interfaces in Java. The final exercise asks students to review the previous examples and take notes on how each of the main visual components can be implemented.

Uploaded by

Victoria Oprea
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Fi de laborator

Java Principalele componente vizuale


Ex 1: Scriei programul Java de mai jos apoi rulai-l i analizai rezultatele afiate.
import java.awt.*;
import javax.swing.*;
class Fereastra extends JFrame
{
Fereastra(String titlu)
{
super(titlu);
setBounds(100,100,300,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//aici vin trecute restul componentelor vizuale.....

setVisible(true);
}//Fereastra
}//class Fereastra
public class testFereastra
{
public static void main(String arg[])
{
Fereastra f = new Fereastra("Prima fereasta Java");
}///main
}//class testFereastra

Ex 2: Scriei programul Java de mai jos apoi rulai-l i analizai rezultatele afiate.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SchimbCulori extends JFrame implements ActionListener
{
Container x;
SchimbCulori(String titlu)
{
super(titlu);
setBounds(100,100,300,180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = getContentPane();
x.setLayout(null);
JButton b1 = new JButton("Rosu");
b1.setBounds(10,10,100,25);
b1.addActionListener(this);
x.add(b1);

JButton b2 = new JButton("Verde");


b2.setBounds(10,45,100,25);
b2.addActionListener(this);
x.add(b2);
JButton b3 = new JButton("Albastru");
b3.setBounds(10,80,100,25);
b3.addActionListener(this);
x.add(b3);
JButton b4 = new JButton("Gri");
b4.setBounds(10,115,100,25);
b4.addActionListener(this);
x.add(b4);
setVisible(true);
}//SchimbCulori
public void actionPerformed(ActionEvent ev)
{
String t = ev.getActionCommand();
if(t.compareTo("Rosu")==0)
x.setBackground(Color.RED);
if(t.compareTo("Verde")==0)
x.setBackground(Color.GREEN);
if(t.compareTo("Albastru")==0)
x.setBackground(Color.BLUE);
if(t.compareTo("Gri")==0)
x.setBackground(new Color(236,236,236));
}//actionPerformed
}//class SchimbCulori
public class testSchimbCulori
{
public static void main(String arg[])
{
SchimbCulori f = new SchimbCulori("Schimba culoarea de fundal..");
}///main
}//class testSchimbCulori

Ex 3: Scriei programul Java de mai jos apoi rulai-l i analizai rezultatele afiate.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GhicesteNr extends JFrame implements ActionListener
{
int Nr;
JTextField tf;
GhicesteNr(String titlu,int nr)
{
super(titlu);
Nr=nr;
setBounds(100,100,300,160);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container x = getContentPane();
x.setLayout(null);
JLabel l1 = new JLabel("Am ales un numar in intervalul [1,100]...");
l1.setBounds(13,10,250,15);
x.add(l1);
JLabel l2 = new JLabel("Poti spune care este numarul ales de mine?");
l2.setBounds(10,30,250,15);
x.add(l2);
JLabel l3 = new JLabel("Numarul este: ");
l3.setBounds(10,60,100,15);
x.add(l3);
tf = new JTextField();
tf.setBounds(100,60,75,15);
x.add(tf);
JButton b1 = new JButton("Verifica");
b1.setBounds(100,95,100,25);
b1.addActionListener(this);
x.add(b1);
setVisible(true);
}//GhicesteNr
public void actionPerformed(ActionEvent ev)
{
try{
int n = Integer.parseInt(tf.getText());
if(n==Nr)
JOptionPane.showMessageDialog(this,
"
FELICITARI!!!\nAi ghicit numarul meu.");
else if(n<Nr)
JOptionPane.showMessageDialog(this,
"Numarul introdus este prea mic");
else JOptionPane.showMessageDialog(this,
"Numarul introdus este prea mare");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,
"Trebuie sa introduci un numar in intervalul [1,100]");
}
}//actionPerformed
}//class GhicesteNr
public class testGhicesteNr
{
public static void main(String arg[])
{
GhicesteNr f = new GhicesteNr("Camp text...",
(int)(1+Math.random()*100));
}///main
}//class testGhicesteNr

Ex 4: Scriei programul Java de mai jos apoi rulai-l i analizai rezultatele afiate.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CasetaCombo extends JFrame implements ActionListener
{
JComboBox lista;
CasetaCombo(String titlu)
{
super(titlu);
setBounds(100,100,300,135);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container x = getContentPane();
x.setLayout(null);
JLabel et = new JLabel("Alege materia preferata");
et.setBounds(20,10,160,20);
x.add(et);
lista = new JComboBox();
lista.addItem("Matematica");
lista.addItem("Fizica");
lista.addItem("Informatica");
lista.addItem("Alta materie");
lista.setBounds(170,10,100,20);
x.add(lista);
JButton b1 = new JButton("Arata alegerea");
b1.setBounds(75,60,150,25);
b1.addActionListener(this);
x.add(b1);
setVisible(true);
}//CasetaCombo
public void actionPerformed(ActionEvent ev)
{
String t = (String)lista.getSelectedItem();
JOptionPane.showMessageDialog(this,"Ai ales "+t);
}//actionPerformed
}//class CasetaCombo
public class testCasetaCombo
{
public static void main(String arg[])
{
CasetaCombo f = new CasetaCombo("caseta combo...");
}///main
}//class testCasetaCombo

Ex 5: Scriei programul Java de mai jos apoi rulai-l i analizai rezultatele afiate.
4

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CaseteValidare extends JFrame implements ActionListener
{
JCheckBox cb1,cb2,cb3,cb4;
CaseteValidare(String titlu)
{
super(titlu);
setBounds(100,100,300,180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container x = getContentPane();
x.setLayout(null);
JLabel l1 = new JLabel("Selecteaza materia preferata...");
l1.setBounds(10,10,200,15);
x.add(l1);
cb1 = new JCheckBox("Matematica");
cb1.setBounds(20,30,100,15);
x.add(cb1);
cb2 = new JCheckBox("Informatica");
cb2.setBounds(20,50,100,15);
x.add(cb2);
cb3 = new JCheckBox("Engleza");
cb3.setBounds(20,70,100,15);
x.add(cb3);
cb4 = new JCheckBox("Biologie");
cb4.setBounds(20,90,100,15);
x.add(cb4);
JButton b1 = new JButton("Ce-am selectat?");
b1.setBounds(75,120,150,25);
b1.addActionListener(this);
x.add(b1);
setVisible(true);
}//CaseteValidare
public void actionPerformed(ActionEvent ev)
{
String s="";
if(cb1.isSelected()) s=s+"Matematica
";
if(cb2.isSelected()) s=s+"Informatica
";
if(cb3.isSelected()) s=s+"Engleza
";
if(cb4.isSelected()) s=s+"Biologie
";
JOptionPane.showMessageDialog(this,"Ai selectat materiile:\n"+s);
}//actionPerformed
}//class CaseteValidare
public class testCaseteValidare
{

public static void main(String arg[])


{
CaseteValidare f = new CaseteValidare("Caseta de validare...");
}///main
}//class testCaseteValidare

Ex 6: Scriei programul Java de mai jos apoi rulai-l i analizai rezultatele afiate.
import
import
import
import

java.awt.*;
java.awt.event.*;
javax.swing.*;
java.util.*;

class ButonRadio extends JFrame implements ActionListener


{
ButtonGroup bg;
ButonRadio(String titlu)
{
super(titlu);
setBounds(100,100,300,180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container x = getContentPane();
x.setLayout(null);
JLabel l1 = new JLabel("Selecteaza materia preferata...");
l1.setBounds(10,10,200,15);
x.add(l1);
JRadioButton rb1 = new JRadioButton("Matematica");
rb1.setBounds(20,30,100,15);
x.add(rb1);
JRadioButton rb2 = new JRadioButton("Informatica",true);
rb2.setBounds(20,50,100,15);
x.add(rb2);
JRadioButton rb3 = new JRadioButton("Engleza");
rb3.setBounds(20,70,100,15);
x.add(rb3);
JRadioButton rb4 = new JRadioButton("Biologie");
rb4.setBounds(20,90,100,15);
x.add(rb4);
JButton b1 = new JButton("Ce-am selectat?");
b1.setBounds(75,120,150,25);
b1.addActionListener(this);
x.add(b1);
bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
bg.add(rb4);
setVisible(true);

}//ButonRadio
public void actionPerformed(ActionEvent ev)
{
String s="";
Enumeration en = bg.getElements();
while(en.hasMoreElements())
{
JRadioButton rb = (JRadioButton)en.nextElement();
if(rb.isSelected() ) s = s + rb.getText()+"
";
}
JOptionPane.showMessageDialog(this,"Ai selectat materiile:\n"+s);
}//actionPerformed
}//class ButonRadio
public class testButonRadio
{
public static void main(String arg[])
{
ButonRadio f = new ButonRadio("Butoane radio...");
}///main
}//class testButonRadio

Ex 7: Scriei programul Java de mai jos apoi rulai-l i analizai rezultatele afiate.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Meniu extends JFrame implements ActionListener
{
JLabel et;
Meniu(String titlu)
{
super(titlu);
setBounds(100,100,300,180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container x = getContentPane();
x.setLayout(null);
JMenuBar mb = new JMenuBar();
JMenu men1 = new JMenu("Masini");
JMenu men11 = new JMenu("Dacia");
JMenu men2 = new JMenu("Flori");
JMenuItem mi11 = new JMenuItem("Trabant");
mi11.addActionListener(this);
JMenuItem mi12 = new JMenuItem("BMW - BX3");
mi12.addActionListener(this);
JMenuItem mi13 = new JMenuItem("Logan");
mi13.addActionListener(this);
JMenuItem mi14 = new JMenuItem("Solenza");
mi14.addActionListener(this);
JMenuItem mi15 = new JMenuItem("VW Passat");
mi15.addActionListener(this);

JMenuItem mi21 = new JMenuItem("Lalele");


mi21.addActionListener(this);
JMenuItem mi22 = new JMenuItem("Garoafe");
mi22.addActionListener(this);
JMenuItem mi23 = new JMenuItem("Trandafiri");
mi23.addActionListener(this);
men1.add(mi11);
men1.add(mi12);
men11.add(mi13);
men11.add(mi14);
men1.add(men11);
men1.add(mi15);
men2.add(mi21);
men2.add(mi22);
men2.add(mi23);
mb.add(men1);
mb.add(men2);
et = new JLabel();
et.setBounds(75,40,150,17);
et.setFont(new Font("Comic Sans MS",Font.ITALIC+Font.BOLD,14));
add(et);
setJMenuBar(mb);
setVisible(true);
}//Meniu
public void actionPerformed(ActionEvent ev)
{
String t = ev.getActionCommand();
et.setText("Ai ales "+t);
}//actionPerformed
}//class Meniu
public class testMeniu
{
public static void main(String arg[])
{
Meniu f = new Meniu("Testam componentele pentru meniuri...");
}///main
}//class testMeniu

Ex 8: Folosindu-va de programele de la exerciiile anterioare ncercai sa desprindei modul n care


pot fi folosite principalele componente vizuale (JButton, JLabel, JTextField, JComboBox,
JCheckBox, JRadioButton, JMenuBar, JMenu, JMenuItem) apoi notai pe caiet informaiile
desprinse.
NOT: Pentru rezolvarea exerciiului putei folosii i ajutorul oferit de distribuia Java instalat pe
calculator (din JCreator se alege meniul Help apoi JDK Help sau direct se apas
combinaia de taste Ctrl+F1).

Ex 9: Scriei programul Java de mai jos apoi rulai-l i analizai rezultatele afiate.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FerestreDialog extends JFrame implements ActionListener
{
FerestreDialog(String titlu)
{
super(titlu);
setBounds(100,100,370,180);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container x = getContentPane();
x.setLayout(null);
JButton b1 = new JButton("Mesaj simplu");
b1.setBounds(10,10,150,25);
b1.addActionListener(this);
x.add(b1);
JButton b2 = new JButton("Introducere date");
b2.setBounds(10,45,150,25);
b2.addActionListener(this);
x.add(b2);
JButton b3 = new JButton("Dialog simplu");
b3.setBounds(10,80,150,25);
b3.addActionListener(this);
x.add(b3);
JButton b4 = new JButton("Dialog eroare");
b4.setBounds(200,10,150,25);
b4.addActionListener(this);
x.add(b4);
JButton b5 = new JButton("Dialog informare");
b5.setBounds(200,45,150,25);
b5.addActionListener(this);
x.add(b5);
JButton b6 = new JButton("Dialog intrebare");
b6.setBounds(200,80,150,25);
b6.addActionListener(this);
x.add(b6);
JButton b7 = new JButton("Dialog avertizare");
b7.setBounds(200,115,150,25);
b7.addActionListener(this);
x.add(b7);
setVisible(true);
}//Fereastra
public void actionPerformed(ActionEvent ev)
{

String t = ev.getActionCommand();
if(t.compareTo("Mesaj simplu")==0)
{
//s-a apasat b1 -> MessageDialog
JOptionPane.showMessageDialog(this,
"Acesta este un mesaj scurt...");
}
if(t.compareTo("Introducere date")==0)
{
//s-a apasat b2 -> InputDialog
String rasp = JOptionPane.showInputDialog(this,
"Introdu ceva (numar / text).......");
if (rasp!=null)
JOptionPane.showMessageDialog(this,
"Ai introdus: "+rasp);
else JOptionPane.showMessageDialog(this,
"Ai apasat CANCEL");
}
if(t.compareTo("Dialog simplu")==0)
{
//s-a apasat b3 -> ConfirmDialog
int rasp = JOptionPane.showConfirmDialog(this,
"Am dreptate ca-i adevarat?",
"Fereastra confirmare",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE);
if(rasp==0)
JOptionPane.showMessageDialog(this,
"Ai apasat butonul Yes!");
else JOptionPane.showMessageDialog(this,
"Ai apasat butonul No!");
}
if(t.compareTo("Dialog eroare")==0)
{
//s-a apasat b4 -> ConfirmDialog
int rasp = JOptionPane.showConfirmDialog(this,
"A aparut o problema!...\n Vrei sa continuam?",
"Fereastra eroare", JOptionPane.YES_NO_OPTION,
JOptionPane.ERROR_MESSAGE);
if(rasp==0)
JOptionPane.showMessageDialog(this,
"Ai apasat butonul Yes!");
else JOptionPane.showMessageDialog(this,
"Ai apasat butonul No!");
}
if(t.compareTo("Dialog informare")==0)
{
//s-a apasat b5 -> ConfirmDialog
int rasp = JOptionPane.showConfirmDialog(this,
"Toate merg bine....:)",
"Fereastra informare",
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(rasp==0)

10

JOptionPane.showMessageDialog(this,
"Ai apasat butonul Yes!");
else JOptionPane.showMessageDialog(this,
"Ai apasat butonul No!");
}
if(t.compareTo("Dialog intrebare")==0)
{
//s-a apasat b6 -> ConfirmDialog
int rasp = JOptionPane.showConfirmDialog(this,
"Crezi ca se putea mai bine?",
"Fereastra intrebare",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(rasp==0)
JOptionPane.showMessageDialog(this,
"Ai apasat butonul Yes!");
else JOptionPane.showMessageDialog(this,
"Ai apasat butonul No!");
}
if(t.compareTo("Dialog avertizare")==0)
{
//s-a apasat b7 -> ConfirmDialog
int rasp = JOptionPane.showConfirmDialog(this,
"Te avertizez ca daca vei continua"+
"\ns-ar putea ca fie periculos....",
"Fereastra avertizare",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if(rasp==0)
JOptionPane.showMessageDialog(this,
"Ai apasat butonul Yes!");
else if(rasp==1)
JOptionPane.showMessageDialog(this,
"Ai apasat butonul No!");
else
JOptionPane.showMessageDialog(this,
"Ai apasat butonul Cancel!");
}
}//actionPerformed
}//class Fereastra
public class testFerestreDialog
{
public static void main(String arg[])
{
FerestreDialog f = new FerestreDialog("Ferestre dialog Java");
}///main
}//class testFerestreDialog

Ex 10: Rezolvai urmtoarele probleme folosind diferite componente vizuale i / sau ferestre de
dialog adecvate.
Determinarea soluiilor reale ale unei ecuaii de gradul II. Dac delta este negativ se va
afia un mesaj de eroare.
11

Se citesc doua numere naturale. S se afieze suma, diferena,produsul ctul, restul i


rezultatul mpririi reale a celor doua numere. Toate operaiile(citire i op aritmetice)
se vor alege dintr-un meniu. Vezi ex.7.
Se citesc doua numere naturale. S se afieze suma, diferena,produsul ctul, restul i
rezultatul mpririi reale a celor doua numere. Toate operaiile aritmetice se vor alege
din butoane radio iar numerele vor fi introduse n componente text(JTextField).

12

You might also like