【问题标题】:Click button to draw text won't work unless frame is resized除非调整框架大小,否则单击按钮绘制文本将不起作用
【发布时间】:2021-11-25 10:36:15
【问题描述】:

如果在下面的代码中我省略了布尔变量 bDrawText,那么一旦程序启动,文本就会显示出来。 因此,我将该变量设置为 false,以便仅在单击按钮时才绘制文本。但它不起作用,并且只有在调整框架大小时才会出现文本。我认为我对图形的使用是错误的......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Demo1 {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() { 
                justAnyFrame f = new justAnyFrame("Draw text",200,100,600,400);                         
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);       
            }
        });
    }
}
////////////////////////////////////////////////////////////////
class justAnyFrame extends JFrame { 
    private JButton bDrawText;
    private boolean drawNow=false;
    public justAnyFrame(String title,int left,int top,int width,int height) {   
        setTitle(title);
        setLocation(left,top);
        setSize(width,height);
        centralPanel cp=new centralPanel();
        cp.setBackground(Color.LIGHT_GRAY);
        add(cp,BorderLayout.CENTER);
        lowerPanel lp=new lowerPanel();
        add (lp, BorderLayout.SOUTH);       
    }
    //////////////////////////////////////////////////////////////////
    class lowerPanel extends JPanel  implements ActionListener {
        public lowerPanel() {
            setLayout(new FlowLayout(FlowLayout.LEFT));
            bDrawText=new JButton("Draw Text");
            bDrawText.addActionListener(this);  
            add(bDrawText);
        }
        
        public void actionPerformed(ActionEvent e) {
            Object clicked=e.getSource();
            if(clicked==bDrawText) {
                drawNow=true;
                repaint();
            }
        }
    }
    ////////////////////////////////////////////////////////////////
    class centralPanel extends JPanel {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g; 
            if(drawNow) {
                g2.setColor(Color.BLUE);
                Font myFont=new Font("Helvetica",Font.BOLD,40);
                g2.setFont(myFont);
                g2.drawString("QWERTY",50,100);
            }
        }           
    }   
}

【问题讨论】:

    标签: java swing graphics


    【解决方案1】:

    你为lowerPanel调用repaint,但是需要repaint the centralPanel。

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 谢谢,在我将 centralPanel cp 声明为框架构造函数之外的私有然后调用 cp.repaint();来自按钮的 actionPerformed 方法。
    • 很好地删除另一个答案,并扩展这个答案。继续这样下去,你很快就会有足够的代表来制作 cmets。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2021-12-27
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多