Created
June 20, 2012 20:35
-
-
Save Cherubi/2962047 to your computer and use it in GitHub Desktop.
Graafinen käyttöliittymä
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package swing; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JLabel; | |
/** | |
* | |
* @author Cherubi | |
*/ | |
public class AjastinKuuntelija implements ActionListener { | |
private JLabel label; | |
public AjastinKuuntelija(JLabel label) { | |
this.label = label; | |
} | |
@Override | |
public void actionPerformed(ActionEvent ae) { | |
label.setText("center"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package swing; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JLabel; | |
import javax.swing.Timer; | |
/** | |
* | |
* @author Cherubi | |
*/ | |
public class NappulaKuuntelija implements ActionListener { | |
private JLabel label; | |
public NappulaKuuntelija(JLabel label) { | |
this.label = label; | |
} | |
public void actionPerformed(ActionEvent ae) { | |
//System.out.println("Tapahtuma"); | |
label.setText("Nappulaa painastu."); | |
Timer ajastin = new Timer(3000, new AjastinKuuntelija(label)); | |
ajastin.setRepeats(false); | |
ajastin.start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package swing; | |
import java.awt.*; | |
import javax.swing.*; | |
/** | |
* | |
* @author Cherubi | |
*/ | |
public class Swing implements Runnable { | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(new Swing()); | |
} | |
private JFrame kehys; | |
@Override | |
public void run() { | |
luoIkkuna(); | |
//kehys.setLayout(new BoxLayout(kehys.getContentPane(), BoxLayout.Y_AXIS)); | |
//kehys.setLayout(new FlowLayout()); | |
//kehys.setLayout(new GridLayout(4, 5)); | |
kehys.setLayout(new BorderLayout()); | |
luoKomponentteja(); | |
} | |
private void luoIkkuna() { | |
kehys = new JFrame(); | |
kehys.setPreferredSize(new Dimension(400, 300)); | |
kehys.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
kehys.pack(); | |
kehys.setVisible(true); | |
} | |
private void luoKomponentteja() { | |
//JLabel etiketti = new JLabel("Teksti"); | |
//for (int i=0; i<20; i++) { | |
// kehys.add(luoNappula(i+1 + "")); | |
//} | |
JLabel teksti = new JLabel("center"); | |
kehys.add(teksti, BorderLayout.CENTER); | |
kehys.add(luoNappula("north"), BorderLayout.NORTH); | |
kehys.add(luoNappula("south"), BorderLayout.SOUTH); | |
kehys.add(luoPalkki(teksti), BorderLayout.WEST); | |
kehys.add(luoNappula("east"), BorderLayout.EAST); | |
} | |
private JButton luoNappula(String teksti) { | |
JButton nappula = new JButton(teksti); | |
nappula.setFocusable(false); | |
return nappula; | |
} | |
private Container luoPalkki(JLabel teksti) { | |
Container palkki = new Container(); | |
palkki.setLayout(new GridLayout(5,1)); | |
for (int i = 0; i < 5; i++) { | |
JButton nappula = luoNappula(i+1 + ""); | |
nappula.addActionListener(new NappulaKuuntelija(teksti)); | |
palkki.add(nappula); | |
} | |
return palkki; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment