2013-06-10 3 views
0

좋아, 내 단추를 화면의 특정 위치에 배치하고 싶습니다. 정확하게 픽셀 위치에 배치 할 수있는 방법이 있습니까? 지금은 내 화면의 맨 오른쪽에 배치합니다. 나는 그것이 너무 넘어 가기를 바라는 이미지를 가지고있다.화면에 원하는 곳에 버튼을 놓는 방법.

import java.awt.AWTException; 
import java.awt.FlowLayout; 
import java.awt.Rectangle; 
import java.awt.Robot; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.InputEvent; 
import java.awt.image.BufferedImage; 
import java.awt.image.DataBuffer; 
import java.awt.image.DataBufferInt; 
import java.awt.image.WritableRaster; 

import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 

public class UnfollowGUI extends JFrame{ 

private JLabel label; 
private JButton button; 

private ImageIcon bgi; 
private JLabel bgl; 

public static Rectangle gameSquare; 

public static boolean rock = true; 
public static boolean runningMine = true; 
public static int stray = 0; 
public static int strayCount = 0; 


public static void main(String[] args) { 
    UnfollowGUI gui = new UnfollowGUI(); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program 
    //gui.setSize(886, 266); 
    gui.pack(); 
    gui.setVisible(true); 
    gui.setTitle("Solid Cloud Inc - Twitter Unfolower"); 
} 

public UnfollowGUI(){ 

    setLayout(new FlowLayout()); 

    bgi = new ImageIcon(getClass().getResource("tu.png")); 
    bgl = new JLabel (bgi); 
    add(bgl); 

    ImageIcon start = new ImageIcon(getClass().getResource("start.png")); 
    button = new JButton (start); 
    button.setBorder(BorderFactory.createEmptyBorder()); 
    button.setContentAreaFilled(false); 

    add(button); 

    label = new JLabel (""); 
    add(label); 

    Events e = new Events(); 
    button.addActionListener(e); 
} 

public class Events implements ActionListener { 


    public void actionPerformed(ActionEvent e) { 

     if (e.getSource() == button) { 
      label.setText("Searching"); 
      try { 
      Unfollow(); 
     } catch (InterruptedException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     } 
} 

} 

답변

0

레이아웃 관리자를 사용하지 않고 원하는 장소에 물건을 배치 할 수 있습니다. 이것은 절대 위치라고, 당신은 여기에 튜토리얼을 읽을 수 있습니다 http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

을 여기에 튜토리얼 내부에서 코드 조각입니다 :

pane.setLayout(null); 

JButton b1 = new JButton("one"); 
JButton b2 = new JButton("two"); 
JButton b3 = new JButton("three"); 

pane.add(b1); 
pane.add(b2); 
pane.add(b3); 

Insets insets = pane.getInsets(); 
Dimension size = b1.getPreferredSize(); 
b1.setBounds(25 + insets.left, 5 + insets.top, 
      size.width, size.height); 
size = b2.getPreferredSize(); 
b2.setBounds(55 + insets.left, 40 + insets.top, 
      size.width, size.height); 
size = b3.getPreferredSize(); 
b3.setBounds(150 + insets.left, 15 + insets.top, 
      size.width + 50, size.height + 20); 

...//In the main method: 
Insets insets = frame.getInsets(); 
frame.setSize(300 + insets.left + insets.right, 
       125 + insets.top + insets.bottom); 

같이 찾고 끝 :

enter image description here

+0

그러나 나는 다른 방법을 모르는 – nachokk

+0

@nachokk는에 버튼을 넣어 원하는 정확한 픽셀. –

+0

나도 모르지만 조심해야 할 것은 많은 마법의 숫자들이다. GUI 편집기가 없다면 유용하지 않다. :) – nachokk

3

Java GUI는 다양한 PLAF를 사용하는 다양한 화면 해상도 &에서 여러 플랫폼에서 작동해야 할 수 있습니다. 따라서 구성 요소의 정확한 배치에 도움이되지 않습니다.

견고한 GUI를 위해 구성 요소를 구성하려면 레이아웃 관리자 (또는 그 조합)와 공백 테두리 레이아웃 &을 사용하십시오.

3

설명하는 방법을 달성하는 또 다른 방법은 이렇게 할 수 있습니다 (많은 대안이 있지만 결국에는 구성 요소를 서로 상대적으로 배치하려는 방법과 크기) 부모 컨테이너 변경 : mattise이 레이아웃 매니저를 : 사용하지 않는 매우 실망처럼 u는 GUI 편집기를 사용하지 않는 경우

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.FlowLayout; 
import java.net.MalformedURLException; 
import java.net.URL; 

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

public class TestLoginGridBagLayout { 

    protected void initUI() throws MalformedURLException { 
     JFrame frame = new JFrame("Background image"); 
     frame.getContentPane().setBackground(Color.BLACK); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JLabel background = new JLabel(new ImageIcon(new URL("http://images2.layoutsparks.com/1/161318/city-lights-bridge-decoration.jpg"))); 
     background.setVerticalAlignment(JLabel.BOTTOM); 
     background.setHorizontalAlignment(JLabel.LEFT); 
     background.setLayout(new BorderLayout()); 
     frame.add(background); 
     JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); 
     buttonPanel.setOpaque(false); 
     buttonPanel.add(new JButton("Some button in the bottom left")); 
     background.add(buttonPanel, BorderLayout.SOUTH); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        new TestLoginGridBagLayout().initUI(); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

} 
관련 문제