带有setUndercorated的JFrame为true

时间:2016-06-03 13:21:05

标签: java jframe

使用setUndercorated = true,如何使用退出按钮退出窗口?

public static void main(String[] args) {
    JLabel label = new JLabel("My label");
    label.setText("Wello!");

    JFrame f = new JFrame();
    f.setUndecorated(true);
    f.setAlwaysOnTop(true);
    f.setResizable(false);
    try {
        f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("file")))));
    } catch (IOException e) {
        e.printStackTrace();**strong text**
    }
    f.pack();
    f.setVisible(true);
}

}

1 个答案:

答案 0 :(得分:0)

您需要将自己的退出按钮添加到面板:

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

public class Example extends JFrame {

    public Example() {

        setUndecorated(true);
        setAlwaysOnTop(true);
        setResizable(false);
        setMinimumSize(new Dimension(300, 300));
        setLocationRelativeTo(null);

        setLayout(null);

        JButton closeButton = new JButton("X");
        closeButton.setFocusPainted(false);
        closeButton.setBorder(new LineBorder(Color.black, 1));
        closeButton.setBounds(0, 0, 30, 30);
        closeButton.addActionListener(e -> dispose());
        add(closeButton);

        pack();
        setVisible(true);
    }

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

这导致JFrame看起来像:

enter image description here

当您单击该按钮时,它会关闭JFrame。