我正在尝试在JFrame中绘制一个字符串,但它不起作用..请帮助

时间:2014-06-19 07:07:47

标签: java swing jframe paint repaint

我按下按钮时尝试显示字符串,但它不起作用。我不知道问题是什么。我没有错误,但这并没有打扰我。我想,我错过了一些基本的东西。请帮忙!!

//I'm trying to draw a string in the frame when a button is pressed, but it won't work..
//Can't figure out what the problem is.


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class AppletTwo extends JFrame implements ActionListener
{

    JFrame frameOne;
    JButton btnOne;

    AppletTwo()
    {

        frameOne = new JFrame("frameOne");
        frameOne.setSize(320,240);
        frameOne.setLayout(new FlowLayout(FlowLayout.LEFT));
        frameOne.setVisible(true);
        frameOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btnOne = new JButton("Print");
        btnOne.addActionListener(this);
        frameOne.add(btnOne);
    }

    public void actionPerformed(ActionEvent ae)
    {

        if(ae.getSource() == btnOne)
        {
            repaint();
        }
    }

    public void paint(Graphics g)
    {
        g.drawString("Never Works",150,150);
    }

    public static void main(String[] args)
    {

        AppletTwo frame1 = new AppletTwo();
    }
}

1 个答案:

答案 0 :(得分:6)

  

“我想,我错过了一些基本的东西。”

是的,你是:

主要问题:

  • 您的班级是JFrame,这是您要覆盖paint方法的组件。但是您创建了JFrame的另一个实例,这是您setVisible的实例。请记住,您没有在此框架中绘制任何内容。所以你看到了框架的新实例,不是你正在绘制的类框架(并且你从未设置过可见的框架)。

其他问题:

  • 您应该在super.paint[Component]覆盖

    之后致电paint[Component]
    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }
    
  • 不要在JFrame等顶级容器上绘画。而是在JPanelJComponent上绘制并覆盖paintComponent方法并调用super.paintComponent,然后将该组件添加到框架中。请参阅Performing Custom Painting

  • 应该在事件派发线程(EDT)上运行Swing应用程序。您可以将main代码封装在SwingUtilities.invokeLater(...)中。见Initial Threads

    public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                AppletTwo frame1 = new AppletTwo();
            }
        }); 
    }
    
  • 通常,您总是希望在添加组件后将框架设置为

其他说明:


<强>更新

上述所有要点的例子。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SimpleDrawing {

    public SimpleDrawing() {
        final DrawingPanel panel = new DrawingPanel();

        final JTextField field = new JTextField(15);

        JButton button = new JButton("Change name");
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                String someString = field.getText();
                if (!someString.isEmpty()) {
                    panel.setString(someString);
                }
            }
        });

        JPanel bottomPanel = new JPanel();
        bottomPanel.add(field);
        bottomPanel.add(button);

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.add(bottomPanel, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    public class DrawingPanel extends JPanel {
        private String someString = "Stackoverflow";

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString(someString, 75, 75);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension (300, 100);
        }

        public void setString(String someString) {
            this.someString = someString;
            repaint();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                new SimpleDrawing();
            }
        });
    }  
}