Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将按钮移动到准确的位置?_Java_Swing_Layout_Null Layout Manager - Fatal编程技术网

Java 如何将按钮移动到准确的位置?

Java 如何将按钮移动到准确的位置?,java,swing,layout,null-layout-manager,Java,Swing,Layout,Null Layout Manager,我希望按钮和文本字段在标记为红色的空间中与它们所在的布局相同。看这张图片来理解我的意思 更新:按钮现在已就位,但图像不会出现在第二个面板上 我怎样才能把它们搬到那里去?这是到目前为止我的密码 package gasindicator; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.swing.*; import javax.swing.border.*

我希望按钮和文本字段在标记为红色的空间中与它们所在的布局相同。看这张图片来理解我的意思

更新:按钮现在已就位,但图像不会出现在第二个面板上

我怎样才能把它们搬到那里去?这是到目前为止我的密码

package gasindicator;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;
import java.io.*;
import javax.imageio.*;
import java.net.*;

public class GasIndicator extends JPanel
 {
private Image image;


GasIndicator()
{
    try
    {
        image = ImageIO.read(new URL("http://i68.tinypic.com/2ceja8i.png"));

    }
    catch (IOException ioe)
    {
        System.out.println("Unable to fetch image.");
        ioe.printStackTrace();
    }

    setLayout( new BorderLayout() );

    JLabel background = new JLabel( new ImageIcon(image) );
    background.setLayout( new FlowLayout(FlowLayout.LEFT) );
    add( background );

    JPanel buttonPanel = new JPanel( new GridLayout(0, 3, 6, 5) );
    buttonPanel.setBorder( new EmptyBorder(338, 233, 0, 0) );

    buttonPanel.setOpaque( false );


    //for (int i = 0; i < 7; i++)
    {
        JButton button = new JButton("Button");
        JButton button1 = new JButton("Button");
        JButton button2 = new JButton("Button");
        JButton button3 = new JButton("Button");
        JButton button4 = new JButton("Button");
        JButton button5 = new JButton("Button");

        button.setPreferredSize( new Dimension(160, 45) );
        buttonPanel.add(button);
        buttonPanel.add(button1);
        buttonPanel.add(button2);
        buttonPanel.add(button3);
        buttonPanel.add(button4);
        buttonPanel.add(button5);

         button.addActionListener(new Action());
    }

    background.add( buttonPanel );
}

static class Action implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            JFrame frame2 = new JFrame("Museums in London");
            frame2.setVisible(true);
            frame2.setSize(550, 650);
            JPanel panel = new JPanel();
            frame2.add(panel);
            Custom contentPane;

            // JFrame frame = new JFrame("JTextField");
            contentPane = new Custom();
            frame2.setContentPane(contentPane);

        }
    }

private static void ShowGUI()
{
    JFrame frame = new JFrame("SSCCE");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new GasIndicator());
    frame.pack();
    frame.setLocationByPlatform( true );
    frame.setVisible( true );
}

public static void main(String[] args)
{
    EventQueue.invokeLater( () -> ShowGUI() );
/*
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            createAndShowGUI();
        }
    });
*/
}

