两个JFrames并排

时间:2013-01-29 17:56:44

标签: java swing jframe awt

是否可以设置两个不同的JFrame并将它们并排显示?不使用内部框架,多个Jpanel等。

2 个答案:

答案 0 :(得分: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();
    }
});
  }

}

答案 1 :(得分: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