简体   繁体   English

尝试使用JFrame设置Java Button的位置不起作用?

[英]Trying to set the location of a Java Button using JFrame isn't working?

Please look below for Edits. 请在下面查看修改。

So I've looking over numerous "solutions" to fix my problem, but I just can't seem to get it working. 因此,我查看了许多“解决方案”来解决我的问题,但似乎无法正常工作。

This is what my application looks like with the code below: 这是我的应用程序的以下代码:

在此处输入图片说明

Basically, I want to set the location of a button, but I can't manage to do so. 基本上,我想设置按钮的位置,但是我无法这样做。 Here is my code: 这是我的代码:

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

    }

}

EDIT: I have now managed to create a GUI of what I was initially looking for, however, I have a new problem. 编辑: 我现在已经设法创建了最初寻找的GUI,但是,我遇到了一个新问题。 Buttons are now pressable from different parts of the GUI, rather than only on the image. 现在可以从GUI的不同部分而不是仅在图像上按下按钮。 For those interested, here is what I have been able to accomplish: 对于那些感兴趣的人,这是我能够实现的目标:

New GUI look. 新的GUI外观。

Updated Code: 更新的代码:

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

    }

}

在此处输入图片说明

There are typically a number of ways to layout components that end with the same effect. 通常,有多种方法可以布局具有相同效果的组件。 In this example, we use a panel to contain the buttons in a column ( buttonContainer using a GridLayout ) then a panel to restrict that container to the top ( buttonConstrainPanel using a BorderLayout ) then a container to put that panel on the left ( ui with BorderLayout ). 在此示例中,我们使用面板将按钮包含在列中(使用GridLayout buttonContainer ),然后使用面板将容器限制在顶部(使用BorderLayout buttonConstrainPanel ),然后使用将面板放置在左侧的容器( uiBorderLayout )。

It could also be achieved using a single GridBagLayout or a GroupLayout , though the logic of achieving it might not be as simple. 也可以使用单个GridBagLayoutGroupLayout来实现,尽管实现它的逻辑可能并不那么简单。

The focus border seen on the blue button indicates the limits of where a mouse click would activate the button. 蓝色按钮上显示的焦点边框表示鼠标单击将激活该按钮的限制。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM