对于我的开发,我使用3个监视器。当我打开我的Java应用程序并将其拖到另一个屏幕并按下按钮以显示FileChooser时。文件选择器出现在主监视器上,由Windows设置。这有什么不对?
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(<parent Comp>);
我应该怎么做才能使它(File Chooser)出现在我的Java应用程序之上?
答案 0 :(得分:0)
看来你的父母用不正确的方式
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class MainWindow extends JFrame
{
private static final long serialVersionUID = 1708404088747322174L;
private JPanel contentPane;
/**
* Create the frame.
*/
public MainWindow()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
final JFileChooser fileChooser = new JFileChooser();
final JButton button = new JButton("Test");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(final ActionEvent arg0)
{
fileChooser.showOpenDialog(button.getParent());
}
});
contentPane.add(button);
}
/**
* Launch the application.
*/
public static void main(final String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
final MainWindow frame = new MainWindow();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
在此示例中,JFileChooser位于父组件
之上