📄 tonicdemo.java
字号:
return null;
}
/** Parses the specified String and returns a KeyStroke from it. The
* KeyStroke is composed of the last character of the String, plus
* a modifier, if the String starts with "CTRL+", "ALT+", or "SHIFT+".
*/
private KeyStroke getAccelerator(String acc)
{
int modifiers=0;
if(acc.startsWith("CTRL+"))
modifiers|=KeyEvent.CTRL_MASK;
if(acc.startsWith("ALT+"))
modifiers|=KeyEvent.CTRL_MASK;
if(acc.startsWith("SHIFT+"))
modifiers|=KeyEvent.SHIFT_MASK;
return KeyStroke.getKeyStroke(acc.charAt(acc.length()-1), modifiers);
}
/** Returns a JPanel with a JTree mockup */
private JPanel createDataStructurePanel()
{
String treeDef[]=
{
"Root", "Data",
"Data", "Statistics",
"Data", "Profiling",
"Data", "Analysis",
"Data", "Images",
"Statistics", "Time",
"Statistics", "Volume",
"Time", "Average",
"Time", "Cumulated",
"Time", "Histogram",
"Volume", "Average",
"Volume", "Cumulated",
"Volume", "Histogram",
"Analysis", "Deconvolution",
"Analysis", "Noise reduction",
"Analysis", "Signal enhancement",
"Analysis", "FFT",
"Profiling", "Average",
"Profiling", "Weakest of chain",
"Profiling", "Trace",
"Images", "Hyperbolic map",
"Images", "Gauss",
"Images", "Histograms"
};
JPanel result=new JPanel(new BorderLayout());
result.setBackground(Color.WHITE);
result.setBorder(new SplitPaneContentBorder(true));
JPanel titlePanel=new JPanel(new FlowLayout(FlowLayout.LEFT, 6, 2));
titlePanel.add(new JLabel("Data structure"));
titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, UIManager.getColor("Button.borderColor")));
result.add(BorderLayout.NORTH, titlePanel);
JPanel subPanel=new JPanel(new BorderLayout());
subPanel.setBackground(Color.WHITE);
result.add(BorderLayout.CENTER, subPanel);
TreeNode tn=getTree(treeDef);
JTree tree=new JTree(tn);
tree.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
JScrollPane sp=new JScrollPane(tree);
sp.setBorder(null);
subPanel.add(BorderLayout.CENTER, sp);
tree.expandRow(1);
tree.expandRow(2);
tree.expandRow(7);
tree.expandRow(11);
return result;
}
/** Returns another JPanel with a tree mockup */
private JPanel createOutlinePanel()
{
String treeDef[]=
{
"Root", "Assets",
"Assets", "com",
"com", "digitprop",
"digitprop", "lf",
"lf", "DPLookAndFeel",
"lf", "ScrollBarUI",
"lf", "OptionPaneUI",
"lf", "MenuBarUI",
"lf", "ButtonUI",
"Assets", "resources",
"resources", "images",
"images", "Overview",
"images", "Icon_01",
"images", "Icon_02"
};
JPanel result=new JPanel(new BorderLayout());
result.setBackground(Color.WHITE);
result.setBorder(new SplitPaneContentBorder(true));
JPanel titlePanel=new JPanel(new FlowLayout(FlowLayout.LEFT, 6, 2));
titlePanel.add(new JLabel("Outline"));
titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, UIManager.getColor("Button.borderColor")));
result.add(BorderLayout.NORTH, titlePanel);
JPanel subPanel=new JPanel(new BorderLayout());
subPanel.setBackground(Color.WHITE);
result.add(BorderLayout.CENTER, subPanel);
TreeNode tn=getTree(treeDef);
JTree tree=new JTree(tn);
tree.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
JScrollPane sp=new JScrollPane(tree);
sp.setBorder(null);
subPanel.add(BorderLayout.CENTER, sp);
tree.expandRow(1);
tree.expandRow(2);
tree.expandRow(3);
return result;
}
/** Returns a JDesktopPane with two JInternalFrames */
private JDesktopPane createDeskopPanel()
{
JDesktopPane dp=new JDesktopPane();
JInternalFrame inf=new JInternalFrame("Auxiliary data");
inf.setClosable(true);
inf.setMaximizable(true);
inf.setIconifiable(true);
inf.setResizable(true);
ImageIcon ii=(ImageIcon)getIcon(getClass(), "resources/img1.jpg");
inf.setContentPane(new ImageScroller(ii));
//inf.pack();
inf.setBounds(100, 80, 300, 200);
inf.setVisible(true);
JInternalFrame inf2=new JInternalFrame("Profiling results");
inf2.setClosable(true);
inf2.setMaximizable(true);
inf2.setIconifiable(true);
inf2.setResizable(true);
ii=(ImageIcon)getIcon(getClass(), "resources/img2.jpg");
inf2.setContentPane(new ImageScroller(ii));
inf2.setBounds(10, 10, 180, 300);
inf2.setVisible(true);
dp.add(inf);
dp.add(inf2);
return dp;
}
/** Creates the application's main panel and returns it */
private JPanel createMainPanel()
{
JPanel topleft=new JPanel(new BorderLayout());
topleft.setBackground(Color.WHITE);
topleft.setBorder(new SplitPaneContentBorder(true));
JPanel titlePanel=new JPanel(new FlowLayout(FlowLayout.LEFT, 6, 2));
titlePanel.add(new JLabel("Editor - \\assets\\outline.rtf"));
titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, UIManager.getColor("Button.borderColor")));
topleft.add(BorderLayout.NORTH, titlePanel);
JPanel subPanel=new JPanel(new BorderLayout());
subPanel.setBackground(Color.WHITE);
mainPanel=subPanel;
topleft.add(BorderLayout.CENTER, subPanel);
try
{
URL url=getClass().getResource("resources/demo.rtf");
jep=new JEditorPane(url);
JScrollPane sp2=new JScrollPane(jep);
sp2.setBorder(null);
subPanel.add(BorderLayout.CENTER, sp2);
mainPanelContent=sp2;
}
catch(Exception e)
{
e.printStackTrace();
}
return topleft;
}
/** Creates a tree of TreeNodes from the specified String array.
* The array is expected to contain String pairs, consisting of
* the parent node name, and a child node name.
*/
private TreeNode getTree(String tree[])
{
Hashtable table=new Hashtable();
TreeNode root=null;
for(int i=0; i<tree.length; i+=2)
{
if(tree[i].equals("Root"))
{
root=new DefaultMutableTreeNode(tree[i+1]);
table.put(tree[i+1], root);
}
else
{
DefaultMutableTreeNode node=new DefaultMutableTreeNode(tree[i+1]);
DefaultMutableTreeNode parent=(DefaultMutableTreeNode)table.get(tree[i]);
if(parent!=null)
parent.add(node);
table.put(tree[i+1], node);
}
}
return root;
}
/** Returns a JPanel with a JTable */
private JPanel createTasksPanel()
{
JPanel result=new JPanel(new BorderLayout());
result.setBackground(Color.WHITE);
result.setBorder(new SplitPaneContentBorder(false));
table=new JTable(new TonicDemoTableModel());
JScrollPane sp2=new JScrollPane(table);
sp2.setBorder(null);
result.add(BorderLayout.CENTER, sp2);
// Set column widths for the table
table.getColumnModel().getColumn(0).setPreferredWidth(20);
table.getColumnModel().getColumn(1).setPreferredWidth(270);
table.getColumnModel().getColumn(2).setPreferredWidth(60);
table.getColumnModel().getColumn(3).setPreferredWidth(120);
return result;
}
/** Handles action events. This is not very object-oriented, but
* more than sufficient for this demo. Reacts on some of the
* menu items.
*/
public void actionPerformed(ActionEvent e)
{
if(((JMenuItem)e.getSource()).getText().equals(MENU_JEDITORPANE))
{
// Display a JEditorPane at the top right panel
mainPanel.removeAll();
mainPanel.add(BorderLayout.CENTER, mainPanelContent);
}
else if(((JMenuItem)e.getSource()).getText().equals(MENU_JINTERNALFRAME))
{
// Display a JDesktopPane with JInternalFrames at the top right panel
mainPanel.removeAll();
mainPanel.add(BorderLayout.CENTER, desktopPane);
}
else if(((JMenuItem)e.getSource()).getText().equals(MENU_OPEN))
{
// Display a file chooser
JFileChooser fc=new JFileChooser();
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
fc.setLocation(screen.width/2-fc.getWidth()/2, screen.height/2-fc.getHeight()/2);
fc.showOpenDialog(this);
}
else if(((JMenuItem)e.getSource()).getText().equals(MENU_JOPTIONPANE))
{
// Displays a JOptionPane
int result=JOptionPane.showConfirmDialog(this, "Have you seen my glasses?", "Duh...!", JOptionPane.YES_NO_CANCEL_OPTION);
if(result==JOptionPane.YES_OPTION)
JOptionPane.showMessageDialog(this, "Yes, but I won't tell you where!", "Muahahaha...", JOptionPane.INFORMATION_MESSAGE);
else if(result==JOptionPane.NO_OPTION)
JOptionPane.showMessageDialog(this, "No, your glasses were nowhere to be found.", "Nope...", JOptionPane.ERROR_MESSAGE);
}
else if(((JMenuItem)e.getSource()).getText().equals(MENU_DIALOG))
{
// Displays the demo dialog
TonicDemoDialog td=new TonicDemoDialog(this, "Preferences");
int x=getX()+getWidth()/2-td.getWidth()/2;
int y=getY()+getHeight()/2-td.getHeight()/2;
td.setLocation(x, y);
td.show();
}
else if(((JMenuItem)e.getSource()).getText().equals(MENU_EXIT))
{
// Exits the application
setVisible(false);
dispose();
System.exit(0);
}
repaint();
}
/** Initializes the GUI, including the central panels of the
* application.
*/
private void initGUI()
{
this.toolBar=initToolBar();
JToolBar tb=toolBar;
JPanel topleft=createDataStructurePanel();
JPanel bottomleft=createOutlinePanel();
JPanel topright=createMainPanel();
JPanel bottomright=createTasksPanel();
desktopPane=createDeskopPanel();
left=new JSplitPane(JSplitPane.VERTICAL_SPLIT, topleft, bottomleft);
left.setOneTouchExpandable(true);
left.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
right=new JSplitPane(JSplitPane.VERTICAL_SPLIT, topright, bottomright);
right.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
sp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
getContentPane().add(BorderLayout.NORTH, tb);
getContentPane().add(BorderLayout.CENTER, sp);
}
/** Main method: Sets the Tonic look and feel, then creates an instance
* of this class and shows it.
*/
public static void main(String args[])
{
if(args.length==0 || !args[0].startsWith("m"))
{
try
{
UIManager.setLookAndFeel(new TonicLookAndFeel());
}
catch(UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
}
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
TonicDemo instance=new TonicDemo("Tonic Look And Feel v1.0");
instance.setIconImage(TonicLookAndFeel.getTonicIcon("open").getImage());
}
/** Used to display an image within a scroll pane - taken from the
* SwingSet demo.
*/
class ImageScroller extends JScrollPane
{
public ImageScroller(Icon icon)
{
super();
JPanel p = new JPanel();
p.setBackground(Color.white);
setBorder(null);
p.setLayout(new BorderLayout() );
p.add(new JLabel(icon), BorderLayout.CENTER);
getViewport().add(p);
getHorizontalScrollBar().setUnitIncrement(10);
getVerticalScrollBar().setUnitIncrement(10);
}
public Dimension getMinimumSize()
{
return new Dimension(25, 25);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -