是否可以设置两个不同的 JFrame 并并排显示它们?不使用 Internalframe、多个 Jpanels 等。
问问题
3426 次
3 回答
0
首先将您的框架放置在每个屏幕设备上。
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();
}
});
}
}
于 2013-01-29T18:21:22.183 回答
0
是的,如下;
JFrame leftFrame = new JFrame();
// get the top left point of the left frame
Point leftFrameLocation = leftFrame.getLocation();
// then make a new point with the same top (y) and add the width of the frame (x)
Point rightFrameLocation = new Point(
leftFrameLocation.x + leftFrame.getWidth(),
leftFrameLocation.y);
JFrame rightFrame = new JFrame();
rightFrame.setLocation(rightFrameLocation); // and that's the new location
于 2021-03-03T15:11:27.390 回答
0
是的,这是一个非常简单的例子;对于我自己的用途,我对非模态 JDialogs 做同样的事情。
public static void main(String[] args) {
JFrame frameL = new JFrame("Left Frame");
frameL.setSize(400, 200); // this size arbitrary; set how you need.
frameL.setLocationRelativeTo(null);
frameL.setVisible(true);
Point p = frameL.getLocationOnScreen();
Dimension d = frameL.getSize();
JFrame frameR = new JFrame("Right Frame");
frameR.setSize(300, 160); // this size arbitrary; set how you need.
frameR.setLocation((p.x + d.width), p.y);
frameR.setVisible(true);
while(frameL.isVisible()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.exit(0);
}
于 2021-12-17T06:01:48.843 回答