2016-07-19 5 views
1

BorderLayout을 사용하여 JFrame을 사용하는 Java Swing 응용 프로그램을 가지고 있으며 그 안에는 CardLayout을 사용하는 JPanel이 있습니다. 나는 3 개의 다른 카드를 보여주고있다. JFrame의 크기를 수동으로 설정하면 내용이 원하는대로 표시됩니다. 이미지가있는 라벨은 남동쪽 코너에 있습니다. enter image description here전체 화면의 스윙 레이아웃 경계 여백

하지만 전체 화면으로 설정하면, 많은 여백이 :

Frame[] frames = Frame.getFrames(); 
     JFrame frame = (JFrame) frames[0]; 
     frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     //frame.getContentPane().setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize()); 
     frame.setUndecorated(true); 
     //frame.setSize(600,500); 
     frame.setVisible(true); 
     frame.setLayout(new BorderLayout()); 

카드 빌드입니다 : 여기 enter image description here

내가 전체 화면으로 설정되는 코드입니다 Netbeans GUI 빌더 및 레이아웃 용으로 "Free Design"이 설정됩니다.

전체 화면으로 전체 응용 프로그램을 볼 수 있습니다. 크기가 조정 된 창 (이미지 예제 1)처럼 이미지가있는 레이블을 SE 코너로 사용하고 싶습니다. 이 레이아웃을 변경해야합니까 아니면 다른 것입니까?

+1

그래서 당신은 텍스트 사이의 여백을 삭제하거나 글꼴 크기를 증가 하시겠습니까? –

+0

첫 번째 이미지와 같이 아래쪽 모서리에있을 이미지가있는 라벨을 붙입니다. – Jure

+2

1) 더 나은 도움을 받으려면 [MCVE] 또는 [Short, Self Contained, Correct Example] (http://www.sscce.org/)를 게시하십시오. 2) 예를 들어 이미지를 얻는 한 가지 방법은 [이 Q & A] (http://stackoverflow.com/q/19209650/418556)에서 볼 수있는 이미지에 핫 링크하는 것입니다. 또 다른 방법은 런타임에 새로운'BufferedImage'를 만드는 것입니다. –

답변

2

이러한 UI에는 UI 전체에 작은 경계가 있습니다. 그것을 제거하려면 줄을 주석 :

ui.setBorder(new EmptyBorder(4,4,4,4)); 

enter image description hereenter image description here

import java.awt.*; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class ImageInSouthEast { 

    private JComponent ui = null; 

    ImageInSouthEast() { 
     initUI(); 
    } 

    public void initUI() { 
     if (ui!=null) return; 

     ui = new JPanel(new GridBagLayout()); 
     ui.setBorder(new EmptyBorder(4,4,4,4)); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.WEST; 
     gbc.gridwidth = 2; 
     gbc.weighty = .5; 
     gbc.weightx = .5; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 

     // first add the labels 
     for (int ii=1; ii<5; ii++) { 
      gbc.gridy = ii; 
      if (ii==4) { 
       gbc.gridwidth = 1; 
      } 
      JLabel l = new JLabel("Label " + ii); 
      l.setFont(l.getFont().deriveFont(50f)); 
      ui.add(l, gbc); 
     } 

     // now for the image! 
     BufferedImage bi = new BufferedImage(100, 50, BufferedImage.TYPE_INT_RGB); 
     JLabel l = new JLabel(new ImageIcon(bi)); 
     gbc.anchor = GridBagConstraints.LAST_LINE_END; 
     gbc.gridx = 2; 
     gbc.weighty = 0; 
     ui.add(l, gbc); 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception useDefault) { 
       } 
       ImageInSouthEast o = new ImageInSouthEast(); 

       JFrame f = new JFrame(o.getClass().getSimpleName()); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
+0

그것은 작동합니다. 당신과 @nick zoum 솔루션의 차이점은 무엇입니까? – Jure

+0

코드 스 니펫을 읽지 않으므로 말할 수 없습니다. 그러나 내가 지적 할 수있는 한 가지 차이점 - 나의 대답은 스크린 샷입니다. ;) –

0

텍스트 사이의 간격을 제거하려면 을 사용하면됩니다.

add(new JLabel(text), 5); 

이 일을하여 이미지를 추가합니다 :

Container pane = frame.getContentPane(); 
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); 
pane.add(Box.createHorizontalGlue()); 

public void add(Component comp, int gap){ 
    //comp is the component that will be added 
    //gap is the extra space after the last component and this 
    pane.remove(pane.getComponents().length - 1); 
    pane.add(Box.createVerticalStrut(gap)); 
    pane.add(comp); 
    //Obviously pane or frame need to be visible to use this method 
} 

이 작업을 수행하여 텍스트를 추가 요소를 추가 :

이 작업을 수행하여 레이아웃을 설정

JPanel panel = new JPanel(); 
panel.add(image, BorderLayout.EAST); 
panel.setOpaque(false); 
add(Box.createHorizontalGlue(),0); 
add(panel,0); 
+0

텍스트 사이의 간격에 대해서는별로 신경 쓰지 않습니다. 나는 첫 번째 이미지와 같이 오른쪽 아래 모서리에 이미지 (로고) 레이블을 넣고 싶습니다. – Jure

+0

@ Jure 그냥 내 지시를 따르십시오, 그것은 모두 작동 –