多语言展示
当前在线:941今日阅读:142今日分享:25

Java内部类(分类介绍)

普通内部类package edu.test.ch7;import javax.swing.JFrame;import java.awt.BorderLayout;import java.awt.Color;import java.awt.EventQueue;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.UIManager;import javax.swing.JButton;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class ButtonTest extends JFrame { /** * */ private static final long serialVersionUID = -57262900L; private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { try { UIManager.setLookAndFeel('com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel'); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { ButtonTest frame = new ButtonTest(); frame.setVisible(true); frame.contentPane.requestFocus(); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ButtonTest() { setTitle('普通内部类的简单应用'); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 300, 200); contentPane = new JPanel(); contentPane.setLayout(null); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); final JButton redButton = new JButton(); redButton.setText('红色'); redButton.setBounds(15, 20, 82, 30); redButton.addActionListener(new ColorAction(Color.RED)); contentPane.add(redButton); final JButton greenButton = new JButton(); greenButton.setText('绿色'); greenButton.setBounds(100, 20, 82, 30); greenButton.addActionListener(new ColorAction(Color.GREEN)); contentPane.add(greenButton); final JButton blueButton = new JButton(); blueButton.setText('蓝色'); blueButton.setBounds(185, 20, 82, 30); blueButton.addActionListener(new ColorAction(Color.BLUE)); contentPane.add(blueButton); } ///在类中的普通内部类 private class ColorAction implements ActionListener { private Color background; public ColorAction(Color background) { this.background = background; } public void actionPerformed(ActionEvent e) {contentPane.setBackground(background); } }}局部内部类package edu.test.ch7;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.SimpleDateFormat;import java.util.Date;import javax.swing.JOptionPane;import javax.swing.Timer;public class AlarmClock {private int delay;private boolean flag;public AlarmClock(int delay, boolean flag) { this.delay = delay; this.flag = flag;}public void start() { class Printer implements ActionListener { @Override public void actionPerformed(ActionEvent e) { SimpleDateFormat format = new SimpleDateFormat('k:m:s'); String result = format.format(new Date()); System.out.println('当前的时间是:' + result); if (flag) { Toolkit.getDefaultToolkit().beep(); } } } new Timer(delay, new Printer()).start(); //启动定时器类就是相当于在设定的频率下执事件 ///定时器类javax.swing.Timer就是一个按预定频率触发ActionEvent事件的源组件。 }public static void main(String[] args) {AlarmClock clock = new AlarmClock(1000, true); clock.start(); JOptionPane.showMessageDialog(null, '是否退出?'); System.exit(0);}}匿名内部类package edu.test.ch7;import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.UIManager;import javax.swing.JButton;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JLabel;import javax.swing.SwingConstants;import java.awt.GridLayout;public class ImageViewer extends JFrame { /** * */ private static final long serialVersionUID = 22117198L; private JPanel contentPane; private JLabel label; /** * Launch the application. */ public static void main(String[] args) { try { UIManager.setLookAndFeel('com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel'); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { ImageViewer frame = new ImageViewer(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ImageViewer() { setTitle('匿名内部类的简单应用'); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 300, 240); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel = new JPanel(); contentPane.add(panel, BorderLayout.CENTER); panel.setLayout(new BorderLayout(0, 0)); JPanel buttonPanel = new JPanel(); panel.add(buttonPanel, BorderLayout.NORTH); JButton button1 = new JButton('图片1'); button1.setFont(new Font('微软雅黑', Font.PLAIN, 16)); ///这里是匿名内部类的核心代码 button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setIcon(new ImageIcon('src/images/1.png')); } }); buttonPanel.setLayout(new GridLayout(2, 3, 5, 5)); buttonPanel.add(button1); JButton button2 = new JButton('图片2'); button2.setFont(new Font('微软雅黑', Font.PLAIN, 16)); button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setIcon(new ImageIcon('src/images/2.png')); } }); buttonPanel.add(button2); JButton button3 = new JButton('图片3'); button3.setFont(new Font('微软雅黑', Font.PLAIN, 16)); button3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setIcon(new ImageIcon('src/images/3.png')); } }); buttonPanel.add(button3); JButton button4 = new JButton('图片4'); button4.setFont(new Font('微软雅黑', Font.PLAIN, 16)); button4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setIcon(new ImageIcon('src/images/4.png')); } }); buttonPanel.add(button4); JButton button5 = new JButton('图片5'); button5.setFont(new Font('微软雅黑', Font.PLAIN, 16)); button5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setIcon(new ImageIcon('src/images/5.png')); } }); buttonPanel.add(button5); JButton button6 = new JButton('图片6'); button6.setFont(new Font('微软雅黑', Font.PLAIN, 16)); button6.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setIcon(new ImageIcon('src/images/6.png')); } }); buttonPanel.add(button6); label = new JLabel(''); label.setHorizontalAlignment(SwingConstants.CENTER); panel.add(label, BorderLayout.CENTER); }}静态内部类package edu.test.ch7;public class MaxMin {public static class Result { private double max; private double min; public Result(double max, double min) { this.max = max; this.min = min; } public double getMax() { return max; } public double getMin() { return min; } } public static Result getResult(double[] array) { double max = Double.MIN_VALUE; double min = Double.MAX_VALUE; for (double i : array) { if (i > max) { max = i; } if (i < min) { min = i; } } return new Result(max, min); }public static void main(String[] args) {double[] array = new double[5]; for (int i = 0; i < array.length; i++) { array[i] = 100*Math.random(); } System.out.println('源数组:'); for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } System.out.println('最大值:' + MaxMin.getResult(array).getMax()); System.out.println('最小值:' + MaxMin.getResult(array).getMin());}}
推荐信息