繁体   English   中英

我无法将按钮移至底部-JFrame

[英]I can't move the buttons to the bottom - JFrame

我尝试使用GridLayout和BorderLayout,但是无法使Buttons脱离狗的嘴(图片)。 将按钮置于狗嘴下方之后,如何为按钮添加功能? 对于“设置”,如何在单击它后打开另一个窗口,并且可以关闭设置窗口而不是整个窗口。 谢谢!

这是我的代码:

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

屏幕截图

将布局设置为null view.setLayout(null); 然后像下面的btn.setBounds一样设置按钮的位置(开始x坐标,开始y坐标,btn宽度,btn高度); 通过setVisible(false)删除面板; 并使用setVisible(true)显示新的; 在btn点击事件上

暂无
暂无

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

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