문제

I have a decorated JFrame. I need to make close button and minimize button. What should I do?

Here is my code snippet:

public Startup()
{
    setTitle("STARTUP");
    setSize(800,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setUndecorated(true);
    setLocationRelativeTo(null);
    setVisible(true);
}
도움이 되었습니까?

해결책

Your approach is very unique and will look quite good. There are many ways to solve your problem. Now, as per your request, you want a CLOSE and a MINIMIZE button. Let us make the following Actions.

private final Action exitAction = new AbstractAction("Exit")
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    };
private final Action minimizeAction = new AbstractAction("Minimize")
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            setState(JFrame.ICONIFIED);
        }
    };

Now, let us apply the above actions to JButtons:

JButton closeButton = new JButton(exitAction);
JButton miniButton = new JButton(minimizeAction);

There you have it. Now, all you need to do is add your buttons to your JFrame.

다른 팁

Note For Eclipse Users this code will come in your minimize button when You click On minimize button in Eclipse.

YourFrameName is the Frame name you had set or it is set by default, use that frame name here:

YourFrameName.setState(YourFrameName.ICONIFIED);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top