Components in second JFrame not showing up

前端 未结 2 1578
有刺的猬
有刺的猬 2021-01-27 18:24

i want to start another JFrame from pressing a button in a JFrame. But if I press the Button it shows the JFrame but not the Buttons and Sliders in it.

public c         


        
相关标签:
2条回答
  • 2021-01-27 18:45

    Make setVisible the last thing you call...

    public void buildGUI1() throws NullPointerException {
        setTitle("Hauptmenü");
        setSize(800, 480);
        setLayout(new GridLayout());
        setAlwaysOnTop(false);
        setLocation((Toolkit.getDefaultToolkit().getScreenSize().width) / 4, (Toolkit.getDefaultToolkit().getScreenSize().height) / 4);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        final JButton startclickbt = new JButton("Start Clicker");
        startclickbt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
                if (klick == null) {
                    klick = new Clicker();
                    add(klick);
                    // Add this when you need to add/remove components
                    revalidate();
                    repaint();
                }
    
            }
        });
    
        add(startclickbt);
        // Move to here
        setVisible(true);
    
    }
    

    Also, use revalidate() and repaint() to encourage the container to update it's layout when adding new components

    Recommendations:

    • Make sure your UI is started within the context of the EDT, see Initial Threads for more details
    • Avoid extending directly from top level containers like JFrame and instead consider using a JPanel as you base container. This free's up you UI to a wider range of use-cases and prevents you from getting locked in (as you can't add frames to other frames)
    • The Use of Multiple JFrames, Good/Bad Practice?

    Updated

    You have two immediate problems

    1. Clicker extends from a JFrame, but you are trying to add it to another container, this is not possible in Swing and will cause an exception, however...
    2. You are blocking the Event Dispatching Thread with your while (true) loop, meaning that Swing is unable to process any new events, including repaint events

    For example...

    public Clicker(boolean visible) {
    
        buildGUI(visible);
        j = new Click(false).addPosition(new Point((Toolkit.getDefaultToolkit().getScreenSize().width) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height) / 2)).addPosition(new Point(getLocation().x + 1, getLocation().y + 20));
        // This is bad
        while (true) {
    
            j.runClicks(getDelay());
        }
    }
    
    public Clicker() {
        buildGUI(true);
        j = new Click(false).addPosition(new Point((Toolkit.getDefaultToolkit().getScreenSize().width) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height) / 2)).addPosition(new Point(getLocation().x + 1, getLocation().y + 20));
        // This is bad
        while (true) {
    
            j.runClicks(getDelay());
        }
    }
    

    Now, Click looks like it's UI component which brings up a bunch of other problems, but we don't have the code for that so it's impossible to comment.

    The immediate solutions might be to use a:

    • SwingWorker, see Worker Threads and SwingWorker for more details

    • Swing Timer, see How to use Swing Timers for more details

    • Plain old Thread

    Things to remember

    • Swing (like most UI frameworks) is single threaded, anything which blocks this thread (like never ending loops), will prevent it from processing new events, including paint events, which will make your application look like it's "hung", because it has
    • Swing is NOT thread safe. All interactions with the UI MUST be made from within the context of the Event Dispatching Thread. Both SwingWorker and Swing Timer provide the capability to update the UI safely.
    0 讨论(0)
  • 2021-01-27 18:58

    Try with repaint() or revalidate() at the end of you constructor. And also try with calling setVisible(true) at the end.

    0 讨论(0)
提交回复
热议问题