package cn.com.joycode.nimble.demo; import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Set; public class SwingBasicDemo { public static void setDefaultSize(int size) { Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet(); Object[] keys = keySet.toArray(new Object[0]); for (Object key : keys) { if (key != null && key.toString().toLowerCase().contains("font")) { System.out.println(key); Font font = UIManager.getDefaults().getFont(key); if (font != null) { font = font.deriveFont((float) size); UIManager.put(key, font); } } } } public static void main(String[] args) { // 在事件派发线程中运行 SwingUtilities.invokeLater(() -> { setDefaultSize(12); JFrame jfrm = new JFrame("编程小战"); jfrm.setSize(800, 600); jfrm.setVisible(true); JLabel label = new JLabel("我去淄博吃烧烤!", SwingConstants.CENTER); label.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.BLUE)); label.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { JOptionPane.showMessageDialog(jfrm, "别闹!"); } }); jfrm.getContentPane().add(label); jfrm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jfrm.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); }); } }
运行后是这个样子:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END