gpt4 book ai didi

java - 两个并排的 JFrame

转载 作者:行者123 更新时间:2023-12-01 04:53:19 25 4
gpt4 key购买 nike

是否可以设置两个不同的 JFrame 并并排显示它们?不使用Internalframe、多个Jpanels等。

最佳答案

首先将您的框架放置在每个屏幕设备上。

frame1.setLocation(pointOnFirstScreen);
frame2.setLocation(pointOnSecondScreen);

工作示例:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class GuiApp1 {
protected void twoscreen() {
Point p1 = null;
Point p2 = null;
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment ().getScreenDevices()) {
if (p1 == null) {
p1 = gd.getDefaultConfiguration().getBounds().getLocation();
} else if (p2 == null) {
p2 = gd.getDefaultConfiguration().getBounds().getLocation();
}
}
if (p2 == null) {
p2 = p1;
}
createFrameAtLocation(p1);
createFrameAtLocation(p2);
}

private void createFrameAtLocation(Point p) {
final JFrame frame = new JFrame();
frame.setTitle("Test frame on two screens");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
final JTextArea textareaA = new JTextArea(24, 80);
textareaA.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
panel.add(textareaA, BorderLayout.CENTER);
frame.setLocation(p);
frame.add(panel);
frame.pack();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {


public void run() {
new GuiApp1().twoscreen();
}
});
}

}

关于java - 两个并排的 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14589181/

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