strategypatternexample.java~95~
来自「《深入浅出设计模式》的完整源代码」· JAVA~95~ 代码 · 共 81 行
JAVA~95~
81 行
package exporter;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.*;
import java.awt.HeadlessException;
public class StrategyPatternExample
extends JFrame {
private JButton exportButton;
private JTable dataInput;
private JTextArea converterOutput;
private JRadioButton rbTabs, rbCSV, rbHTML;
public StrategyPatternExample () throws HeadlessException {
super ("Strategy Pattern Example");
addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent e) {
System.exit (0);
}
});
JPanel listPanel = new JPanel ();
JPanel controlPanel = new JPanel ();
// data input
dataInput = new JTable (new DataModel ());
JScrollPane dataInputPane = new JScrollPane (dataInput);
dataInput.setPreferredScrollableViewportSize (new Dimension (200, 100));
listPanel.add (dataInputPane);
// converter output
converterOutput = new JTextArea ();
converterOutput.setFont (new Font ("monospaced", 0, 12));
JScrollPane converterOutputPane = new JScrollPane (converterOutput);
converterOutputPane.setPreferredSize (new Dimension (200, 120));
listPanel.add (converterOutputPane);
exportButton = new JButton ("Export");
exportButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {
doExport ();
}});
ButtonGroup bg = new ButtonGroup ();
rbTabs = new JRadioButton ("Tabs");
rbCSV = new JRadioButton ("CSV");
rbHTML = new JRadioButton ("HTML");
bg.add (rbTabs);
bg.add (rbCSV);
bg.add (rbHTML);
controlPanel.setLayout (new FlowLayout ());
controlPanel.add (rbTabs);
controlPanel.add (rbCSV);
controlPanel.add (rbHTML);
controlPanel.add (exportButton);
getContentPane ().setLayout (new BorderLayout ());
getContentPane ().add (listPanel, BorderLayout.CENTER);
getContentPane ().add (controlPanel, BorderLayout.SOUTH);
pack ();
setVisible (true);
}
public void doExport(){
TableExporter te=null;
if(this.rbTabs.isSelected()){
te = new TableExporterTabs();
}
if(te!=null){
String ss="i love u";
this.converterOutput.setText(ss);
}
}
public static void main (String[] args) throws HeadlessException {
StrategyPatternExample spe = new StrategyPatternExample ();
spe.show ();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?