Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- class Komorka{
- private int wartosc=0;
- void setWartosc(int w){ wartosc=w;}
- int getWartosc(){ return wartosc; }
- }
- class TrzyKomorki{
- Komorka[] tk = new Komorka[3];
- TrzyKomorki(){
- int i;
- for (i=0;i<3;i++)
- tk[i] = new Komorka();
- }
- public String dajWartosci() {
- return tk[0].getWartosc() + " " + tk[1].getWartosc() + " " + tk[2].getWartosc();
- }
- void losuj(){
- for (int i=0;i<3;i++)
- tk[i].setWartosc( (int)(Math.random()*10));
- }
- void zamienMiejscami() {
- Komorka temp = tk[0];
- tk[0] = tk[2];
- tk[2] = temp;
- }
- }
- class GI extends JFrame {
- private TrzyKomorki komorki = new TrzyKomorki();
- private JButton pokazKomorkiB = new JButton("Pokaz komorki");
- private JButton losujB = new JButton("Losuj komorki");
- private JButton zamienWartosciB = new JButton("Zmien wartosci");
- private JLabel wynikT = new JLabel();
- GI() {
- setTitle("Trzy komorki GUI");
- Container container = getContentPane();
- container.setLayout(new GridLayout(2, 2));
- container.add(pokazKomorkiB);
- container.add(wynikT);
- container.add(losujB);
- container.add(zamienWartosciB);
- // pokazKomorkiB.addActionListener(e -> {
- // wynikT.setText(komorki.dajWartosci());
- // });
- pokazKomorkiB.addActionListener(new PokazKomorki());
- losujB.addActionListener(new LosujKomorki());
- zamienWartosciB.addActionListener(new ZamienMiejscami());
- // losujB.addActionListener(e -> {
- // komorki.losuj();
- // });
- //
- // zamienWartosciB.addActionListener(e -> {
- // komorki.zamienMiejscami();
- // });
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- wynikT.setEnabled(false);
- setVisible(true);
- }
- class PokazKomorki implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- wynikT.setText(komorki.dajWartosci());
- }
- }
- class LosujKomorki implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- komorki.losuj();
- }
- }
- class ZamienMiejscami implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- komorki.zamienMiejscami();
- }
- }
- public static void main(String[] args) {
- JFrame f = new GI();
- f.setSize(400,400);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement