我无法将按钮移动到底部 - JFrame



我尝试使用GridLayout和BorderLayout,但无法让我的按钮进入狗的嘴巴下方(图片(。在狗嘴下面制作按钮后,如何为按钮添加功能?对于"设置",如何在单击它后使其打开另一个窗口,并且可以关闭设置窗口而不是整个窗口。谢谢!

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Codegi extends JFrame {
    public Codegi() {
        JFrame frame = new JFrame();
        try {
            frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("enter image description here[1]")))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        frame.pack();
        frame.setLayout(new FlowLayout());
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        p1.setOpaque(false);
        p2.setOpaque(false);
        p1.setLayout(new BorderLayout(0, 20));
        //JButton b1 = new JButton("START");
        //JButton b2 = new JButton("SETTINGS");
        p2.add(p1);
        p1.add(new JButton("SETTINGS"), BorderLayout.SOUTH);
        p1.add(new JButton("START"), BorderLayout.CENTER);
        //p1.add(b1);
        //p1.add(b2);
        frame.add(p2);
        //design of button
        //Font largefont = new Font("TimesRoman", Font.BOLD, 20);
        //b1.setBackground(Color.ORANGE);
        //b2.setBackground(Color.ORANGE);
        //b1.setForeground(Color.BLACK);
        //b2.setForeground(Color.BLACK);
        //b1.setFont(largefont);
        //b2.setFont(largefont);
        frame.setTitle("Codegi:Programming made fun");
        frame.setSize(498, 687);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(467, 627);
    }
    public static void main(String args[]) {
        new Codegi();
    }
}

截图

将布局设置为 nullview.setLayout(null(;然后设置按钮的位置,如下所示btn.setBounds(起始 x 坐标,起始 y 坐标,btn 宽度,btn 高度(;通过设置可见(假(删除面板;并用 setVisible(true( 显示新的;在BTN点击事件上

最新更新