gpt4 book ai didi

java - 当你有JLabel背景时如何将Jbuttons设置到特定位置: code below

转载 作者:行者123 更新时间:2023-12-02 06:01:10 25 4
gpt4 key购买 nike

当您有 JLabel 背景时,如何将 Jbuttons 设置到特定位置:代码如下我无法让 jlabel 保持在顶部而按钮保持在南边(底部)?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonsClass extends JFrame
implements ActionListener {
JButton b1 = new JButton("button1");
JButton b2 = new JButton("button2");
JButton b3 = new JButton("button3");
JButton b4 = new JButton("button4");
JLabel label = new JLabel("buttons:");

public static void main(String[] args) {
new ButtonsClass();
}

public Jukebox() {
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("image.png")));
setLayout(new FlowLayout());
setSize(500,150);
setTitle("Backgroundwithbuttons");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);


JPanel top = new JPanel();

top.add(label);
add("North", top);
JPanel bottom = new JPanel();
bottom.add(b1);
bottom.add(b2);
bottom.add(b3);
bottom.add(b4);
add("South", bottom);
setVisible(true);
}

}

最佳答案

" i can't get the jlabel to stay at the top and the buttons to stay south(bottom)"

  • 那是因为您将布局设置为 BorderLayout,然后立即将其设置为 FlowLayout。使用 FlowLayout,您的 BorderLayout 定位将不会执行任何操作。

    setLayout(new BorderLayout());
    setContentPane(new JLabel(new ImageIcon("image.png")));
    setLayout(new FlowLayout());

只需摆脱 setLayout(new FlowLayout());

  • 你的构造函数也是错误的

    public Jukebox() {

    -Should be-

    public ButtonClass() {
  • 您还需要设置设置为内容 Pane 的 JLabel 的布局。您的构造函数应该如下所示

    public ButtonClass() {
    JLabel background = new JLabel(new ImageIcon("image.png"));
    background.setLayout(new BorderLayout());
    setContentPane(background);
    setTitle("Background with buttons");
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

    JPanel top = new JPanel();
    top.add(label);
    add(top, BorderLayout.NORTH);
    JPanel bottom = new JPanel();
    bottom.add(b1);
    bottom.add(b2);
    bottom.add(b3);
    bottom.add(b4);
    add(bottom, BorderLayout.SOUTH);
    //pack();
    setVisible(true);
    }
  • 此外,add("North", top); 是一个已弃用的方法。而是使用 add(top, BorderLayout.NORTH) 和相同的 add(bottom, BorderLayout.SOUTH)

  • 此外,Swing 应用程序应在事件调度线程上运行。您可以通过使用 SwingUtilities.invokeLater...

    将代码包装在 main 中来实现此目的
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new ButtonClass();
    }
    });
    }
  • 此外,如果您希望图像显示在面板后面,则应该将面板的 opaque 属性设置为 false。

    top.setOpaque(false);
    bottom.setOpaque(false);

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonClass extends JFrame
implements ActionListener {

JButton b1 = new JButton("button1");
JButton b2 = new JButton("button2");
JButton b3 = new JButton("button3");
JButton b4 = new JButton("button4");
JLabel label = new JLabel("buttons:");

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ButtonClass();
}
});
}

public ButtonClass() {
label.setForeground(Color.WHITE);
JLabel background = new JLabel(new ImageIcon(getClass().getResource("/resources/space.png")));
background.setLayout(new BorderLayout());
setContentPane(background);
setTitle("Background with buttons");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

JPanel top = new JPanel();
top.setOpaque(false);
top.add(label);
add(top, BorderLayout.NORTH);
JPanel bottom = new JPanel();
bottom.setOpaque(false);
bottom.add(b1);
bottom.add(b2);
bottom.add(b3);
bottom.add(b4);
add(bottom, BorderLayout.SOUTH);
setSize(400, 300);
setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {}
}

关于java - 当你有JLabel背景时如何将Jbuttons设置到特定位置: code below,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22668846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com