📄 exercise16_3.java
字号:
// Exercise16_3.java: Display country's flag and description.
// Descriptions are in the file
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Exercise16_3 extends JFrame implements ItemListener {
// Declare an array of Strings for flag titles
private String[] flagTitle = {"Canada", "China", "Denmark",
"France", "Germany", "India", "Norway", "United Kingdom",
"United States of America"};
// Declare an ImageIcon array for the national flags of 9 countries
private ImageIcon[] flagImage = new ImageIcon[9];
// Declare an array of strings for flag descriptions
private String[] flagDescription = new String[9];
// Declare and create a description panel
private DescriptionPanel descriptionPanel = new DescriptionPanel();
// The combo list for selecting countries
private JComboBox jcbo;
// Base directory for the files
String baseDirectory;
// Main Method
public static void main(String[] args) {
Exercise16_3 frame = new Exercise16_3();
frame.pack();
frame.setTitle("Exercise16_3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// Default Constructor
public Exercise16_3() {
// Load images info flagImage array
flagImage[0] = new ImageIcon("image/ca.gif");
flagImage[1] = new ImageIcon("image/china.gif");
flagImage[2] = new ImageIcon("image/denmark.gif");
flagImage[3] = new ImageIcon("image/fr.gif");
flagImage[4] = new ImageIcon("image/germany.gif");
flagImage[5] = new ImageIcon("image/india.gif");
flagImage[6] = new ImageIcon("image/norway.gif");
flagImage[7] = new ImageIcon("image/uk.gif");
flagImage[8] = new ImageIcon("image/us.gif");
// Set text description
for (int i=0; i<9; i++) {
flagDescription[i] = getDescription(i);
}
// Create items into the combo box
jcbo = new JComboBox(flagTitle);
// Set the first country (Canada) for display
setDisplay(0);
// Add combo box and description panel to the list
getContentPane().add(new JScrollPane(jcbo), BorderLayout.NORTH);
getContentPane().add(descriptionPanel, BorderLayout.CENTER);
// Register listener
jcbo.addItemListener(this);
}
// Handle item selection
public void itemStateChanged(ItemEvent e) {
setDisplay(jcbo.getSelectedIndex());
}
// Set display information on the description panel
public void setDisplay(int index) {
descriptionPanel.setTitle(flagTitle[index]);
descriptionPanel.setImageIcon(flagImage[index]);
descriptionPanel.setDescription(flagDescription[index]);
}
private String getDescription(int i) {
String result = new String();
try {
BufferedReader in = new BufferedReader(new FileReader(
"text/description" + i + ".txt"));
String line;
while ((line = in.readLine()) != null) {
result += line + '\n';
}
}
catch (IOException ex) {
System.out.println(ex);
}
return result;
}
}
// Define a panel for displaying image and text
//class DescriptionPanel extends JPanel {
// /** Label for displaying an image icon */
// private JLabel jlblImage = new JLabel();
//
// /** Label for displaying a title */
// private JLabel jlblTitle = new JLabel();
//
// /** Text area for displaying text */
// private JTextArea jtaTextDescription;
//
// /** Default constructor */
// public DescriptionPanel() {
// // Group image label and title label in a panel
// JPanel panel = new JPanel();
// panel.setLayout(new BorderLayout());
// panel.add(jlblImage, BorderLayout.CENTER);
// panel.add(jlblTitle, BorderLayout.SOUTH);
//
// // Create a scroll pane to hold text area
// JScrollPane scrollPane = new JScrollPane
// (jtaTextDescription = new JTextArea());
//
// // Center the title on the label
// jlblTitle.setHorizontalAlignment(JLabel.CENTER);
//
// // Set the font for the title and text
// jlblTitle.setFont(new Font("SansSerif", Font.BOLD, 16));
// jtaTextDescription.setFont(new Font("Serif", Font.PLAIN, 14));
//
// // Set lineWrap and wrapStyleWord true for text area
// jtaTextDescription.setLineWrap(true);
// jtaTextDescription.setWrapStyleWord(true);
// jtaTextDescription.setEditable(false);
//
// // Set preferred size for the scroll pane
// scrollPane.setPreferredSize(new Dimension(200, 100));
//
// // Set BorderLayout for the whole panel, add panel and scrollpane
// setLayout(new BorderLayout(5, 5));
// add(scrollPane, BorderLayout.CENTER);
// add(panel, BorderLayout.WEST);
// }
//
// /** Set the title */
// public void setTitle(String title) {
// jlblTitle.setText(title);
// }
//
// /** Set the image icon */
// public void setImageIcon(ImageIcon icon) {
// jlblImage.setIcon(icon);
// Dimension dimension = new Dimension(icon.getIconWidth(),
// icon.getIconHeight());
// jlblImage.setPreferredSize(dimension);
// }
//
// /** Set the text description */
// public void setTextDescription(String text) {
// jtaTextDescription.setText(text);
// }
//}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -