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

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,设置一下就行了

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题