class Custom extends JPanel {

public BufferedImage image;

public Custom() {
    try {

        image = ImageIO.read(new URL 
("http://www.destination360.com/europe/uk/images/s/museums.jpg"));
    } catch (IOException ioe) {
        System.out.println("Unable to fetch image.");
        ioe.printStackTrace();
    }
}

public Dimension getPreferredSize() {
    return (new Dimension(image.getWidth(), image.getHeight()));
}

public void paintComponent(Graphics x) {
    super.paintComponent(x);
    x.drawImage(image, 10, 10, this);
}
  }
}
包装气体指示器;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.image.buffereImage;
导入javax.swing.*;
导入javax.swing.border.*;
导入javax.swing.plaf.basic.*;
导入java.io.*;
导入javax.imageio.*;
导入java.net。*;
公共类GasIndicator扩展JPanel
{
私有图像;
气体指示剂()
{
尝试
{
image=ImageIO.read(新URL(“http://i68.tinypic.com/2ceja8i.png"));
}
捕获(ioe异常ioe)
{
System.out.println(“无法获取图像”);
ioe.printStackTrace();
}
setLayout(新的BorderLayout());
JLabel background=新JLabel(新图像图标(图像));
setLayout(新的FlowLayout(FlowLayout.LEFT));
添加(背景);
JPanel buttonPanel=新的JPanel(新的网格布局(0,3,6,5));
按钮面板设置顺序(新的清空顺序(338233,0,0));
buttonPanel.set不透明(假);
//对于(int i=0;i<7;i++)
{
JButton按钮=新JButton(“按钮”);
JButton button1=新JButton(“按钮”);
JButton button2=新JButton(“按钮”);
JButton button3=新JButton(“按钮”);
JButton button4=新JButton(“按钮”);
JButton button5=新JButton(“按钮”);
按钮。设置首选尺寸(新尺寸(160,45));
按钮面板。添加(按钮);
按钮面板。添加(按钮1);
按钮面板。添加(按钮2);
按钮面板。添加(按钮3);
按钮面板。添加(按钮4);
按钮面板。添加(按钮5);
addActionListener(新操作());
}
添加(按钮面板);
}
静态类操作实现ActionListener{
已执行的公共无效操作(操作事件e){
JFrame frame2=新JFrame(“伦敦博物馆”);
frame2.setVisible(true);
框架2.设置尺寸(550650);
JPanel面板=新的JPanel();
框架2.添加(面板);
自定义内容窗格;
//JFrame=新的JFrame(“JTextField”);
contentPane=newcustom();
frame2.setContentPane(contentPane);
}
}
私有静态void ShowGUI()
{
JFrame=新JFrame(“SSCCE”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新的气体指示器());
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共静态void main(字符串[]args)
{
invokeLater(()->ShowGUI());
/*
invokeLater(新的Runnable()
{
公开募捐
{
createAndShowGUI();
}
});
*/
}
类自定义扩展JPanel{
公共缓存图像;
公共习俗{
试一试{
image=ImageIO.read(新URL
("http://www.destination360.com/europe/uk/images/s/museums.jpg"));
}捕获(ioe异常ioe){
System.out.println(“无法获取图像”);
ioe.printStackTrace();
}
}
公共维度getPreferredSize(){
返回(新维度(image.getWidth(),image.getHeight());
}
公共组件(图形x){
超级组件(x);
x、 drawImage(图像,10,10,this);
}
}
}

这里的问题是您的面板上设置了standart Layoutmanager。 尝试使用禁用它

contentPane.setLayout(null);
然后,您可以使用
button.setCounts(x,y,w,h)随意移动按钮

我总是直接在框架上画图,不像你那样使用自定义面板,但我发现在一些用例中,这是更好的方法

如果你写信

new JFrame.getContentPane().setLayout(null);
您的框架获得默认窗格,但其布局仍设置为空

不同之处在于,您可以直接添加到框架中,而不是使用一个可以放置您的东西的面板

frame.add(new Button().setBounds(x,y,w,h));
请尝试我尝试调试的代码的以下版本:

Custom1 contentPane;


//at first create and set the frame
JFrame frame = new JFrame("Windowname goes here");
frame.setSize(1160, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

//next add your custom panel
contentPane = new Custom1();
frame.setContentPane(contentPane);

//better add this to the panel instead of the frame
JTextField textfield = new JTextField(20);
frame.add(textfield);

textfield.setBackground(Color.black);
textfield.setForeground(Color.white);

//you are using 2 panels with null layout here
JPanel panel = new JPanel();
panel.setSize(frame.getHeight(),frame.getWidth());      //give the panel some bounds!
panel.setOpaque(false); //unnecesary
panel.setLayout(null);
panel.setBackground(Color.BLACK);
frame.add(panel);

JButton button = new JButton("London");
JButton button1 = new JButton("Oxford");
JButton button2 = new JButton("Cambridge");

//you seemed to not have added these before
JButton button4 = new JButton("Click");
JButton button5 = new JButton("Click");
JButton button6 = new JButton("Click");

button.setBounds(60, 400, 220, 30);
button.setBackground(Color.black);
button.setForeground(Color.WHITE);

button1.setBounds(x,y,w,h);                         // !!!! <------
button1.setBackground(Color.black);
button1.setForeground(Color.WHITE);

button2.setBounds(x,y,w,h);                         // !!!! <------
button2.setBackground(Color.black);
button2.setForeground(Color.WHITE);

button4.setBounds(80, 50, 100, 30);
button5.setBounds(x,y,w,h);                         // !!!! <------
button6.setBounds(x,y,w,h);                         // !!!! <------

panel.add(button);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);

//SETVISIBLE ALWAYS GOES LAST
frame.setVisible(true);
Custom1内容窗格;
//首先,创建并设置框架
JFrame=newjframe(“Windowname在此显示”);
框架设置尺寸(1160700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(空);
//下一步添加自定义面板
contentPane=newcustom1();
frame.setContentPane(contentPane);
//最好将其添加到面板而不是框架中
JTextField textfield=新的JTextField(20);
frame.add(textfield);
textfield.setBackground(颜色:黑色);
设置前景(颜色为白色);
//您在这里使用的是两个布局为空的面板
JPanel面板=新的JPanel();
panel.setSize(frame.getHeight(),frame.getWidth())//给小组一些界限!
面板。设置不透明(假)//不必要的
panel.setLayout(空);
面板.立根背景(颜色.黑色);
框架。添加(面板);
JButton button=新JButton(“伦敦”);
JButton button1=新JButton(“牛津”);
JButton button2=新JButton(“剑桥”);
//您以前似乎没有添加这些
JButton button4=新JButton(“单击”);
JButton button5=新JButton(“单击”);
JButton button6=新JButton(“单击”);
按钮.立根(60,400,220,30);
按钮。背景(颜色。黑色);
按钮。设置前景(颜色。白色);

按钮1.立根(x,y,w,h);/ 演示使用带边框的布局管理器概念的简单示例。按钮的大小也已调整为图像中按钮的大小:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;
import java.io.*;
import javax.imageio.*;
import java.net.*;

public class SSCCE extends JPanel
{
    private Image image;

    SSCCE()
    {
        try
        {
            image = ImageIO.read(new URL("http://i68.tinypic.com/2ceja8i.png"));

        }
        catch (IOException ioe)
        {
            System.out.println("Unable to fetch image.");
            ioe.printStackTrace();
        }

        setLayout( new BorderLayout() );

        JLabel background = new JLabel( new ImageIcon(image) );
        background.setLayout( new FlowLayout(FlowLayout.LEFT) );
        add( background );

        JPanel buttonPanel = new JPanel( new GridLayout(0, 3, 6, 5) );
        buttonPanel.setBorder( new EmptyBorder(338, 233, 0, 0) );

        buttonPanel.setOpaque( false );

        for (int i = 0; i < 6; i++)
        {
            JButton button = new JButton("Button " + i);
            button.setPreferredSize( new Dimension(160, 45) );
            buttonPanel.add(button);
        }

        background.add( buttonPanel );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.border.*;
导入javax.swing.plaf.basic.*;
导入java.io.*;
导入javax.imageio.*;
导入java.net。*;
公共类SSCCE扩展了JPanel
{
私有图像;
SSCCE()
{
尝试
{
image=ImageIO.read(新URL(“http://i68.tinypic.com/2ceja8i.png"));
}
捕获(ioe异常ioe)
{
System.out.println(“无法