📄 encodingschemes.java
字号:
package encoder_decoder;
/* Imports the java.awt package classes. */
import java.awt.*;
/* Imports the java.nio.charset package classes. */
import java.nio.charset.Charset;
/* Imports the java.util package classes. */
import java.util.SortedMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Collection;
import java.lang.Object;
import java.util.Enumeration;
/* Imports the java.awt.event package classes. */
import java.awt.event.*;
/* Imports the javax.swing package classes. */
import javax.swing.*;
import javax.swing.ButtonModel;
/*
class EncodingSchemes - This class creates a window that displays various encoding schemes.
Fields:
title - Creates a title label.
ok - Creates a OK button.
cancel - Creates a Cancel button.
rb - Creates the radio button.
buttonPanel - Creates a panel for button.
schemePanel - Creates a panel to display the encoding scheme.
numberPanel - Creates a panel to display the number of encoding scheme.
mainPanel - Creates a main panel.
encodingScheme - Contains the encoding scheme.
cs- Creates an object of the Charset class.
c - Creates an object of the Collection class.
Methods:
getSchemes() - This method is executed when the EncodingSchemes constructor loads.
actionPerformed() - This method is invoked when an end user clicks any button of the EncodingSchemes window.
*/
public class EncodingSchemes extends JDialog implements ActionListener
{
/* Declares the object of the SortedMap class. */
SortedMap sm;
/* Declares the object of the JLabel class. */
JLabel title;
/* Declares the objects of the JButton class. */
JButton ok;
JButton cancel;
/* Declares the object of the String class. */
String temp;
/* Declares the object of the ButtonGroup class. */
ButtonGroup bg;
/* Declares the object of the JRadioButton class. */
JRadioButton rb;
/* Declares the object of the Charset class. */
Charset cs;
/* Declares the object of the Collection class. */
Collection c;
/* Declares the objects of the JPanel class. */
JPanel buttonPanel;
JPanel schemePanel;
JPanel numberPanel;
JPanel mainPanel;
JScrollPane s_pane;
/* Declares the object of the Font class. */
Font f;
int s;
public EncoderDecoder ed;
/* Declares the object of the String class. */
public static String encodingScheme = "UTF-16";
/* Defines the default constructor of the EncodingSchemes class. */
public EncodingSchemes(EncoderDecoder ed)
{
this.ed = ed;
/* Sets the title of the Encoding Schemes window. */
setTitle("Encoding Schemes");
/* Sets the size of the Encoding Schemes window. */
setSize(280, 500);
/* Sets the re-sizability of the Encoding Schemes window to false. */
setResizable(false);
/* Initializes the object of the ButtonGroup class. */
bg=new ButtonGroup();
/* Initializes the object of the Font class. */
f=new Font("Verdana", Font.BOLD, 12);
/* Initializes the objects of the JPanel class. */
buttonPanel=new JPanel();
schemePanel=new JPanel();
numberPanel=new JPanel();
mainPanel=new JPanel();
/* Initializes the object of the JLabel class. */
title = new JLabel("Select the Encoding Scheme");
title.setFont(f);
/* Initializes and sets the layout of the button panel to grid layout. */
buttonPanel.setLayout(new GridLayout(1, 2));
/* Initializes and sets the layout of the number panel to border layout. */
numberPanel.setLayout(new BorderLayout());
/* Initializes and sets the layout of the main panel to border layout. */
mainPanel.setLayout(new BorderLayout());
/* Initializes the objects of the JButton class. */
ok=new JButton("OK");
cancel=new JButton("Cancel");
/* Adds the action listener events to the Ok and Cancel buttons. */
ok.addActionListener(this);
cancel.addActionListener(this);
/* Calls the getSchemes() method. */
getSchemes();
/* Adds various swing components on the panels. */
numberPanel.add(title, BorderLayout.NORTH);
buttonPanel.add(ok);
buttonPanel.add(cancel);
mainPanel.add(numberPanel, BorderLayout.NORTH);
mainPanel.add(s_pane, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
/* Adds the main panel to frame. */
getContentPane().add(mainPanel);
/*
addWindowListener - It contains a windowClosing() method.
windowClosing(): This method is called when the end user clicks the close button of the Window.
Parameter: we- Represents an object of the WindowEvent class.
Return Value: NA
*/
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
dispose();
}
});
}
/*
getSchemes() - This method returns the selected encoding scheme.
Parameter - NA
Return Value - NA
*/
public void getSchemes()
{
try
{
/* Retrieves the available character schemes. */
sm=Charset.availableCharsets();
/* Gets the size of sorted map object. */
s=sm.size();
/* Sets the layout of the schemePanel panel to the GridLayout. */
schemePanel.setLayout(new GridLayout(s, 1));
/* Retrieves the value from the sorted model object. */
c=sm.values();
/* Creates and initializes the object of the Iterator class. */
Iterator i=c.iterator();
while(i.hasNext())
{
temp = i.next().toString();
/* Initializes the object of the JRadioButton class. */
rb = new JRadioButton(temp);
if(temp.equals("UTF-16"))
{
rb.setSelected(true);
}
/* Adds the radio button to the button group. */
bg.add(rb);
/* Adds the radio button group to the schemePanel panel. */
schemePanel.add(rb);
s_pane = new JScrollPane(schemePanel);
}
}catch(Exception e)
{
System.out.println(e);
}
}
/*
actionPerformed() - This method is called when the end user clicks the any button.
Parameters: ev – Represents an objects of the ActionEvent class that contains the details of the event.
Return Value: NA
*/
public void actionPerformed(ActionEvent ev)
{
/* This section is executed when the end user clicks the OK button. */
if(ev.getSource()==ok)
{
/* Creates and initializes the object of the Enumeration class to
get the elements from the button group. */
Enumeration enum=bg.getElements();
while(enum.hasMoreElements())
{
JRadioButton button = (JRadioButton)enum.nextElement();
/* Checks the status of a radio button. */
if (button.isSelected())
{
/* Retrieves the text from the radio button. */
encodingScheme = button.getText();
ed.schemeText.setText(encodingScheme);
}
}
/* Disposes the window. */
dispose();
}
/* This section is executed when the end user clicks the Cancel button. */
else if(ev.getSource()==cancel)
{
/* Disposes the window. */
dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -