简体   繁体   中英

Remove title bar with maximum size

package com.company;

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

public class Main extends JFrame{

    private JButton button1;
    private JPanel mainPanel;
    private JCheckBox checkBox1;
    private JCheckBox checkBox2;

    public void command(int x) {
        System.out.println(x);
    }

    public Main() {
//        setSize(300,250);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setUndecorated ( true );

        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Menu");
        add(mainPanel);
        button1.addActionListener(ActionEvent -> dispose());
        checkBox1.addActionListener(ActionEvent -> command(2));
        checkBox2.addActionListener(ActionEvent -> command(1));
    }

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

I have a form alongside this as well for the actual buttons. But how would I get it so the window can be maximized but still get rid of the title bar because with these two lines.

        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setUndecorated ( true );

It maximizes it, but it goes over the bottom task bar which isn't how I want it. Is there any way to fix this or another way to just get rid of title bar that'll also let me maximize it.

But how would I get it so the window can be maximized but still get rid of the title bar

You can manually set the size to exclude the task bar:

//setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated ( true );
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
setBounds(env.getMaximumWindowBounds());
setVisible(true);

This should also work if the task bar is positioned other than the bottom.

Also note that components should be added to the frame BEFORE the frame is made visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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