Sie sind auf Seite 1von 5

package fereastra2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Fereastra2 extends JFrame implements ActionListener {
JButton b1 = new JButton("Close");
public Fereastra2(){
add(new JLabel("Aplicatie Swing"));//eticheta
add(b1);// adaugarea butonului
b1.addActionListener(this);//buton adaugat pe ascultator
setLayout(new FlowLayout());// manager de pozitionare
b1.setBackground(Color.GREEN);// culoarea butonului
setSize(500,300);// dimensiunea ferestrei
setLocationRelativeTo(null);// pozitionare centrala
setResizable(false); // fereastra sa nu poata fi modificata (redimensionata)
setTitle("Swing1");// titlul ferestrei
setVisible(true);// fereastra vizibila
}
@Override
public void actionPerformed(ActionEvent ev){
System.exit(0);
}
public static void main(String[] args) {
new Fereastra2();
}
}

package fereastra2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Fereastra2 extends JFrame{
private JButton b1 = new JButton("0");
private int nr;
public Fereastra2(){
super("Fereastra 2");
add(b1);
b1.addActionListener(new Inner());
setLayout(new FlowLayout());
setSize(300,300);
setLocationRelativeTo(null);

setVisible(true);
}
private class Inner implements ActionListener{
public void actionPerformed(ActionEvent ev){
nr++;
b1.setText(String.valueOf(nr));
}
}
public static void main(String[] args) {
new Fereastra2();
}
}

3. Incrementarea individuala a butoanelor.


package fereastra2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Fereastra2 extends JFrame implements ActionListener{
private JButton b1 = new JButton("0");
private JButton b2 = new JButton("0");
private int nr1, nr2;
public Fereastra2(){
super("Fereastra");
setLayout(new FlowLayout());
setSize(200,200);
setLocationRelativeTo(null);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent ev){
if(ev.getSource()==b1){
nr1++;
b1.setText(String.valueOf(nr1));
}
if(ev.getSource()==b2){

nr2++;
b2.setText(String.valueOf(nr2));
}
}
public static void main(String[] args) {
new Fereastra2();
}
}

package fereastra2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Fereastra2 extends JFrame{
private JButton b1 = new JButton("0");
private JButton b2 = new JButton("Buton2");
private int nr1, nr2;
public Fereastra2(){
super("Fereastra");
setLayout(new FlowLayout());
setSize(200,200);
setLocationRelativeTo(null);
add(b1);
add(b2);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
nr1++;
b1.setText(String.valueOf(nr1));
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
if(b2.getText().equals("Buton2")){
b2.setText("Hello");
}else{
b2.setText("Buton2");
}
}
});
setVisible(true);
}

public static void main(String[] args) {


new Fereastra2();
}
}

package fereastra2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Fereastra2 extends JFrame implements ActionListener{
private
private
private
private
private
private
private
private

JPanel p = new JPanel();


final JMenuBar mb = new JMenuBar();// bara de meniu
final JMenu menu = new JMenu("Optiuni");//meniu
final JMenu mt = new JMenu("Culori");
final JMenuItem a1 = new JMenuItem("RED");
final JMenuItem a2 = new JMenuItem("GREEN");
final JMenuItem a3 = new JMenuItem("BLUE");
final JMenuItem exit = new JMenuItem("Exit");

public Fereastra2(){
super("Fereastra");
add(p);
setJMenuBar(mb);
mb.add(menu);
menu.add(mt);
menu.add(exit);
mt.add(a1);
mt.add(a2);
mt.add(a3);
KeyStroke ks = KeyStroke.getKeyStroke(" alt O");
menu.setMnemonic('o');
KeyStroke ks1 = KeyStroke.getKeyStroke("control R");
a1.setAccelerator(ks1);
//expresia Lambda
a1.addActionListener((ActionEvent ev) -> {
p.setBackground(Color.RED);
});
a2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
p.setBackground(Color.GREEN);
}
} );

a3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
p.setBackground(Color.BLUE);
}
} );
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent av){
int r = JOptionPane.showConfirmDialog(null, "Sigur doriti sa inchideti?",
"Intregare!",JOptionPane.YES_NO_OPTION);
if(r==JOptionPane.YES_OPTION){
System.exit(0);
}
}
}
);
// setLayout(new FlowLayout());
setSize(400,400);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Fereastra2();
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change
body of generated methods, choose Tools | Templates.
}
}

Das könnte Ihnen auch gefallen