简体   繁体   English

如何将 JPanel 带到 JFrame 的前面

[英]How to bring a JPanel to the front of the JFrame

I have a project due tomorrow so I need help.我明天有一个项目要交,所以我需要帮助。 I'm making a clock I largely got help from others and I need to get a JButton to the front.我正在制作一个时钟,我在很大程度上得到了其他人的帮助,我需要在前面安装一个 JButton。 I have the button as part of a JPanel我有按钮作为 JPanel 的一部分

public static void main(String[] args) {
        JFrame window = new JFrame("Clock");  
        Color c=new Color(0,0,0);  
        Clock clock = new Clock();
        JPanel p=new JPanel();

        window.setPreferredSize(new Dimension(400,400));
        window.pack();


        window.setBackground(c);  
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        Button buton =new Button("Stopwatch");

        p.add(buton);
        window.getContentPane().add(clock);  
        window.setVisible(true);
        clock.start(); 
}
g.setColor(Color.black);//Background color
        g.setFont(new Font("ComicSans", Font.BOLD, 20));  
        g.setColor(Color.white);//inner circle color
        g.fillOval(xcenter - 150, ycenter - 150, 300, 300);  

        LocalDateTime time =LocalDateTime.now();
        String strTime =time.toString().substring(14,19);
        String strHour =time.toString().substring(11,13);
        int hour =_12hrFormat(strHour);

        g.setColor(Color.black);//time color
        g.drawString(hour+":"+strTime ,141,300);  
        g.setColor(Color.black);//number color
        g.drawString("9", xcenter - 145, ycenter +10);  
        g.drawString("3", xcenter + 135, ycenter +10);  
        g.drawString("12", xcenter-10 , ycenter - 130); 
}

That is most of the code to do with visuals这是与视觉效果有关的大部分代码

Your code is definitely not completed.你的代码肯定没有完成。 But let's presume you draw your oval and other staff in paintComponent .但是让我们假设您在paintComponent 中绘制了椭圆形和其他五线谱。 Also you have to add your JPanel to Frame.您还必须将 JPanel 添加到 Frame。

JFrame frame = new JFrame("some frame");
JPanel panel = new JPanel();
frame.setContentPane(panel);

Then you can add whatever you want on it.然后你可以在上面添加任何你想要的东西。

And I hope your g is part of paintComponent.我希望你的g是paintComponent的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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