📄 backgrounddialog.java
字号:
String colorString = currentColor.toString();
imageFileNameField.setText(colorString.substring(colorString.indexOf('[')));
this.repaint();
}
else if (actionCommand.equals("PressedImageRadioButton")) {
colorBrowseButton.setEnabled(false);
imageBrowseButton.setEnabled(true);
viewingColors = false;
//imageFileNameField.setVisible(true);
if (currentImageFileName!=null) {
imageFileNameField.setText(currentImageFileName);
okButton.setEnabled(true);
}
else {
imageFileNameField.setText(msg.getString("NoImageLabel"));
okButton.setEnabled(false);
}
this.repaint();
}
else if (actionCommand.equals("BrowseColors")) {
Color tempColor = JColorChooser.showDialog(null,
msg.getString("BGColorChooserTitle"),
currentColor);
if (tempColor != null) {
currentColor = tempColor;
String colorString = currentColor.toString();
imageFileNameField.setText(colorString.substring(colorString.indexOf('[')));
this.repaint();
//currentImageFileName = null;
}
}
else if (actionCommand.equals("BrowseImages")) {
int returnVal = imageChooser.showOpenDialog(this);
// If they selected a file and clicked "OK", open the flie!
if (returnVal == JFileChooser.APPROVE_OPTION) {
currentImageFileName = imageChooser.getSelectedFile().getAbsolutePath();
previewImage = new ImageIcon(currentImageFileName).getImage();
int length = currentImageFileName.length();
String extension = currentImageFileName.substring(length-3,length).toUpperCase();
if ((!extension.equals("GIF") && !extension.equals("JPG")) || previewImage==null) {
JOptionPane.showMessageDialog(this,
msg.getString("ImageFileError") + "\n'" +
currentImageFileName + "'",
msg.getString("ErrorDialogTitle"),
JOptionPane.ERROR_MESSAGE);
currentImageFileName = null;
okButton.setEnabled(false);
imageFileNameField.setText(msg.getString("NoImageLabel"));
}
else {
imageFileNameField.setText(currentImageFileName);
okButton.setEnabled(true);
this.repaint();
}
}
}
else if (actionCommand.equals("BackgroundOK")) {
this.setVisible(false);
}
else if (actionCommand.equals("BackgroundCancel")) {
currentColor = null;
currentImageFileName = null;
this.setVisible(false);
}
}
/*****************************************************************************/
/**
* Returns the background the user chose - either a
* <code>java.awt.Color</code> or a <code>java.awt.Image</code>,
* or <code>null</code> if they hit cancel.
*
* @return The background, as an <code>Object</code>.
*/
public Object getChosenBackground() {
if (viewingColors)
return currentColor;
else if (currentImageFileName != null)
return getImageFromFile(currentImageFileName);
return null;
}
/*****************************************************************************/
/**
* Returns the full path to the image the user chose, or <code>null</code>
* if the user selected a color or hit cancel.
*
* @return The full path to the image the user chose for the background,
* or <code>null</code>.
*/
public String getCurrentImageFileName() {
if (viewingColors)
return null;
return currentImageFileName; // Should be null if they hit cancel.
}
/*****************************************************************************/
/**
* Returns an image from a file in a safe fashion.
*
* @param fileName The file from which to get the image (must be .jpg or
* .bmp).
* @return The image contained in the file, or <code>null</code> if the
* image file was invalid.
*/
private Image getImageFromFile(String fileName) {
BufferedImage image = null;
try {
image = ImageIO.read(new URL("file:///" + fileName));
} catch (Exception e) { // IOException or MalformedURLException
JOptionPane.showMessageDialog(this,
msg.getString("ImageFileIOError") + "\nFile: " +
fileName + " -\n" + e,
msg.getString("ErrorDialogTitle"),
JOptionPane.ERROR_MESSAGE);
}
return image;
}
/*****************************************************************************/
/**
* Sets the currently-selected background in this dialog. This should be
* called just before calling <code>setVisible(true)</code>.
*
* @param defaultBackground Should be either a <code>java.awt.Color</code>
* instance or a <code>java.awt.Image</code> instance containing an
* image to use as the initial background choice.
* @param imageFileName Full path to the file containing
* <code>defaultBackground</code>, if it is an image. If it is a
* <code>java.awt.Color</code>, this parameter should be
* <code>null</code>.
*/
public void initializeData(Object defaultBackground, String imageFileName) {
if (defaultBackground instanceof Color) {
currentColor = (Color)defaultBackground;
currentImageFileName = null;
String colorString = currentColor.toString();
imageFileNameField.setText(colorString.substring(colorString.indexOf('[')));
colorButton.setSelected(true); // In case they selected "Image", then cancelled last time.
colorBrowseButton.setEnabled(true);
imageBrowseButton.setEnabled(false);
okButton.setEnabled(true); // In case they selected "Image", then cancelled last time.
viewingColors = true;
}
else if (defaultBackground instanceof Image) {
currentImageFileName = imageFileName;// We know that the image is currently displayed.
previewImage = new ImageIcon(currentImageFileName).getImage();
imageFileNameField.setText(currentImageFileName);
currentColor = Color.WHITE;
imageButton.setSelected(true); // In case they selected "Color", then cancelled last time.
colorBrowseButton.setEnabled(false);
imageBrowseButton.setEnabled(true);
viewingColors = false;
}
else {
JOptionPane.showMessageDialog(this,
msg.getString("BGBadObjectMessage") + "\n" + defaultBackground,
msg.getString("ErrorDialogTitle"),
JOptionPane.ERROR_MESSAGE);
currentImageFileName = null;
imageFileNameField.setText(msg.getString("NoImageLabel"));
currentColor = Color.WHITE;
colorBrowseButton.setEnabled(true);
imageBrowseButton.setEnabled(false);
viewingColors = true;
}
}
/*****************************************************************************/
/**
* Paints the dialog. This is overridden to properly paint the "preview"
* image or color.
*
* @param g The graphics context.
*/
public void paint(Graphics g) {
// Paint all of the regular dialog components.
super.paint(g);
// If they are looking at colors, paint the current color in a block.
if (viewingColors==true) {
Insets insets = getInsets();
Graphics newG = g.create();
newG.setColor(currentColor);
newG.fillRect(18+insets.left, 28+insets.top,
80+insets.left, 61+insets.top);
}
// Otherwise, if they are looking at images, paint the current image.
else {
if (currentImageFileName != null) {
Insets insets = getInsets();
g.drawImage(previewImage, 18+insets.left,28+insets.top,
80+insets.left,61+insets.top, null);
}
}
}
/*****************************************************************************/
/**
* Updates the UI used by this dialog. This is overridden to update the
* file chooser for images.
*/
public void updateUI() {
if (imageChooser != null)
SwingUtilities.updateComponentTreeUI(imageChooser);
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -