Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lec_05

.pdf
Скачиваний:
13
Добавлен:
31.05.2015
Размер:
914.74 Кб
Скачать

Модель обработки событий

Register Listener

Event Source

Event Listener

Send Notification

User Action

Event Object

События

Слушатели

Список типов слушателей http://docs.oracle.com/javase/tutorial/uiswing/events/api.html

Listener Interface

Adapter Class

Listener Methods

 

 

 

ActionListener

none

actionPerformed(ActionEvent)

 

 

 

 

 

keyPressed(KeyEvent)

KeyListener

KeyAdapter

keyReleased(KeyEvent)

 

 

keyTyped(KeyEvent)

 

 

 

 

 

mouseClicked(MouseEvent)

 

MouseAdapter,

mouseEntered(MouseEvent)

MouseListener

mouseExited(MouseEvent)

MouseInputAdapter

 

mousePressed(MouseEvent)

 

 

 

 

mouseReleased(MouseEvent)

 

 

 

 

 

windowActivated(WindowEvent)

 

 

windowClosed(WindowEvent)

 

 

windowClosing(WindowEvent)

WindowListener

WindowAdapter

windowDeactivated(WindowEvent)

 

 

windowDeiconified(WindowEvent)

 

 

windowIconified(WindowEvent)

 

 

windowOpened(WindowEvent)

 

 

 

Пример обработки событий: Beeper

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Beeper extends JPanel implements ActionListener {

public Beeper() {

super(new BorderLayout());

JButton button = new JButton("Click Me"); button.setPreferredSize(new Dimension(200, 80)); add(button, BorderLayout.CENTER); button.addActionListener(this);

}

public void actionPerformed(ActionEvent e) { Toolkit.getDefaultToolkit().beep(); } private static void createAndShowGUI() {

JFrame frame = new JFrame("Beeper"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new Beeper());

frame.pack(); frame.setVisible(true);

}

Полный пример можно взять здесь

http://docs.oracle.com/javase/tutorial/uiswing/

public static void main(String[] args) { … }

examples/events/BeeperProject/src/events/Bee

}

per.java

Пример обработки событий: Beeper 2

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Beeper extends JFrame {

public Beeper(String name) { super(name); } private static void createAndShowGUI() { Beeper frame = new Beeper("Beeper 2");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); button.setPreferredSize(new Dimension(200, 80)); button.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) { Toolkit.getDefaultToolkit().beep();

}

});

frame.getContentPane().add(button, BorderLayout.CENTER); frame.pack(); frame.setVisible(true);

}

public static void main(String[] args) { … }

}

Swing: Look and Feel

import javax.swing.UIManager;

public class LookAndFellsWeHave { public static void main(String[] args) {

for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) System.out.println(info);

}

}

1.Metal javax.swing.plaf.metal.MetalLookAndFeel

2.Nimbus com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel

3.CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel

4.Windows com.sun.java.swing.plaf.windows.WindowsLookAndFeel

5.Windows Classic com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel

Примеры Look and Feel из Demo

Изменение Look and Fell

SwingSet2

SwingSet3 http://download.java.net/javadesktop/swingset3/SwingSet3.jnlp

Изменение темы

Metalworks

Изменение размеров

Laffy http://download.java.net/javadesktop/laffy/Laffy.jnlp

Swing: Look and Feel

import javax.swing.UIManager;

public class LookAndFellsWeHave { public static void main(String[] args) {

UIManager.LookAndFeelInfo[] lfis = UIManager.getInstalledLookAndFeels(); for (UIManager.LookAndFeelInfo lfi: lfis)

System.out.println(lfi);

}

}

1.Metal javax.swing.plaf.metal.MetalLookAndFeel

2.Nimbus com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel

3.CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel

4.Windows com.sun.java.swing.plaf.windows.WindowsLookAndFeel

5.Windows Classic com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel

Swing: Look and Feel

public static void main(String[] args) {

try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }

catch (Exception e) { e.printStackTrace(); }

}

public void actionPerformed(ActionEvent evt) {

LookAndFeel currentLookAndFeel = UIManager.getLookAndFeel(); try {

if (currentLookAndFeel.isNativeLookAndFeel()) UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

else

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } SwingUtilities.updateComponentTreeUI(this); this.pack();

}

}

Swing: MetalLookAndFeel и темы

public static void main(String[] args) { try {

MetalLookAndFeel.setCurrentTheme(new javax.swing.plaf.metal.OceanTheme ()); UIManager.setLookAndFeel(new MetalLookAndFeel());

}

catch (Exception e) { e.printStackTrace(); }

}

class MyTheme extends DefaultMetalTheme { public String getName() {

return "MyTheme";

}

private final FontUIResource controlTextFont =

new FontUIResource("Comic Sans MS", Font.PLAIN, 36); public FontUIResource getControlTextFont() {

return controlTextFont ;

}

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]