Table of Contents

GUI #1

Main.java

package gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
 
/**
 * Tlacitko s hezcim vykreslovanim
 * @author Martin
 */
class MojeTlacitko extends JButton{
 
    MojeTlacitko(String string) {
        super(string);
    }
 
    @Override
    public void paint(Graphics g) {
        /* nejdriv vykreslime puvodni tlacitko
           muze se nam to hodit */
        super.paint(g);
 
        /* nastavime si cervenou barvu */
        g.setColor(Color.red);
        /* plechovka */
        g.fillRect(0, 0, getWidth(), getHeight());
 
        g.setColor(Color.black);
        /* nakreslime obdelnik */
        g.drawRect(0, 0, getWidth()-1, getHeight()-1);
        g.drawRect(1, 1, getWidth()-3, getHeight()-3);
 
        /* predpocitame, kolik zabere text */
        int ofx = g.getFontMetrics().stringWidth(getText()) /2 ;
        int ofy = g.getFontMetrics().getHeight() / 2;
 
        /* vykreslime text, trefime se pokud mozno do stredu */
        g.drawString(this.getText(),getWidth()/2 - ofx, getHeight()/2 + ofy -1);
    }
}
 
public class Main extends JFrame {
 
    JButton tlacitko = new JButton("Ahoj");
    JButton tlacitko2 = new MojeTlacitko("Nazdar");
    JComboBox combo = new JComboBox();
 
    public Main() {
        setSize(200, 150);
        /*
         * jak se chovat pri zavreni okna, dobre pro hlavni okno aplikace
         * setDefaultCloseOperation(EXIT_ON_CLOSE);
         */
 
        /* layout - zpusob rozmisteni prvku */
        setLayout(new BorderLayout());
 
        /*
         * polozky comboboxu
         */
        combo.addItem("a");
        combo.addItem("b");
 
        /*
         * konteinter pro komponenty okna + pridani prvku
         * tohle neni potreba, ono lze volat add i nad oknem samotnym
         */
        Container c = getContentPane();
 
        c.add(tlacitko, BorderLayout.NORTH);
        c.add(combo, BorderLayout.SOUTH);
        c.add(tlacitko2, BorderLayout.CENTER);
 
        /*
         * action listener pro tlacitko 2 pres anonymni tridu.
         * Je obcas uzitecne volat metodu okna, protoze pak mame pristup k oknu
         * samotnemu.
         */
        tlacitko2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    tlacitko2stisknuto(e);
            }
        });
 
        /*
         * tlacitko 1
         */
        tlacitko.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tlacitko1stisknuto(e);
            }
        });
 
        /*
         * listener na zmenu vyberu, zajima nas jen stav nove vybrane polozky
         */
        combo.addItemListener(new ItemListener() {
 
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED){
                    tlacitko.setText((String)e.getItem());
                }
            }
        });
 
        /*
         * listener na udalosti okna.
         * protoze windowlistener ma moc metod, pouzijeme adapter, ktery implementuje
         * vsechny metody prazdnym kodem, a prekryjeme jen tu co nas zajima
         */
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent evt){
                System.out.println("Konec");
                System.exit(0);
            }
        });
 
        /*
         * kdykoliv v kodu muzeme zavolat prekresleni komponenty.
         * Tady je to k nicemu, gui se o to stara samo,
         * V nekterych pripadech se ale muze znalost teto metody hodit
         */
        tlacitko2.repaint();
    }
 
    private void tlacitko2stisknuto(ActionEvent e){
        /*
         * Eventy maji ruzne flagy, ktere jsou ulozene v ruznych stavovych promennych
         * zde je promenna modifiers, ktera uklada informaci o stisknutych klavesach pri kliku
         * jedna se o celociselnou promennou. Kazdy flag je reprezentovan jednim bitem (on/off).
         * Nemusime si poradi pamatovat, mame konstanty, ktere obsahuji prave a jen jeden bit
         * zapnuty. Takze staci trocha hrani s bitovymi operatory and a or.
         */
        if ((e.getModifiers() & (ActionEvent.CTRL_MASK + ActionEvent.SHIFT_MASK)) == ActionEvent.CTRL_MASK + ActionEvent.SHIFT_MASK){
            System.exit(0);
        }
        else {
            JOptionPane.showMessageDialog(rootPane, "Musis se vic snazit! Zkus Ctrl+Shift");
        }
    }
 
    private void tlacitko1stisknuto(ActionEvent e){
        JOptionPane.showMessageDialog(rootPane, "Neco provedeme, sledujte combo box");
        String item = "brum" + ((int)(Math.random()*100));
        combo.addItem(item);
        combo.setSelectedItem(item);
    }
 
    public static void main(String[] args) {
        JFrame okno = new Main();
        okno.setVisible(true);
    }
}
pjvomo/gui1.txt · Last modified: 2011/08/16 20:33 by 127.0.0.1
Back to top
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0