#頭條創作挑戰賽#
❝
❤️作者簡介:大家好,我是小虛竹。Java領域優質創作者,CSDN部落格專家,華為雲享專家,掘金年度人氣作者,阿裡雲專家部落客,51CTO專家部落客
❤️技術活,該賞
❤️點贊 收藏 ⭐再看,養成習慣
❞
零、前言
今天是學習 「JAVA語言」 打卡的第39天,我的學習政策很簡單,題海政策+ 費曼學習法。如果能把這100題都認認真真自己實作一遍,那意味着 「JAVA語言」 已經築基成功了。後面的進階學習,可以繼續跟着我,一起走向架構師之路。
一、題目描述
題目:在類中,除以可以定義參數,方法和塊,還可以定義類。這種類叫做内部類。
實作:寫一個簡單的圖檔檢視軟體,支援6張圖檔,通過單擊檢視不同的圖檔。
二、解題思路
寫一個圖檔檢視類ImageViewer,這個類繼承了JFrame。
在窗體裡定義6個按鈕和一個标簽
「當隻需要建立類的一個對象時,可以使用匿名内部類」
三、代碼詳解
圖檔檢視類:
public class ImageViewer extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1311689308422117198L;
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("JAVA進階技術-内部類3(匿名内部類)");
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) {
String fileName = ImageViewer.class.getClassLoader().getResource("images/1.jpg").getPath();
label.setIcon(new ImageIcon(fileName));
}
});
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) {
String fileName = ImageViewer.class.getClassLoader().getResource("images/2.jpg").getPath();
label.setIcon(new ImageIcon(fileName));
}
});
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) {
String fileName = ImageViewer.class.getClassLoader().getResource("images/3.jpg").getPath();
label.setIcon(new ImageIcon(fileName));
}
});
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) {
String fileName = ImageViewer.class.getClassLoader().getResource("images/4.jpg").getPath();
label.setIcon(new ImageIcon(fileName));
}
});
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) {
String fileName = ImageViewer.class.getClassLoader().getResource("images/5.jpg").getPath();
label.setIcon(new ImageIcon(fileName));
}
});
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) {
String fileName = ImageViewer.class.getClassLoader().getResource("images/6.jpg").getPath();
label.setIcon(new ImageIcon(fileName));
}
});
buttonPanel.add(button6);
label = new JLabel("");
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label, BorderLayout.CENTER);
}
}
多學一個知識點
如何擷取項目中的圖檔檔案位址:
❝ ImageViewer.class.getClassLoader().getResource("images/6.jpg").getPath()
❞
我是虛竹哥,我們下一題見~