GUI 中 Windows 的引用(?)问题

发布于 2024-11-26 06:22:27 字数 3561 浏览 0 评论 0原文

- 编辑 - 我有一个由两个 JLabel 组成的欢迎窗口。它有一个从 3 到 0 计数的计时器的链接。在该时间之后,包含 JLabel 和单选按钮的新窗口“UsedBefore”应自动出现在前一个窗口的位置。当我运行“启动器”时,第一个窗口显示,计数器显示 3,2,1,0,然后什么也没有发生。

我认为问题在于引用不当,但我不确定。我有“Launcher”类:

public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            Welcome window = new Welcome();
            window.setVisible(true);
        }
    });

} // end main

我在其中启动“Welcome”窗口:

public Welcome() {
    init();
}

public void init() {

    // here I'm adding stuff to the window and then I have:     

    setLayout(cardLayout);
    add(big, "1welcome");
//  UsedBefore.MakeUsedBeforeWindow(); // ???
    new MyTimer(this).start();

} // end init

这会转到 MyTimer 进行倒计时,并且:

 welcome.showNextWindow(); // private Welcome welcome;

我们回到“Welcome”类:

public void showNextWindow() {
    cardLayout.next(this);
}

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("My Frame");
    frame.getContentPane().add(new Welcome());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(550, 450);
    frame.setResizable(false);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

最后是“UsedBefore”类:

public UsedBefore() {           
        super(new BorderLayout());
        init();         
    }

    public void MakeUsedBeforeWindow() {            

        String q = "Have you used GUI before?";
        JPanel area = new JPanel(new BorderLayout());
        add(area, "2usedBefore?");
        area.setBackground(Color.white);
        JLabel textLabel = new JLabel("<html><div style=\"text-align: center;\">"
                + q + "</html>", SwingConstants.CENTER);
        textLabel.setForeground(Color.green);
        Font font = new Font("SansSerif", Font.PLAIN, 30);
        textLabel.setFont(font);
        textLabel.setBorder(new EmptyBorder(0, 0, 250, 0)); //top, left, bottom, right
        area.add(textLabel, SwingConstants.CENTER);
        add(area, "2usedBefore?");          
    }

其主要内容:

        public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

public static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("RadioButtons");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane - not sure how to do it
//          JComponent newContentPane = new UsedBefore();
//          newContentPane.setOpaque(true); //content panes must be opaque
//          frame.setContentPane(newContentPane);
//          frame.getContentPane().add(new UsedBefore());

        //Display the window.
        frame.setSize(550, 450);
        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        }

这是一段相当长的旅程。抱歉有很多代码,我希望路径是清晰的。一旦我正确获得了 1->2->3 链接,我应该能够完成其余的链接,因此我们将不胜感激。谢谢。

--EDIT--
I have got a welcome window consisting of two JLabels. It has a link to a timer counting from 3 to 0. After that time, a new window, "UsedBefore", containing JLabel and radio buttons should automatically appear in the place of the previous one. When I run the "Launcher", the first window shows up with counter displaying 3,2,1,0 and then nothing happens.

I think the problem lies in poor referencing, but I'm not sure. I've got "Launcher" class:

public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            Welcome window = new Welcome();
            window.setVisible(true);
        }
    });

} // end main

Where I launch the "Welcome" window:

public Welcome() {
    init();
}

public void init() {

    // here I'm adding stuff to the window and then I have:     

    setLayout(cardLayout);
    add(big, "1welcome");
//  UsedBefore.MakeUsedBeforeWindow(); // ???
    new MyTimer(this).start();

} // end init

this goes to MyTimer which does the countdown and:

 welcome.showNextWindow(); // private Welcome welcome;

we go back to the "Welcome" class:

public void showNextWindow() {
    cardLayout.next(this);
}

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("My Frame");
    frame.getContentPane().add(new Welcome());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(550, 450);
    frame.setResizable(false);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

and finally the "UsedBefore" class:

public UsedBefore() {           
        super(new BorderLayout());
        init();         
    }

    public void MakeUsedBeforeWindow() {            

        String q = "Have you used GUI before?";
        JPanel area = new JPanel(new BorderLayout());
        add(area, "2usedBefore?");
        area.setBackground(Color.white);
        JLabel textLabel = new JLabel("<html><div style=\"text-align: center;\">"
                + q + "</html>", SwingConstants.CENTER);
        textLabel.setForeground(Color.green);
        Font font = new Font("SansSerif", Font.PLAIN, 30);
        textLabel.setFont(font);
        textLabel.setBorder(new EmptyBorder(0, 0, 250, 0)); //top, left, bottom, right
        area.add(textLabel, SwingConstants.CENTER);
        add(area, "2usedBefore?");          
    }

with its main:

        public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

public static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("RadioButtons");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane - not sure how to do it
//          JComponent newContentPane = new UsedBefore();
//          newContentPane.setOpaque(true); //content panes must be opaque
//          frame.setContentPane(newContentPane);
//          frame.getContentPane().add(new UsedBefore());

        //Display the window.
        frame.setSize(550, 450);
        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        }

That's quite a journey. Sorry for a lot of code, I hope the path is clear. Once I've got 1->2->3 links right, I should be able to do the rest of them, so any help is appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2024-12-03 06:22:27

我建议采取稍微不同的方法。为什么不构建 JPanel 而不是 Windows,并在需要的时候添加/删除这些 JPanel

例如(这里首先显示 welcome 面板及其递减的计数器,当计数器达到 0 时,显示 other 面板):

public class T extends JFrame {

private int couterValue = 3;
private JPanel welcome;
private JLabel counter;

private JPanel other;
private Timer timer;

public T() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    buildWelcomePanel();
    buildOtherPanel();

    add(welcome);
    setSize(550, 450);
    setLocationRelativeTo(null);
    setVisible(true);

    timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            if (couterValue == 0) {
                timer.cancel();
                // Switch the panels as the counter reached 0
                remove(welcome);
                add(other);
                validate();
            } else {
                counter.setText(couterValue + ""); // Update the UI counter
                couterValue--;
            }
        }
    }, 0, 1000);

}

private void buildWelcomePanel() {
    welcome = new JPanel();
    counter = new JLabel();
    welcome.add(counter);
}

private void buildOtherPanel() {
    other = new JPanel();
    JLabel otherStuff = new JLabel("Anything else ...");
    other.add(otherStuff);
}

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

I suggest a slightly different approach. Why not building JPanel instead of Windows and adding/removing these JPanel at the desire time.

Ex (here welcome panel is shown first with its decreasing counter and when the counter reach 0, other panel is shown):

public class T extends JFrame {

private int couterValue = 3;
private JPanel welcome;
private JLabel counter;

private JPanel other;
private Timer timer;

public T() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    buildWelcomePanel();
    buildOtherPanel();

    add(welcome);
    setSize(550, 450);
    setLocationRelativeTo(null);
    setVisible(true);

    timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            if (couterValue == 0) {
                timer.cancel();
                // Switch the panels as the counter reached 0
                remove(welcome);
                add(other);
                validate();
            } else {
                counter.setText(couterValue + ""); // Update the UI counter
                couterValue--;
            }
        }
    }, 0, 1000);

}

private void buildWelcomePanel() {
    welcome = new JPanel();
    counter = new JLabel();
    welcome.add(counter);
}

private void buildOtherPanel() {
    other = new JPanel();
    JLabel otherStuff = new JLabel("Anything else ...");
    other.add(otherStuff);
}

public static void main(String[] args) {
    new T();
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文