📄 aboutdialog.java
字号:
/*
* 09/02/2004
*
* AboutDialog.java - An About dialog for an application.
* Copyright (C) 2003 Robert Futrell
* email@address.com
* www.website.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui;
import java.awt.Rectangle;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
import org.fife.RUtilities;
/**
* An "About Dialog" for an application.
*
* @author Robert Futrell
* @version 0.8
*/
public class AboutDialog extends JDialog implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 6658907392608834135L;
private JButton okButton;
private JButton licenseButton;
private Frame parent;
private JTabbedPane tabbedPane;
private ResourceBundle bundle;
/*****************************************************************************/
/**
* Creates a new <code>AboutDialog</code>.
*
* @param parent The owner of this dialog.
*/
public AboutDialog(Frame parent) {
this(parent, null);
}
/*****************************************************************************/
/**
* Creates a new <code>AboutDialog</code>.
*
* @param parent The owner of this dialog.
* @param title The title of the about dialog.
*/
public AboutDialog(Frame parent, String title) {
super(parent);
this.parent = parent;
bundle = ResourceBundle.getBundle("org.fife.ui.UI");
// Set the main content pane for the "About" dialog.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(UIUtilities.getEmpty5Border());
setContentPane(contentPane);
tabbedPane = new JTabbedPane();
contentPane.add(tabbedPane);
// Add a panel to the tabbed pane about the Java environment.
JPanel temp = UIUtilities.createTabbedPanePanel();
temp.setLayout(new BorderLayout());
temp.setBorder(UIUtilities.getEmpty5Border());
temp.add(new JLabel(bundle.getString("EnvironInfo")),
BorderLayout.NORTH);
InfoPanel environPanel = new InfoPanel(bundle);
// Make scroll pane's visible area "too small" so that the About
// dialog will only be as large as the largest panel added by the
// user.
temp.add(new RScrollPane(10,10, environPanel));
tabbedPane.add(bundle.getString("Environment"), temp);
// Add the OK and license buttons.
JPanel buttonPanel = new JPanel();
temp = new JPanel(new GridLayout(1,2, 5,0));
okButton = RUtilities.createRButton(bundle, "OK", "OKButtonMnemonic");
okButton.setActionCommand("OK");
okButton.addActionListener(this);
temp.add(okButton);
licenseButton = RUtilities.createRButton(bundle, "License",
"LicenseButtonMnemonic");
licenseButton.setActionCommand("License");
licenseButton.addActionListener(this);
temp.add(licenseButton);
buttonPanel.add(temp);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
// Put everything into a neat little package.
getRootPane().setDefaultButton(okButton);
setTitle(title!=null ? title : bundle.getString("AboutDialogTitle"));
setModal(true);
// Make the escape key hide the dialog.
JRootPane rootPane = getRootPane();
InputMap inputMap = rootPane.getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = rootPane.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "OnEsc");
actionMap.put("OnEsc", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
}
);
// Ensure that the first tab is the one initially shown
// (starting in J2SE 5.0, this isn't the default behavior;
// the last tab added is initially shown).
SwingUtilities.invokeLater(new Runnable() {
public void run() {
tabbedPane.setSelectedIndex(0);
}
});
}
/*****************************************************************************/
/**
* Called whenever an action occurs in this dialog.
*/
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("OK")) {
setVisible(false);
}
else if (actionCommand.equals("License")) {
GPLLicenseDialog licenseDialog = new GPLLicenseDialog();
licenseDialog.setVisible(true);
}
}
/*****************************************************************************/
/**
* Adds a panel to the tabbed pane.
*
* @param title The title for the panel.
* @param panel The panel to add.
*/
public void addPanel(String title, Component panel) {
// Keep the "Environment" tab last.
tabbedPane.insertTab(title, null, panel, null,
tabbedPane.getTabCount()-1);
pack();
setLocationRelativeTo(parent);
}
/*****************************************************************************/
/************************ PRIVATE INNER CLASSES ******************************/
/*****************************************************************************/
// This class is simply a dialog box that displays the GNU GPL.
class GPLLicenseDialog extends JDialog implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 3415329148564950239L;
GPLLicenseDialog() {
super();
setTitle(bundle.getString("LicenseDialogTitle"));
JPanel contentPane = new JPanel();
BoxLayout box = new BoxLayout(contentPane, BoxLayout.Y_AXIS);
contentPane.setLayout(box);
contentPane.setBorder(UIUtilities.getEmpty5Border());
setContentPane(contentPane);
JTextArea textArea = new JTextArea();
textArea.setText(
"This program is free software; you can redistribute it and/or modify " +
"it under the terms of the GNU General Public License as published by " +
"the Free Software Foundation; either version 2 of the License, or " +
"(at your option) any later version.\n\n" +
"This program is distributed in the hope that it will be useful, " +
"but WITHOUT ANY WARRANTY; without even the implied warranty of " +
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the " +
"GNU General Public License for more details.\n\n" +
"You should have received a copy of the GNU General Public License " +
"along with this program; if not, write to the Free Software " +
"Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA");
textArea.setFont(new Font("monospaced", Font.PLAIN, 12));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setCaretPosition(0);
RScrollPane scrollPane = new RScrollPane(400,200, textArea);
contentPane.add(scrollPane);
JPanel buttonPanel = new JPanel();
RButton okButton = RUtilities.createRButton(bundle, "OK",
"OKButtonMnemonic");
okButton.addActionListener(this);
buttonPanel.add(okButton);
contentPane.add(buttonPanel);
setModal(true);
setResizable(false);
pack();
setLocationRelativeTo(AboutDialog.this);
}
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
}
/*****************************************************************************/
/**
* The panel containing system information.
*/
class InfoPanel extends JPanel implements Scrollable {
private static final long serialVersionUID = 7101643582202764925L;
InfoPanel(ResourceBundle msg) {
super(new SpringLayout());
addProperty(msg.getString("Environment.Label.JavaVersion"),
"java.version");
addProperty(msg.getString("Environment.Label.JavaVendor"),
"java.vendor");
addProperty(msg.getString("Environment.Label.JavaVMVersion"),
"java.vm.version");
addProperty(msg.getString("Environment.Label.JavaVMVendor"),
"java.vm.vendor");
addProperty(msg.getString("Environment.Label.JavaSpecification"),
"java.specification.name");
addProperty(msg.getString("Environment.Label.JavaSpecificationVersion"),
"java.specification.version");
addProperty(msg.getString("Environment.Label.Classpath"),
"java.class.path");
addProperty(msg.getString("Environment.Label.OS"),
"os.name");
addProperty(msg.getString("Environment.Label.OSVersion"),
"os.version");
RUtilities.makeSpringCompactGrid(this, 9,2, //rows, cols
0,0, //initX, initY
6, 6); //xPad, yPad
}
/**
* Adds a property/value pair to the panel.
*/
private final void addProperty(String name, String key) {
JLabel label = new JLabel(name);
label.setFont(label.getFont().deriveFont(Font.BOLD));
add(label);
label = new JLabel(System.getProperty(key));
add(label);
}
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation, int direction) {
if (orientation==SwingConstants.VERTICAL)
return getHeight();
else // SwingConstants.HORIZONTAL
return getWidth();
}
public boolean getScrollableTracksViewportHeight() {
return false;
}
public boolean getScrollableTracksViewportWidth() {
return false;
}
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 8;
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -