java - 如何将按钮放在屏幕上我想要的位置。

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

}

最佳答案

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

要组织组件以获得强大的 GUI,请使用布局管理器(或它们的组合)以及用于空白的布局填充和边框。

关于java - 如何将按钮放在屏幕上我想要的位置。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17029108/

相关文章:

html - 水平布局

java - 是否可以使用java中的FileNameFilter接口(interface)按大小过滤文件

java - 安卓相机: call Preview callback on Button click

java - JPanel 中的 JLabels,在运行时新标签包含以前的标签

Swing 应用程序的 Java 运行时问题

html - 不同的行为流体布局

android - 如何通过选项卡传递数据?

java - 如何将焦点设置在 SWT MessageDialog 或 MessageBox 中的文本而不是按钮上?

java - Spring MVC 自定义转换报错信息

Java 时钟在 Swing 中不计数