2016-09-13 8 views
-1

수정 사항은 아래를 참조하십시오.JFrame을 사용하여 Java Button의 위치를 ​​설정하려고하지 않습니다.

내 문제를 해결하기 위해 수많은 "솔루션"을 살펴 보았지만 문제가 해결되지 않는 것 같습니다.

enter image description here

는 기본적으로,이 버튼의 위치를 ​​설정하려면,하지만 난 그렇게 할 관리 할 수 ​​없습니다 :

이 내 응용 프로그램은 아래의 코드와 같은 모습입니다. 여기 내 코드입니다 :

package me.cervinakuy.application; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class ControlPanel3 extends JFrame { 

    JPanel panel = new JPanel(); 

    JButton startRobo = new JButton(); 
    JButton stopRobo = new JButton(); 
    JButton restartRobo = new JButton(); 

    public ControlPanel3() { 

//  setLayout(null); 
     setSize(1000, 700); 
     setResizable(false); 
     setLocation(450, 150); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     getContentPane().setBackground(new Color(45, 48, 55)); 
     setTitle("Espin Software | Control Panel"); 
     setVisible(true); 

     startRobo.setIcon(new ImageIcon(getClass().getResource("/resources/startRobo.png"))); 
     stopRobo.setIcon(new ImageIcon(getClass().getResource("/resources/stopRobo.png"))); 
     restartRobo.setIcon(new ImageIcon(getClass().getResource("/resources/restartRobo.png"))); 

     startRobo.setBorder(null); 
     stopRobo.setBorder(null); 
     restartRobo.setBorder(null); 

     startRobo.setLocation(100, 100); 

     panel.add(startRobo); 
     panel.add(stopRobo); 
     panel.add(restartRobo); 
     panel.setOpaque(false); 

     add(panel); 

     validate(); 

    } 

} 

편집 : 지금은 처음 찾던의 GUI를 만들고 관리해야 그러나, 나는 새로운 문제가있다. 버튼은 이제 이미지가 아닌 GUI의 다른 부분에서 누를 수 있습니다. 관심있는 사람들을 위해, 여기에 내가 달성 할 수 있었던 것입니다 :

New GUI look.

업데이트 코드 :

package me.cervinakuy.application; 

import java.awt.Color; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class ControlPanel3 extends JFrame { 

    JPanel panel = new JPanel(); 

    JButton startRobo = new JButton(); 
    JButton stopRobo = new JButton(); 
    JButton restartRobo = new JButton(); 

    public ControlPanel3() { 

//  setLayout(null); 
     setSize(1000, 700); 
     setResizable(false); 
     setLocation(450, 150); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     getContentPane().setBackground(new Color(45, 48, 55)); 
     setTitle("Espin Software | Control Panel"); 
     setVisible(true); 

     startRobo.setIcon(new ImageIcon(getClass().getResource("/resources/startRobo.png"))); 
     stopRobo.setIcon(new ImageIcon(getClass().getResource("/resources/stopRobo.png"))); 
     restartRobo.setIcon(new ImageIcon(getClass().getResource("/resources/restartRobo.png"))); 

     startRobo.setBorder(null); 
     stopRobo.setBorder(null); 
     restartRobo.setBorder(null); 

     panel.setLayout(null); 
     startRobo.setLocation(200, 200); 
     startRobo.setBounds(5, -95, 300, 300); 
     stopRobo.setBounds(5, 0, 300, 300); 
     restartRobo.setBounds(5, 95, 300, 300); 

     panel.add(startRobo); 
     panel.add(stopRobo); 
     panel.add(restartRobo); 
     panel.setOpaque(false); 

     add(panel); 

     validate(); 

    } 

} 
+3

* 그들이) 도움이 실패한 이유 * 상위 5 링크 설명 "내 문제를 해결하는"솔루션 "나는 수많은 찾고했습니다", & b)는 당신을 설득한다는 데 실패 명시 적 위치를 설정 의 구성 요소는 역효과를 냈습니다. –

+1

1) ASCII 아트 또는 최소 크기에서 GUI의 * 의도 된 * 레이아웃의 간단한 그림을 제공하고 크기를 조정할 수 있으면 더 많은 너비와 높이를 제공하십시오. 2) 더 나은 도움을 받으려면 [MCVE] 또는 [Short, Self Contained, Correct Example] (http://www.sscce.org/)를 게시하십시오. 3) 예를 들어 이미지를 얻는 한 가지 방법은 [이 Q & A] (http://stackoverflow.com/q/19209650/418556)에서 볼 수있는 이미지에 핫 링크하는 것입니다. –

+0

나는 내 질문에 제목을 붙였을 때 처음 5 개를 이미 시도했지만, 처음 몇 개는 내가 성취하려고하는 것과 관련이 없으며, 나머지는 단순히 내 응용 프로그램 화면을 비워 두었습니다. 수많은 솔루션을 살펴 보았습니다. 의도 한 레이아웃은 매우 간단하며 왼쪽에 정렬됩니다. 그러나, 무엇인가의 이유로 그들은 모두 중앙에 위치하여 맨 위에 놓습니다. 나는 그들이 더 이상 존재하지 않게 위치를 설정하는 방법을 알아 내려고 노력하고있다. 따라서이 질문의 목적. – Espin

답변

3

enter image description here

이 레이아웃 방법 일반적으로 수 있습니다 동일한 효과로 끝나는 구성 요소. 이 예에서는 열 (buttonContainerGridLayout을 사용)의 버튼을 포함하는 패널을 사용하고 그 컨테이너를 상단 (buttonConstrainPanel을 사용하여 BorderLayout을 사용)으로 제한 한 다음 해당 컨테이너를 왼쪽에 넣을 패널을 사용합니다 (ui with BorderLayout).

하나의 GridBagLayout 또는 GroupLayout을 사용하여 달성 할 수도 있지만, 달성의 논리는 간단하지 않을 수 있습니다.

파란색 버튼에 표시된 포커스 경계는 마우스 클릭이 버튼을 활성화하는 위치의 한계를 나타냅니다.

import java.awt.*; 
import java.net.MalformedURLException; 
import java.net.URL; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class ThreeButtonAlignedLeft { 

    private JComponent ui = null; 
    private String prefix = "http://i.stack.imgur.com/"; 
    private String[] suffix = {"gJmeJ.png","T5uTa.png","wCF8S.png"}; 

    ThreeButtonAlignedLeft() { 
     try { 
      initUI(); 
     } catch (MalformedURLException ex) { 
      ex.printStackTrace(); 
     } 
    } 

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

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

     JPanel buttonContainer = new JPanel(new GridLayout(0, 1, 5, 5)); 
     for (int ii=0; ii<suffix.length; ii++) { 
      JButton b = new JButton(new ImageIcon(new URL(prefix + suffix[ii]))); 
      b.setBorderPainted(false); 
      b.setMargin(new Insets(0,0,0,0)); 
      b.setContentAreaFilled(false); 
      buttonContainer.add(b); 
     } 
     JPanel buttonConstrainPanel = new JPanel(new BorderLayout(0, 0)); 
     buttonConstrainPanel.add(buttonContainer, BorderLayout.PAGE_START); 

     ui.add(buttonConstrainPanel, BorderLayout.LINE_START); 
    } 

    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) { 
       } 
       ThreeButtonAlignedLeft o = new ThreeButtonAlignedLeft(); 

       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); 
    } 
} 
+1

놀랍습니다 -'null' 레이아웃은 실제로 작동합니다! –