从底部动画JDialog移动

时间:2013-08-07 14:47:07

标签: java swing animation jframe jdialog

我已经为我的JDialog编写了一个动画,但是如果我在JButton的监听器中这样做,它可以工作但是在我的构造函数中没有。我已尝试使用线程和计时器,但它也不能正常工作。

我的代码:

TestTheDialog.java

import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;

public class TestTheDialog implements ActionListener {
    JFrame mainFrame = null;
    JButton myButton = null;

    public TestTheDialog() {
        mainFrame = new JFrame("TestTheDialog Tester");
        mainFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {System.exit(0);}
            });
        myButton = new JButton("Test the dialog!");
        myButton.addActionListener(this);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.getContentPane().add(myButton);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if(myButton == e.getSource()) {
            System.err.println("Opening dialog.");
            CustomDialogMessage myDialog = new CustomDialogMessage(mainFrame, true, "+33679149407","azertyuiopqsdfghjklmwxcvbnazertyuiopqsdfghjklmwxcvbn");
            System.err.println("After opening dialog.");
            if(myDialog.getAnswer()) {
                System.err.println("The answer stored in CustomDialog is 'true' (i.e. user clicked yes button.)");
            }
            else {
                System.err.println("The answer stored in CustomDialog is 'false' (i.e. user clicked no button.)");
            }
        }
    }

    public static void main(String argv[]) {

        TestTheDialog tester = new TestTheDialog();
    }
}

CustomDialogMessage.java:

import javax.swing.JDialog; 

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.Timer;

import java.awt.event.ActionEvent;
import java.util.concurrent.TimeUnit;

public class CustomDialogMessage extends JDialog implements ActionListener {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel myPanel = null;
    private JButton closeButton = null;
    private JButton answerButton = null;
    private JButton plusButton = null;
    private boolean answer = false;
    public boolean getAnswer() { return answer; }
    public int dialogWidth = 300;
    public int dialogHeight = 100;
    String textHeader ="You got a new message from :" ;

    public CustomDialogMessage(JFrame frame, boolean modal,String myNumero, String myMessage) {
        super(frame, modal);
        setUndecorated(true);
        setBackground(new Color(82,82,82,175));
        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle winSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
        final int taskBarHeight = screenSize.height - winSize.height;
        myPanel = new JPanel();
        myPanel.setPreferredSize(new Dimension(dialogWidth, dialogHeight));
        myPanel.setBackground(new Color(1,0,0,0));
        myPanel.setLayout(null);
        getContentPane().add(myPanel);
        JLabel header = new JLabel(textHeader);
        FontMetrics fm = header.getFontMetrics( header.getFont());
        fm.stringWidth("You got a new message !");
        header.setBounds((dialogWidth- fm.stringWidth(textHeader))/2, 0, 200, 30);
        myPanel.add(header);
        closeButton = new JButton("Close");
        closeButton.addActionListener(this);
        closeButton.setBounds(210, 75, 90, 25);
        myPanel.add(closeButton); 
        answerButton = new JButton("Answer");
        answerButton.addActionListener(this);
        answerButton.setBounds(0, 75, 90, 25);
        myPanel.add(answerButton);
        plusButton = new JButton("Plus");
        plusButton.addActionListener(this);
        plusButton.setBounds(105, 75, 90, 25);
        myPanel.add(plusButton);
        pack();
        setLocation(screenSize.width-dialogWidth,screenSize.height-dialogHeight-taskBarHeight);
        setVisible(true);
    }

    public void animation(){
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle winSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
        int taskBarHeight = screenSize.height - winSize.height;
        int i = 0;
        while(i<=dialogHeight){
            try {
                TimeUnit.MILLISECONDS.sleep(50);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            i+=5;
            this.setLocation(screenSize.width-dialogWidth,screenSize.height-i-taskBarHeight);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

最后我找到了一种方法:(我认为它会帮助很多人;))

MainWindow.java

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

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

public class MainWindow extends JFrame {

    private static final long serialVersionUID = 1L;


    public MainWindow() throws IOException {
        // name of window
        this.setTitle("BlueNect");
        // size of the window
        this.setSize(300, 300);
        // to not resize the window
        this.setResizable(false);
        JButton button = new JButton("Test Notif");
        button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              new PopUpMessage().makeUI("0679149407","azertyu iopqsdf ghjklmw xcvbnaze rtyu iopqsdfghjk lmwxcvbn azertyu iopqsdf ghjklmw xcvbnaze rtyu iopqsdfghjk lmwxcvbn azertyu iopqsdf ghjklmw xcvbnaze rtyu iopqsdfghjk lmwxcvbn azertyu iopqsdf ghjklmw xcvbnaze rtyu iopqsdfghjk lmwxcvbn azertyu iopqsdf ghjklmw xcvbnaze rtyu iopqsdfghjk lmwxcvbn");
              //new PartialFrame().makeUI("0679149407","azertyu iopqsdf ghjklmw xcvbnaze rtyu iopqsdfghjk");
          }
        });
        this.getContentPane().add(button);
        // close safely the frame
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // put the window on the center of the display
        this.setLocationRelativeTo(null);
        // set the window visible
        this.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            new MainWindow();
        } catch (IOException e) {
            e.printStackTrace();
        }

      }
}

