gpt4 book ai didi

java - JFileChooser 显示在全屏 JFrame 之外

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:15 27 4
gpt4 key购买 nike

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
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;

public class Sample {
public static String audioName;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
frame.setVisible(true);

JButton btn = new JButton();
btn.setText("Button");
JPanel panel = new JPanel();

panel.add(btn);
frame.add(panel);
btn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int returnName = chooser.showOpenDialog(frame);
if (returnName == JFileChooser.APPROVE_OPTION) {
System.out.println("Sample");
}
}
});
}
}

如何在我的全屏中显示 JFileChooser?我不熟悉 JInternalFrame/JDesktopPane,您认为这会解决这个问题还是有其他方法可以解决这个问题?

最佳答案

JFileChooser 在装有 Java 6 的 Windows XP 计算机上位于框架的中心。我将框架移动到两个显示器上的不同位置。

我注释掉了更改显示设置的行,并修复了一些其他问题。

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.SwingUtilities;

public class Sample implements Runnable {
public static String audioName;

public void run() {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// GraphicsDevice device = GraphicsEnvironment
// .getLocalGraphicsEnvironment().getDefaultScreenDevice();
// device.setFullScreenWindow(frame);
// device.setDisplayMode(new DisplayMode(800, 600, 32, 60));

JButton btn = new JButton();
btn.setText("Button");
JPanel panel = new JPanel();

panel.add(btn);
frame.add(panel);
frame.setExtendedState(
frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
btn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int returnName = chooser.showOpenDialog(frame);
if (returnName == JFileChooser.APPROVE_OPTION) {
System.out.println("Sample");
}
}
});
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Sample());
}
}

如果您想最大化您的 JFrame,请在您的 setVisible 方法之前的某处添加以下语句。

frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);

关于java - JFileChooser 显示在全屏 JFrame 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14044298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com