📄 selectformat.java
字号:
/*Import required packages*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.util.*;
import javax.swing.event.*;
import java.awt.GridLayout;
/*
Class: SelectFormat-Creates a user interface to set the date format.
Methods:
actionPerformed(): Defines the operations to be performed, when the user clicks any button.
*/
public class SelectFormat extends JFrame implements ActionListener
{
/* Declare variables*/
Font f,f1;
JPanel pane,buttonPanel;
JRadioButton rb1,rb2,rb3,rb4,rb5,rb6;
ButtonGroup bg;
JLabel title;
JButton ok,cancel;
public static String retvalue="dd/mm/yyyy";
SpeechCalendar calendar = null;
/*
SelectFormat():Default constructor of SelectFormat class
Parameters:
calendar: An object of SpeechCalendar class
Return Type: NA
*/
public SelectFormat(SpeechCalendar calendar)
{
this.calendar = calendar;
/* Initialize the object of JPanel class*/
pane=new JPanel();
/* Initialize a JPanel to add buttons.*/
buttonPanel=new JPanel();
/*
Set the layout of JPanel, buttonPanel
*/
buttonPanel.setLayout(new GridLayout(1,2));
/*
Initialize the JRadioButtons
*/
rb1=new JRadioButton("dd/mm/yy");
rb2=new JRadioButton("dd/mm/yyyy");
rb3=new JRadioButton("mm/dd/yy");
rb4=new JRadioButton("mm/dd/yyyy");
rb5=new JRadioButton("yy/mm/dd");
rb6=new JRadioButton("yyyy/mm/dd");
/*
Initialize a ButtonGroup
*/
bg=new ButtonGroup();
/*
Add JRadioButtons to the ButtonGroup
*/
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
bg.add(rb4);
bg.add(rb5);
bg.add(rb6);
/*
Set the layout of the JPanel, pane
*/
pane.setLayout(new GridLayout(8,1));
/*
Create objects of Font class
*/
f=new Font("Verdana",Font.BOLD,12);
f1=new Font("Verdana",Font.BOLD,10);
/*
Set the title of the SelectFormat window
*/
title=new JLabel(" Select the format of the date ");
title.setFont(f);
/*
Initialize objects of JButton class
*/
ok=new JButton("OK");
cancel=new JButton("Cancel");
/*
Set font of buttons
*/
ok.setFont(f1);
cancel.setFont(f1);
/*
Add action listener to the JButtons
*/
ok.addActionListener(this);
cancel.addActionListener(this);
/*
Add JButtons to the buttonPanel
*/
buttonPanel.add(ok);
buttonPanel.add(cancel);
/*
Set the contents of the JPanel, pane
*/
pane.add(title);
pane.add(rb1);
pane.add(rb2);
pane.add(rb3);
pane.add(rb4);
pane.add(rb5);
pane.add(rb6);
pane.add(buttonPanel);
getContentPane().add(pane);
}
/*
actionPerformed(): Defines the actions to be performed, when the user clicks a button.
Parameters:
ev: An object of ActionEvent
Return Type:NA
*/
public void actionPerformed(ActionEvent ev)
{
/*
This code executes when the user clicks the OK button
*/
if(ev.getSource()==ok)
{
/*
Retrieve all elements of the Buttongroup in an enumeration
*/
Enumeration enum=bg.getElements();
/*
check if there exists any element in the enumeration
*/
while(enum.hasMoreElements())
{
JRadioButton button=(JRadioButton)enum.nextElement();
/*
Retrieve the label of the selected JRadioButton
*/
if (button.isSelected())
{
retvalue=button.getText();
this.setVisible(false);
calendar.setFormat(retvalue);
}
}
}
/*
This code executes when the user clicks the Cancel button
*/
else if(ev.getSource()==cancel)
{
this.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -