Fişă de Laborator: Java - Principalele Componente Vizuale
Fişă de Laborator: Java - Principalele Componente 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);
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
{
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.*;
}//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);
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
12