Guest_1013 2021-08-13 20:21 采纳率: 71.4%
浏览 89
已结题

Java,swing,在继承JPanel的组件内添加按钮后的问题

我在继承JPanel类的一个类(即下文的ClockPanel)中添加了一个按钮,功能是在按下后弹出一个新窗口。在运行开始时还能准确工作,但是在最小化并还原窗口后按钮就失效了。我尝试着把按钮直接加入JFrame,结果在最小化并重新还原窗口后按钮仍能点击。请问这是为什么?
完整代码:
ClockPanel.java(即继承了JPanel的类):

package JTimer;
import javax.swing.JPanel;
import java.awt.*;
import java.util.Calendar;

public class ClockPanel extends JPanel {//绘制时钟
    Image im;
    Graphics g;
    
    static final int HOUR_HAND = 50,MINUTE_HAND = 75,SECOND_HAND = 100;
    public ClockPanel(Image im) {
        if(im != null) {
            this.im = im;
            this.g = im.getGraphics();
        }
    }
    public void display() {
        if(this.g != null) {
            super.paint(g);
            Calendar c = Calendar.getInstance();
            int current_sec_raw = c.get(Calendar.SECOND);
            boolean is_drawn = false;
            int current_sec = current_sec_raw;
            this.g.setXORMode(Color.BLACK);
            this.g.setColor(Color.WHITE);
            this.g.drawArc(50,50,300,300,0,360);
            this.g.fillArc(195,195,10,10,0,360);
            
            while(true) {
                c = Calendar.getInstance();
                this.g.setXORMode(Color.BLACK);
                this.g.setColor(Color.WHITE);
                if((is_drawn == true) && (current_sec != c.get(Calendar.SECOND))) {
                    draw(current_sec);
                    current_sec = c.get(Calendar.SECOND);
                    is_drawn = false;
                }
                if(is_drawn == false) {
                    draw(current_sec);
                    is_drawn = true;
                }
                this.repaint();
            }
            
            
        }
    }
    public void draw(int current_sec) {
        Calendar c = Calendar.getInstance();
        
        this.g.drawLine(200,200,(int)(200+this.HOUR_HAND*Math.sin(2*Math.PI*c.get(Calendar.HOUR)/12)),(int)(200-this.HOUR_HAND*Math.cos(2*Math.PI*c.get(Calendar.HOUR)/12)));
        this.g.drawLine(200,200,(int)(200+this.MINUTE_HAND*Math.sin(2*Math.PI*c.get(Calendar.MINUTE)/60)),(int)(200-this.MINUTE_HAND*Math.cos(2*Math.PI*c.get(Calendar.MINUTE)/60)));
        this.g.drawLine(200,200,(int)(200+this.SECOND_HAND*Math.sin(2*Math.PI*current_sec/60)),(int)(200-this.SECOND_HAND*Math.cos(2*Math.PI*current_sec/60)));
    }
    
    @Override
    public void paint(Graphics g) {
        g.drawImage(im,0,0,this);
    }
    
}


AboutButtonActionListener.java(按钮的监听):

package JTimer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class AboutButtonActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("v1.0 alpha");
        JFrame about_frame = new JFrame("About");
        about_frame.setSize(300, 200);
        about_frame.setDefaultCloseOperation(about_frame.DISPOSE_ON_CLOSE);
        about_frame.setResizable(false);
        about_frame.setLayout(null);
        JLabel about_label = new JLabel("version:v1.0 alpha");
        about_label.setBounds(85,0,150,20);
        about_frame.add(about_label);
        about_frame.setVisible(true);
    }
}

launch.java(主函数所在):

package JTimer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

public class launch {

    public static void main(String[] args) {
        
        JFrame frame = new JFrame("JTimer");
        frame.setSize(417, 440);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setResizable(false);
        frame.setVisible(true);
        
        Image im = new BufferedImage(400,400,BufferedImage.TYPE_INT_RGB);//用于ClockPanel
        ClockPanel clock = new ClockPanel(im);
        clock.setBackground(Color.WHITE);
        clock.setBounds(0, 0, 400, 400);
        frame.setContentPane(clock);
        JButton about_button = new JButton("About");//按钮
        about_button.setBounds(0, 0, 100, 20);
        AboutButtonActionListener abal = new AboutButtonActionListener();//按钮监听
        about_button.addActionListener(abal);
        clock.add(about_button);//加入按钮
        clock.display();
    }
}
  • 写回答

1条回答 默认 最新

  • Guest_1013 2021-08-13 21:22
    关注

    我明白了,我没有提前设置好clock的Layout,设置一下就行了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月21日
  • 已采纳回答 8月13日
  • 创建了问题 8月13日

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算