2016-10-03 8 views
1

GUI 디자인에서 속도를 높이는 데 도움이되는 특정 방법을 만들고 싶습니다. 나는 setBounds을 사용하는 데 가장 오래 걸립니다. 이제 FlowLayout 또는 GridLayout을 사용 하겠지만, 나는 그것들에 의지하는 것을 좋아하지 않습니다.JComponents의 setBounds 메소드를 수정하려면 어떻게해야합니까?

기본적으로 나는 다른 JComponent 위에 JComponent를 배치하는 placeAbove과 같은 메소드를 생각하고 있습니다. 이것의 인수는, 기준점 JComponent와 서로의 거리를 나타내는 정수가됩니다. 저는 현재 다음과 성공을 데 : 그 세 번째 인수는 비효율적 인 느낌 때문에

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class BoundBender extends JFrame { 
    public BoundBender() { 
     Container c = getContentPane(); 
     c.setLayout(null); 

     JLabel l1 = new JLabel("Reference Point"); 
     JLabel l2 = new JLabel("Above Label"); 
     JLabel l3 = new JLabel("Below Label"); 
     JLabel l4 = new JLabel("Before Label"); 
     JLabel l5 = new JLabel("After Label"); 

     c.add(l1); 
     l1.setBounds(170, 170, 100, 20); 
     c.add(l2); 
     placeAbove(l1, 0, l2); 
     c.add(l3); 
     placeBelow(l1, 10, l3); 
     c.add(l4); 
     placeBefore(l1, 20, l4); 
     c.add(l5); 
     placeAfter(l1, 30, l5); 

     setVisible(true); 
     setSize(500, 500); 
    } 
    public static void main (String args[]) { 
     BoundBender bb = new BoundBender(); 
     bb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
    public static void placeAbove(JComponent j, int a, JComponent k) { 
     int x= j.getX(); 
     int y= j.getY(); 
     int w= j.getWidth(); 
     int h= j.getHeight(); 

     y=(y-h)-a; 

     k.setBounds(x, y, w, h); 
    } 
    public static void placeBelow(JComponent j, int a, JComponent k) { 
     int x= j.getX(); 
     int y= j.getY(); 
     int w= j.getWidth(); 
     int h= j.getHeight(); 

     y=y+h+a; 

     k.setBounds(x, y, w, h); 
    } 
    public static void placeBefore(JComponent j, int a, JComponent k) { 
     int x= j.getX(); 
     int y= j.getY(); 
     int w= j.getWidth(); 
     int h= j.getHeight(); 

     x=(x-w)-a; 

     k.setBounds(x, y, w, h); 
    } 
    public static void placeAfter(JComponent j, int a, JComponent k) { 
     int x= j.getX(); 
     int y= j.getY(); 
     int w= j.getWidth(); 
     int h= j.getHeight(); 

     x=x+w+a; 

     k.setBounds(x, y, w, h); 
    } 
} 

는 그러나, 나는, l2.placeAbove(l1, 0) 등이 간단하게 할 수 있습니다. 그래서 어떤 제안이라도? 이해할 수있는 용어를 사용하십시오.

+0

Java Swing은 [레이아웃 관리자] (https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)와 함께 사용하도록 설계되었습니다. [Spring layout manager] (https://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html)를보고 원하는 것을 사용하십시오. –

답변

0

그런 다음 반환 값을 사용하십시오. void이 아닌 Rectangle의 인스턴스를 반환하십시오. 당신이 JComponent의에서 java.awt.Component에서 상속 setBounds(Rectangle r) 사용

k.setBounds(placeAbove(j, a)); 

그 방법 :

public static Rectangle placeAbove(JComponent j, int a) { 
    int x= j.getX(); 
    int y= j.getY(); 
    int w= j.getWidth(); 
    int h= j.getHeight(); 

    y=(y-h)-a; 

    //return our new bounds projected in a Rectangle Object 
    return new Rectangle(x, y, w, h); 
} 

그리고 유스 케이스는 접경 사각형을 설정하는 것입니다 : 그것은 같이 보일 것입니다.

희망이 도움이됩니다.

+1

나는 더 이상 setBounds를 사용할 필요가 없다고 생각했다. ... 덕분에 많은 도움을 받았다. 왜냐하면 이것이 Rectangle 클래스를 더 잘 이해할 수 있었기 때문이다. :) – ArcIX