如何把按钮放在我想要的屏幕上。

时间:2013-06-10 16:59:40

标签: java swing layout location jbutton

好的我想将我的按钮放在屏幕上的某个位置。有没有办法将它放在一个确切的像素位置?现在它把它放在我屏幕的最右边。我有一个图像,我希望它也可以过去。

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();
        }
        }
}

}

3 个答案:

答案 0 :(得分:3)

Java GUI可能必须在许多平台上工作,在不同的屏幕分辨率和使用不同的PLAF。因此,它们不利于组件的精确放置。

要组织强大的GUI组件,而是使用布局管理器(或它们的组合),以及布局填充和放大器。白色空间的边界。

答案 1 :(得分:3)

实现你所描述的东西的另一种方法可以通过这种方式完成(有很多选择,但最终,这一切都取决于你希望你的组件如何相互定位,以及当大小相同时会发生什么父容器更改):

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();
                }
            }
        });
    }

}

答案 2 :(得分: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