📄 guiutils.java
字号:
package ranab.gui;
import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import ranab.io.IoUtils;
/**
* This is the GUI utility class.
*
* @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
*/
public
class GuiUtils {
private static JFileChooser mDirChoose = null;
private static JFileChooser mFileChoose = null;
private static final GuiUtils SELF_REF = new GuiUtils();
/**
* Create image icon.
*/
public static ImageIcon createImageIcon(String imgResource) {
InputStream is = null;
ByteArrayOutputStream out = null;
try {
is = SELF_REF.getClass().getClassLoader().getResourceAsStream(imgResource);
if (is != null) {
out = new ByteArrayOutputStream();
byte buff[] = new byte[1024];
int count = 0;
while ( -1 != (count = is.read(buff)) ) {
out.write(buff, 0, count);
}
buff = out.toByteArray();
if (buff.length != 0){
return new ImageIcon(buff);
}
}
}
catch(IOException ex) {
}
finally {
IoUtils.close(is);
IoUtils.close(out);
}
return null;
}
/**
* Create splash window. Returns null if image not found.
*/
public static JWindow createSplashWindow(String imgResource) {
ImageIcon icon = createImageIcon(imgResource);
if (icon == null) {
return null;
}
JLabel lab = new JLabel();
lab.setIcon(icon);
Dimension iDim = new Dimension(icon.getIconWidth(), icon.getIconHeight());
JWindow splashWin = new JWindow();
splashWin.getContentPane().add(lab);
splashWin.setSize(iDim);
setLocation(splashWin);
return splashWin;
}
/**
* Display error message.
*/
public static void showErrorMessage(Component parent, String str) {
JOptionPane.showMessageDialog(parent, str, "Error!",
JOptionPane.ERROR_MESSAGE);
}
/**
* Display warning message.
*/
public static void showWarningMessage(Component parent, String str) {
JOptionPane.showMessageDialog(parent, str, "Warning!",
JOptionPane.WARNING_MESSAGE);
}
/**
* Display information.
*/
public static void showInformationMessage(Component parent, String str) {
JOptionPane.showMessageDialog(parent, str, "Information!",
JOptionPane.INFORMATION_MESSAGE );
}
/**
* Get confirmation.
*/
public static boolean getConfirmation(Component parent, String str) {
int res = JOptionPane.showConfirmDialog(parent,
str,
"Confirmation",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE
);
return(res == JOptionPane.YES_OPTION);
}
/**
* Get file name.
*/
public static String getFileName(Component parent) {
if (mFileChoose == null) {
mFileChoose = new JFileChooser();
mFileChoose.setFileSelectionMode(JFileChooser.FILES_ONLY);
}
int returnVal = mFileChoose.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
return mFileChoose.getSelectedFile().getAbsolutePath();
} else {
return null;
}
}
/**
* Get directory name.
*/
public static String getDirName(Component parent) {
if (mDirChoose == null) {
mDirChoose = new JFileChooser();
mDirChoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
int returnVal = mDirChoose.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
return mDirChoose.getSelectedFile().getAbsolutePath();
} else {
return null;
}
}
/**
* Update component look & feel.
*/
public static void updateLnF() {
if (mDirChoose != null) {
SwingUtilities.updateComponentTreeUI(mDirChoose);
}
if (mFileChoose != null) {
SwingUtilities.updateComponentTreeUI(mFileChoose);
}
}
/**
* Position properly - center.
*/
public static void setLocation(Component comp) {
Dimension cDim = comp.getSize();
Dimension wDim = Toolkit.getDefaultToolkit().getScreenSize();
comp.setLocation((wDim.width - cDim.width)/2, (wDim.height - cDim.height)/2);
}
/**
* Position with respect to the parent component.
*/
public static void setLocation(Component comp, Component parent) {
Dimension cDim = comp.getSize();
Rectangle pRect = parent.getBounds();
int x = pRect.x + (pRect.width - cDim.width)/2;
int y = pRect.y + (pRect.height - cDim.height)/2;
comp.setLocation(x, y);
}
/**
* Display a new panel. First removes all children, then add.
*/
public static void showNewPanel(Container parent, Component child) {
parent.removeAll();
parent.add(child);
parent.validate();
parent.repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -