不管主jframe 在哪里,总之在JFileChooser的showDialog之前新声明一个jframe, 不要设置newframe为visible,然后showDialog(newframe)JFrame j = new JFrame(); j.setLocation(frame.getLocation()); j.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/my1.png")); int result = dialog.showOpenDialog(j);这样弹出的对话框的位置可能不会与主jframe位置同步...
public class Test extends JFileChooser { public Test() { } protected JDialog createDialog(Component parent) throws HeadlessException { JDialog dialog = super.createDialog(parent); dialog.setLocation(500, 200); dialog.setVisible(true); return dialog; } public static void main(String[] args) throws IOException { Test test = new Test(); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setIconImage(ImageIO.read(new File("D:\\212217668.gif"))); test.createDialog(frame); } }
loginjframe.setIconImage(image);但是我有个写入文件的操作按钮
点完以后是就 调用 JFileChooser 它了
但是它的图标还是java 的
在loginjframe布什模态的情况下,只要./res/Logo.jpg设置上去了,下面的方法显示的JFileChooser是一定新图标的。
fc.showOpenDialog(loginjframe);
chooser.showSaveDialog(loginjframe);
在A 中 我调用了JFileChooser 方法是
JFileChooser c = new LicenseFileChooser();我想你说的父窗体 这时候已经不在了吧
不知道我说的对不对
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;public class ShowImage extends JPanel implements ActionListener { private static final long serialVersionUID = 1L;
static JFrame frame = null; private JButton open;
private String filePath;
private BufferedImage image;
private static ShowImage show; public static ShowImage singleton() {
if (show == null)
show = new ShowImage();
return show;
} private ShowImage() {
open = new JButton("open");
setLayout(new BorderLayout());
add(open, BorderLayout.SOUTH);
open.addActionListener(this); } private void init(String str) {
FileInputStream input;
try {
input = new FileInputStream(str);
image = ImageIO.read(input);
repaint();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } private static void createAndShowGUI() {
// Create and set up the window.
frame = new JFrame("JFileChooserDemo");
//换成自己的图标路径
frame.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/工作/My Documents/My Pictures/logo.png"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add content to the window.
frame.add(singleton());
frame.setSize(300, 400);
frame.setTitle("QRCode"); // Display the window.
frame.setVisible(true);
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
} @Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(image, 26, 26, this);
} @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == open) {
JFileChooser dialog = new JFileChooser();
int result = dialog.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = dialog.getSelectedFile();
filePath = file.getPath();
init(filePath);
}
}
}
}
在main函数所在的class,假设为App,利用单例模式,让外部可以获取loginjframe。
private static App instance = null;
private static JFrame loginjframe;
public class App
{
public static App getApplication()
{
if (instance == null)
{
instance = new App();
}
return instance;
} public JFrame getMainFrame()
{
return loginjframe;
}
public static void main(String[] args)
{
loginjframe = new JFrame();
...
}
}外部获取父窗体:App.getApplication().getMainFrame();
给JDialog、JWindow设置父窗体,对于其图标、相对位置都是非常好的。
但是经过这么多了
我只想问问有没有什么方法直接给JFileChooser 的图标换成自己的呢?
不要设置newframe为visible,然后showDialog(newframe)JFrame j = new JFrame();
j.setLocation(frame.getLocation());
j.setIconImage(Toolkit.getDefaultToolkit().createImage("E:/my1.png"));
int result = dialog.showOpenDialog(j);这样弹出的对话框的位置可能不会与主jframe位置同步...
} protected JDialog createDialog(Component parent) throws HeadlessException {
JDialog dialog = super.createDialog(parent);
dialog.setLocation(500, 200);
dialog.setVisible(true);
return dialog;
} public static void main(String[] args) throws IOException {
Test test = new Test();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(ImageIO.read(new File("D:\\212217668.gif")));
test.createDialog(frame);
}
}