📄 loggerpanel.java
字号:
package org.theblueplanet.annealing.ui;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import javax.swing.BorderFactory;import javax.swing.ButtonGroup;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.border.EtchedBorder;import org.apache.log4j.Logger;import org.theblueplanet.annealing.FileAppenderManager;/** * Displays 2 JRadioButtons to select or unselect File logging. * As of version 1.0, this class writes to a file JSimul.log in the home * directory (remedy/additional flexibility is high on the to-do list). * * @author Charles M間nin * @since October 29, 2001 * @version 1.0 */public class LoggerPanel extends JPanel { private static Logger logger = Logger.getLogger("org.theblueplanet.annealing"); private JPanel buttonPanel; private JPanel sliderPanel; private JLabel label1 = new JLabel("Log result every"); private JLabel label2 = new JLabel("iterations"); private final static String yesAppendString = "yes, append"; private final static String yesOverwriteString = "yes, overwrite"; private final static String noString = "no"; private static final String yesAppendCommand = "yesA"; private static final String yesOverwriteCommand = "yesO"; private static final String noCommand = "no"; private AnnealingSchemeFrame parent; private LoggerFrequencySlider loggerFrequencySlider; private int loggerFrequency = LoggerFrequencySlider.defaultValue; /** * Constructor for the LoggerPanel object */ public LoggerPanel(AnnealingSchemeFrame parent) { super(); logger.debug("Instantiating " + this.getClass().getName()); this.parent = parent; init(); display(); } /** * Set JPanel layout */ private void init() { this.setLayout(new GridLayout(2, 1)); this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED)); parent.setFileAppenderState(FileAppenderManager.APPEND); createButtonPanel(); createSliderPanel(); } /** * Display the JPanels */ private void display() { this.add(buttonPanel); this.add(sliderPanel); } /** * Creates a JPanel that contains the JRadioButtons * * @return Description of the Returned Value */ private void createButtonPanel() { buttonPanel = new JPanel(); buttonPanel.add(new JLabel("Log results to file ?")); ButtonGroup group = new ButtonGroup(); RadioListener listener = new RadioListener(); createRadioButton(buttonPanel, group, listener, yesAppendString, yesAppendCommand, KeyEvent.VK_A, true); createRadioButton(buttonPanel, group, listener, yesOverwriteString, yesOverwriteCommand, KeyEvent.VK_O, false); createRadioButton(buttonPanel, group, listener, noString, noCommand, KeyEvent.VK_N, false); } private void createSliderPanel() { sliderPanel = new JPanel(); loggerFrequencySlider = new LoggerFrequencySlider(this); sliderPanel.add(label1); sliderPanel.add(loggerFrequencySlider); sliderPanel.add(label2); } /** * Creates a JRadioButton and adds it to the ButtonGroup and the JPanel. * * @param buttonPanel The JPanel to add the JRadioButton to * @param group The ButtonGroup to add the JRadioButton to * @param listener The Listener to register * @param title The button text * @param key The Mnemonic * @param selected True if this button is to be selected by default */ private void createRadioButton(JPanel buttonPanel, ButtonGroup group, RadioListener listener, String title, String actionCommand, int key, boolean selected) { JRadioButton button = new JRadioButton(title); button.setMnemonic(key); button.setActionCommand(actionCommand); button.setSelected(selected); group.add(button); buttonPanel.add(button); button.addActionListener(listener); } protected void setLoggerFrequency(int frequency){ parent.setLoggerFrequency(frequency); this.loggerFrequency = frequency; } private class RadioListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(yesAppendCommand)) { parent.setFileAppenderState(FileAppenderManager.APPEND); loggerFrequencySlider.setActivated(true); } else if (e.getActionCommand().equals(yesOverwriteCommand)) { parent.setFileAppenderState(FileAppenderManager.OVERWRITE); loggerFrequencySlider.setActivated(true); } else { parent.setFileAppenderState(FileAppenderManager.NO_APPEND); loggerFrequencySlider.setActivated(false); } logger.debug("action performed on RadioListener"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -