2013-01-15 25 views
1

나는 두 개의 모니터 시스템에서 작동하도록 설계된 프로그램을 작성 중이다. 나는 JFrame 오브젝트를 분리해야하고, 디폴트로 설정해야합니다. 첫 번째 프레임 인스턴스가 열립니다. 그런 다음 사용자는 해당 프레임을 특정 모니터로 끌어다 놓거나 제자리에 두어야합니다. 그들이 그 프레임의 버튼을 클릭하면, 반대쪽 모니터에서 두 번째 프레임을 여는 프로그램이 필요합니다.JFrame을 사용하여 다른 모니터에서 다른 JFrame 창을 여는 방법은 무엇입니까?

그래서 프레임 객체가 어떤 모니터에 있는지 알아 낸 다음 반대 프레임에서 다른 프레임 객체를 열려면 어떻게해야합니까?

+1

1) 앱이란 무엇입니까? 구체적으로? [여러 개의 JFrames 사용, 좋고 나쁜 관행?] (http://stackoverflow.com/a/9554657/418556) 2) ['GraphicsEnvironment'] (http://docs.oracle.com/ javase/7/docs/api/java/awt/GraphicsEnvironment.html) 및 ['GraphicsDevice'] (http://docs.oracle.com/javase/7/docs/api/java/awt/GraphicsDevice.html) 개체 그것은 드러낸다. @mKorbel 팁에 따라 수정되었습니다. +1 –

답변

3

GraphicsEnvironment를 검색하면 각 화면의 경계와 위치를 쉽게 찾을 수 있습니다. 그 후에, 그것은 프레임의 위치를 ​​가지고 노는 것에 불과합니다. 그들은 매우 흥미로운 고려 사항을 가지고 있기 때문에 신중하게 The Use of Multiple JFrames, Good/Bad Practice?을 읽는 고려, 그러나

import java.awt.Frame; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class TestMultipleScreens { 

    private int count = 1; 

    protected void initUI() { 
     Point p = null; 
     for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) { 
      p = gd.getDefaultConfiguration().getBounds().getLocation(); 
      break; 
     } 
     createFrameAtLocation(p); 
    } 

    private void createFrameAtLocation(Point p) { 
     final JFrame frame = new JFrame(); 
     frame.setTitle("Frame-" + count++); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     final JButton button = new JButton("Click me to open new frame on another screen (if you have two screens!)"); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       GraphicsDevice device = button.getGraphicsConfiguration().getDevice(); 
       Point p = null; 
       for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) { 
        if (!device.equals(gd)) { 
         p = gd.getDefaultConfiguration().getBounds().getLocation(); 
         break; 
        } 
       } 
       createFrameAtLocation(p); 
      } 
     }); 
     frame.add(button); 
     frame.setLocation(p); 
     frame.pack(); // Sets the size of the unmaximized window 
     frame.setExtendedState(Frame.MAXIMIZED_BOTH); // switch to maximized window 
     frame.setVisible(true); 
    } 

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

      @Override 
      public void run() { 
       new TestMultipleScreens().initUI(); 
      } 
     }); 
    } 

} 

:

여기에 작은 데모 예제 코드를 참조하십시오.

+0

감사합니다. 필자가 쓰고있는 응용 프로그램의 경우 '프레젠테이션'용으로 설계되었으므로 여러 프레임을 가져야하므로 실제로 다른 작업을 수행 할 수는 없습니다. –

관련 문제