SimpleExamplepublic class SimpleExample extends JPanel
Fields Summary |
---|
static JFrame | frame | static Font | smallFont | static Font | mediumFont | static Font | bigFont | private ResourceBundle | resources | private ComponentOrientation | co |
Constructors Summary |
---|
public SimpleExample()
// Load our resource bundle
loadResources();
JRadioButton oneButton, twoButton, threeButton;
JButton button;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.getAllFonts();
// Setup the fonts
smallFont = new Font("Bitstream Cyberbit", Font.PLAIN, 14);
mediumFont = new Font("Bitstream Cyberbit", Font.PLAIN, 18);
bigFont = new Font("Bitstream Cyberbit", Font.PLAIN, 20);
co = ComponentOrientation.getOrientation(Locale.getDefault());
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
String language = Locale.getDefault().getLanguage();
// Create the buttons
button = new JButton(resources.getString("Hello"));
button.setToolTipText(resources.getString("HelloToolTip"));
button.setFont(mediumFont);
// Setup the buttons
oneButton = new JRadioButton(resources.getString("One"));
oneButton.setFont(mediumFont);
oneButton.setMnemonic(resources.getString("OneMnemonic").charAt(0));
oneButton.setHorizontalAlignment(JButton.TRAILING);
oneButton.setHorizontalTextPosition(JButton.TRAILING);
twoButton = new JRadioButton(resources.getString("Two"));
twoButton.setFont(mediumFont);
twoButton.setMnemonic(resources.getString("TwoMnemonic").charAt(0));
twoButton.setHorizontalAlignment(JButton.TRAILING);
twoButton.setHorizontalTextPosition(JButton.TRAILING);
threeButton = new JRadioButton(resources.getString("Three"));
threeButton.setFont(mediumFont);
threeButton.setMnemonic(resources.getString("ThreeMnemonic").charAt(0));
threeButton.setHorizontalAlignment(JButton.TRAILING);
threeButton.setHorizontalTextPosition(JButton.TRAILING);
// Group the radio buttons
ButtonGroup group = new ButtonGroup();
group.add(oneButton);
group.add(twoButton);
group.add(threeButton);
// Register a listener for the radio buttons
RadioListener myListener = new RadioListener();
oneButton.addActionListener(myListener);
twoButton.addActionListener(myListener);
threeButton.addActionListener(myListener);
// Setup the button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
TitledBorder tb = new TitledBorder(resources.getString("Numbers"));
tb.setTitleFont(smallFont);
tb.setTitleJustification(
co.isLeftToRight() ? TitledBorder.LEFT : TitledBorder.RIGHT);
buttonPanel.setBorder(tb);
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(button);
buttonPanel.add(oneButton);
buttonPanel.add(twoButton);
buttonPanel.add(threeButton);
add(buttonPanel, BorderLayout.CENTER);
// Setup the date panel
JPanel datePanel = new JPanel();
datePanel.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
tb = new TitledBorder(resources.getString("Dates"));
tb.setTitleFont(smallFont);
tb.setTitleJustification(
co.isLeftToRight() ? TitledBorder.LEFT : TitledBorder.RIGHT);
datePanel.setBorder(tb);
datePanel.setLayout(new BoxLayout(datePanel,BoxLayout.X_AXIS));
datePanel.add(Box.createRigidArea(new Dimension(5,1)));
DateFormatSymbols dfs = new DateFormatSymbols();
JComboBox months = new JComboBox(dfs.getMonths());
months.setFont(mediumFont);
String weekDays[] = dfs.getWeekdays();
JComboBox days = new JComboBox();
days.setFont(mediumFont);
// Determine what day is the first day of the week
GregorianCalendar cal = new GregorianCalendar();
int firstDayOfWeek = cal.getFirstDayOfWeek();
int dayOfWeek;
for (dayOfWeek = firstDayOfWeek; dayOfWeek < weekDays.length; dayOfWeek++)
days.addItem(weekDays[dayOfWeek]);
for (dayOfWeek = 0; dayOfWeek < firstDayOfWeek; dayOfWeek++)
days.addItem(weekDays[dayOfWeek]);
if (!co.isLeftToRight()) {
datePanel.add(days);
datePanel.add(Box.createRigidArea(new Dimension(5,1)));
datePanel.add(months);
datePanel.add(Box.createRigidArea(new Dimension(5,1)));
} else {
datePanel.add(months);
datePanel.add(Box.createRigidArea(new Dimension(5,1)));
datePanel.add(days);
datePanel.add(Box.createRigidArea(new Dimension(5,1)));
}
add(datePanel);
// Setup the formatting panel
JPanel formatPanel = new JPanel();
formatPanel.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
tb = new TitledBorder(resources.getString("Formats"));
tb.setTitleFont(smallFont);
tb.setTitleJustification(co.isLeftToRight() ?
TitledBorder.LEFT : TitledBorder.RIGHT);
formatPanel.setBorder(tb);
formatPanel.setLayout(new BoxLayout(formatPanel,BoxLayout.X_AXIS));
formatPanel.add(Box.createRigidArea(new Dimension(5,1)));
double theNumber = 1234.56;
NumberFormat nFormat = NumberFormat.getInstance();
NumberFormat cFormat = NumberFormat.getCurrencyInstance();
NumberFormat pFormat = NumberFormat.getPercentInstance();
DateFormat dFormat = DateFormat.getDateInstance();
JLabel numberLabel = new JLabel(nFormat.format(theNumber));
numberLabel.setForeground(Color.black);
numberLabel.setFont(bigFont);
JLabel percentLabel = new JLabel(pFormat.format(theNumber));
percentLabel.setForeground(Color.black);
percentLabel.setFont(bigFont);
JLabel currencyLabel = new JLabel(cFormat.format(theNumber));
currencyLabel.setForeground(Color.black);
currencyLabel.setFont(bigFont);
JLabel dateLabel = new JLabel(dFormat.format(new Date()));
dateLabel.setForeground(Color.black);
dateLabel.setFont(bigFont);
formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
if (co.isLeftToRight()) {
formatPanel.add(numberLabel);
formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
formatPanel.add(percentLabel);
formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
formatPanel.add(currencyLabel);
formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
formatPanel.add(dateLabel);
} else {
formatPanel.add(dateLabel);
formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
formatPanel.add(currencyLabel);
formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
formatPanel.add(percentLabel);
formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
formatPanel.add(numberLabel);
}
formatPanel.add(Box.createRigidArea(new Dimension(25,1)));
add(formatPanel);
|
Methods Summary |
---|
private static void | applyComponentOrientation(java.awt.Component c, java.awt.ComponentOrientation o)
c.setComponentOrientation(o);
if (c instanceof JMenu) {
JMenu menu = (JMenu)c;
int ncomponents = menu.getMenuComponentCount();
for (int i = 0 ; i < ncomponents ; ++i) {
applyComponentOrientation( menu.getMenuComponent(i), o );
}
} else if (c instanceof Container) {
Container container = (Container)c;
int ncomponents = container.getComponentCount();
for (int i = 0 ; i < ncomponents ; ++i) {
applyComponentOrientation( container.getComponent(i), o );
}
}
| public javax.swing.JMenuBar | createMenuBar()
JMenuBar menuBar = new JMenuBar();
JMenu file =
(JMenu) menuBar.add(new JMenu(resources.getString("FileMenu")));
file.setFont(mediumFont);
file.setMnemonic(resources.getString("FileMenuMnemonic").charAt(0));
JMenuItem exitItem = (JMenuItem)
file.add(new JMenuItem(resources.getString("FileMenuExit")));
exitItem.setFont(mediumFont);
exitItem.setMnemonic(resources.getString("FileMenuExitMnemonic").charAt(0));
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menuBar.add(new LocaleChanger());
return menuBar;
| private static javax.swing.JFrame | getFrame()
return frame;
| private void | loadResources()
try {
resources = ResourceBundle.getBundle("resources.Simple",
Locale.getDefault());
} catch (MissingResourceException mre) {
mre.printStackTrace();
System.exit(1);
}
| public static void | main(java.lang.String[] argv)
SimpleExample panel = new SimpleExample();
frame = new JFrame("Simple Example");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.setJMenuBar(panel.createMenuBar());
frame.getContentPane().add("Center", panel);
frame.pack();
frame.setVisible(true);
| public void | reloadResources()
try {
resources = ResourceBundle.getBundle("resources.Simple", Locale.getDefault());
} catch (MissingResourceException mre) {
mre.printStackTrace();
System.exit(1);
}
|
|