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日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效