gpt4 book ai didi

java - 全屏 JFrame 之上的模态 JFrame

转载 作者:行者123 更新时间:2023-12-02 00:05:12 24 4
gpt4 key购买 nike

package javaapplication1;

import java.awt.Color;
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.JFrame;
import javax.swing.JPanel;

public class JavaApplication1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final JFrame frame = new JFrame();
final NewJPanel p = new NewJPanel();
frame.setTitle("Frame");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final 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);

btn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JFrame f = new JFrame();
JPanel p = new JPanel();
f.setSize(300, 300);
p.setBackground(Color.red);

f.add(p);
f.setLocationRelativeTo(null);
f.setAlwaysOnTop(true);
f.setVisible(true);
}
});
}
}

我想要 f.setVisible(true); 在全屏 JFrame 中弹出,当我单击按钮时,该 JFrame 设置为模态。我怎样才能做到这一点?因为在该代码中,当我单击按钮时,f.setVisible(true); 显示在全屏 JFrame 之外。期待您的答复。

最佳答案

所以你想要在你的主JFrame里面有类似JInternalFrame的东西:

import java.awt.BorderLayout;
import java.awt.Color;
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.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JavaApplication1 {

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame();
final JDesktopPane desktopPane = new JDesktopPane();
frame.setTitle("Frame");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final 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, BorderLayout.NORTH);
frame.add(desktopPane, BorderLayout.CENTER);

btn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JInternalFrame f = new JInternalFrame();
JPanel p = new JPanel();
p.setBackground(Color.red);
f.setSize(300, 300);
f.setResizable(true);
f.add(p);
f.setVisible(true);
desktopPane.add(f);
}
});
}
});

}
}

enter image description here

有关 JInternalFrame 的更多信息,您可以找到 here

关于java - 全屏 JFrame 之上的模态 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14033033/

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