8.1.3 创建包含一个方法调用的监听器
格式: yellowButton.addActionListener(EventHandler.create(ActionListener.class,this,"setTitle","source.text"));
EventHandler.create 参数解释:
- 监听器名称
- 控件对象
- 2中控件对应方法
- 设置的值是监听器传入的 EventObject 对应的那个属性值
示例:改标题
public class Main {
public static void main(String[] args) {
Main solution = new Main();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ButtonFrame b = new ButtonFrame();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setVisible(true);
}
});
}
}
class ButtonFrame extends JFrame implements ActionListener{
public static final int W = 300;
public static final int H = 200;
private JPanel buttonPanel;
public ButtonFrame(){
setTitle("ButtonTest");
setSize(W,H);
Toolkit t = Toolkit.getDefaultToolkit();
Dimension d = t.getScreenSize();
setLocation(((int)d.getWidth()-W)/2,((int)d.getHeight()-H)/2);
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
buttonPanel = new JPanel();
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
//与下面三个this等价
yellowButton.addActionListener(EventHandler.create(ActionListener.class,this,"setTitle","source.text"));
blueButton.addActionListener(EventHandler.create(ActionListener.class,this,"setTitle","source.text"));
redButton.addActionListener(EventHandler.create(ActionListener.class,this,"setTitle","source.text"));
// yellowButton.addActionListener(this);
// blueButton.addActionListener(this);
// redButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
setTitle(((JButton)event.getSource()).getText());
}
}
总结:限制太多,不好用
8.1.4 实例:改变风格
- 改配置文件 jdk目录/jre/lib/swing.properties
Swing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel
风格怎么找呢?我们先在IDEA按 shift 两次,找到 MotifLookAndFeel,然后发现它有个父类 BasicLookAndFeel
光标放在 BasicLookAndFeel 上按 F4。
得到有这几种风格
swing.properties 这个文件根本不存在,自己建一个
加入如下内容:
swing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel
页面就变灰了
2.直接在代码中加在控件上
String plat = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try {
UIManager.setLookAndFeel(plat);
SwingUtilities.updateComponentTreeUI(buttonPanel);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
完整代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.EventHandler;
public class Main {
public static void main(String[] args) {
Main solution = new Main();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ButtonFrame b = new ButtonFrame();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setVisible(true);
}
});
}
}
class ButtonFrame extends JFrame implements ActionListener{
public static final int W = 300;
public static final int H = 200;
private JPanel buttonPanel;
public ButtonFrame(){
setTitle("ButtonTest");
setSize(W,H);
Toolkit t = Toolkit.getDefaultToolkit();
Dimension d = t.getScreenSize();
setLocation(((int)d.getWidth()-W)/2,((int)d.getHeight()-H)/2);
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
buttonPanel = new JPanel();
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
String plat = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try {
UIManager.setLookAndFeel(plat);
SwingUtilities.updateComponentTreeUI(buttonPanel);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
//与下面三个this等价
yellowButton.addActionListener(EventHandler.create(ActionListener.class,this,"setTitle","source.text"));
blueButton.addActionListener(EventHandler.create(ActionListener.class,this,"setTitle","source.text"));
redButton.addActionListener(EventHandler.create(ActionListener.class,this,"setTitle","source.text"));
// yellowButton.addActionListener(this);
// blueButton.addActionListener(this);
// redButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
setTitle(((JButton)event.getSource()).getText());
}
}
效果:
作者的示例:换皮肤
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {
Main solution = new Main();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
PlatFrame b = new PlatFrame();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setVisible(true);
}
});
}
}
class PlatFrame extends JFrame{
private int W = 300;
private int H = 200;
private JPanel buttonPanel;
public PlatFrame(){
setTitle("PlafTest");
setSize(W,H);
buttonPanel = new JPanel();
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
for(UIManager.LookAndFeelInfo info:infos){
makeButton(info.getName(), info.getClassName());
}
add(buttonPanel);
}
private void makeButton(String name, final String plafName){
JButton button = new JButton(name);
buttonPanel.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
UIManager.setLookAndFeel(plafName);
SwingUtilities.updateComponentTreeUI(PlatFrame.this);
}catch (Exception ee){
ee.printStackTrace();
}
}
});
}
}
感想:多写兼容性的方式,少想写死的方式,这样可以增加代码的可读性、可扩展性
- 配置文件,此方法适合配置大多数的控件
- 代码,写死的方式,适合固定风格
- 获取所有样式,可以切换,可以固定也可以换风格
相关内容:选择 《Java核心技术 卷1》查找相关笔记
喜欢的话,点个赞吧~!平时做题,以及笔记内容将更新到公众号。
关注公众号,互相学习:钰娘娘知识汇总