Два JFrame рядом

Можно ли настроить два разных JFrames и показать их бок о бок? Без использования Internalframe, нескольких Jpanels и т.д.


person Michal    schedule 29.01.2013    source источник
comment
Взгляните на JFrame — метод setLocation, который в сочетании с установка размера кадров, можно расположить оба рядом   -  person makciook    schedule 29.01.2013
comment
Просто для уточнения: вы имеете в виду два окна приложения рядом или два раздела (в одном окне приложения) рядом?   -  person thatidiotguy    schedule 29.01.2013
comment
Не все возможное разумно. Похоже, вам нужен один JFrame с двумя JPanels рядом.   -  person Gilbert Le Blanc    schedule 29.01.2013
comment
@makciook Да, это можно сделать, как вы сказали. Но есть ли другая возможность?   -  person Michal    schedule 29.01.2013
comment
@thatidiotguy я имею в виду 2 jframe   -  person Michal    schedule 29.01.2013
comment
@GilbertLeBlanc Я думаю об этом, но сейчас меня интересует другой метод   -  person Michal    schedule 29.01.2013
comment
Я бы действительно рассмотрел совет @GilbertLeBlanc, особенно учитывая ответы, представленные в Использование нескольких JFrames, хорошая/плохая практика?.   -  person Guillaume Polet    schedule 30.01.2013


Ответы (2)


1-е место ваших кадров на каждом экране устройств.

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();
    }
});
  }

}
person joey rohan    schedule 29.01.2013

Да, следующим образом;

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
person Peter Wanden    schedule 03.03.2021