PopUpMessage.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class PopUpMessage {

    private int height = 0;
    private Rectangle screenRect = GraphicsEnvironment
            .getLocalGraphicsEnvironment().getMaximumWindowBounds();
    private JPanel panel = new JPanel();
    private JLabel text;
    private JLabel messageSMS;
    private String messageFull;
    private static JTextArea textArea = new JTextArea();
    final JButton plus = new JButton("Plus");
    final JButton reply = new JButton("Reply");
    final JButton close = new JButton("Exit");
    final JButton send = new JButton("Send");
    private int secondClick = 0;
    private static JScrollPane scrollPaneTextArea = new JScrollPane(textArea);
    private Dimension panelDimension = new Dimension(300, 100);
    private JDialog dialog = new JDialog() {

        /**
     * 
     */
        private static final long serialVersionUID = 1L;

        @Override
        public void paint(Graphics g) {
            g.setClip(0, 0, getWidth(), height);
            super.paint(g);
        }
    };

    private Timer timer = new Timer(1, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            height += 5;
            if (height == dialog.getHeight()) {
                timer.stop();
            }
            dialog.setLocation(screenRect.width - dialog.getWidth(),
                    screenRect.height - height);
            dialog.repaint();
        }
    });

    private Timer timer2 = new Timer(1, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            height -= 5;
            if (height == 0) {
                timer.stop();
                dialog.dispose();
            }
            dialog.setLocation(screenRect.width - dialog.getWidth(),
                    screenRect.height - height);
            dialog.repaint();
        }
    });

    public void makeUI(String number, String message) {
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setUndecorated(true);
        dialog.setBackground(new Color(1, 0, 0, 0));
        panel.setPreferredSize(panelDimension);
        panel.setBackground(new Color(22, 83, 206, 200));
        panel.setLayout(null);
        dialog.setContentPane(panel);

        close.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                timer2.setInitialDelay(0);
                timer2.start();
            }
        });
        close.setBounds(210, 70, 90, 25);

        reply.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (secondClick == 0) {
                    textArea.setColumns(20);
                    textArea.setLineWrap(true);
                    textArea.setRows(5);
                    textArea.setWrapStyleWord(true);
                    scrollPaneTextArea.setBounds((panel.getWidth()-280)/2, messageSMS.getY()+messageSMS.getHeight(), 280, 90);
                    panel.add(scrollPaneTextArea);
                    height = dialog.getHeight() + scrollPaneTextArea.getHeight();
                    panel.setPreferredSize(new Dimension(panel.getWidth(),
                            panel.getHeight() + scrollPaneTextArea.getHeight()));
                    dialog.setLocation(dialog.getX(), dialog.getY() - scrollPaneTextArea.getHeight());
                    plus.setBounds(plus.getX(), plus.getY()+scrollPaneTextArea.getHeight(), plus.getWidth(), plus.getHeight());
                    close.setBounds(close.getX(), close.getY()+scrollPaneTextArea.getHeight(), close.getWidth(), close.getHeight());
                    send.setBounds(0, close.getY(), close.getWidth(), close.getHeight());
                    panel.remove(reply);
                    panel.add(send);
                    dialog.pack();
                }
            }
        });
        reply.setBounds(0, 70, 90, 25);

        plus.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (secondClick == 0) {
                    FontMetrics fm = panel.getFontMetrics(messageSMS.getFont());
                    int sizeMessage = fm.stringWidth(messageSMS.getText());
                    int decallage = (sizeMessage / 90) * 15;
                    height = dialog.getHeight() + decallage - fm.getHeight()*3;
                    panel.setPreferredSize(new Dimension(panel.getWidth(),
                            panel.getHeight() + decallage - fm.getHeight()*3));
                    messageSMS.setText("<html><P ALIGN=\"JUSTIFY\">"
                            + messageFull);
                    dialog.setLocation(dialog.getX(), dialog.getY() - decallage
                            + fm.getHeight()*3);
                    close.setBounds(close.getX(), close.getY() + decallage - fm.getHeight()*3, close.getWidth(), close.getHeight());
                    send.setBounds(send.getX(), send.getY() + decallage - fm.getHeight()*3, send.getWidth(), send.getHeight());

                    reply.setBounds(reply.getX(), reply.getY() + decallage - fm.getHeight()*3, reply.getWidth(), reply.getHeight());
                    panel.remove(plus);
                    scrollPaneTextArea.setBounds(scrollPaneTextArea.getX(), scrollPaneTextArea.getY()+ decallage - fm.getHeight()*3, scrollPaneTextArea.getWidth(), scrollPaneTextArea.getHeight());
                    messageSMS.setBounds(messageSMS.getX(), messageSMS.getY(), messageSMS.getWidth(), decallage);
                    dialog.pack();
                }
            }
        });
        plus.setBounds(105, 70, 90, 25);

        send.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                timer2.setInitialDelay(0);
                timer2.start();
            }
        });

        messageFull = message;
        text = new JLabel("New message from :  " + number);
        FontMetrics fm = panel.getFontMetrics(text.getFont());
        messageSMS = new JLabel("<html><center>" + message + "</center></html>");
        int textSize = fm.stringWidth(text.getText());

        text.setBounds((panelDimension.width - textSize) / 2, 0, textSize, 25);

        textSize = fm.stringWidth(messageSMS.getText());
        if (textSize > 810) {
            textSize = 290;
        }
        messageSMS.setBounds((panelDimension.width - textSize) / 2, 20,
                textSize, fm.getHeight()*3);

        panel.add(close);
        panel.add(reply);
        if (fm.stringWidth(messageSMS.getText()) > 870) {
            int sizeChar = fm.stringWidth(messageSMS.getText())
                    / messageSMS.getText().length();
            int allowedChar = 870 / sizeChar;
            String temp = message.substring(0, allowedChar - 33) + "...";
            messageSMS.setText("<html><center>" + temp + "</center></html>");
            panel.add(plus);
        }
        panel.add(messageSMS);
        panel.add(text);

        dialog.pack();
        dialog.setVisible(true);
        timer.setInitialDelay(0);
        timer.start();
    }
}