Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从底部移动动画JDialog_Java_Swing_Animation_Jframe_Jdialog - Fatal编程技术网

Java 从底部移动动画JDialog

Java 从底部移动动画JDialog,java,swing,animation,jframe,jdialog,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 jav

我已经为我的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);
        }
    }

}
import javax.swing.JDialog;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.FontMetrics;
导入java.awt.GraphicsEnvironment;
导入java.awt.Point;
导入java.awt.Rectangle;
导入java.awt.Toolkit;
导入java.awt.event.ActionListener;
导入javax.swing.JPanel;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JButton;
导入javax.swing.Timer;
导入java.awt.event.ActionEvent;
导入java.util.concurrent.TimeUnit;
公共类CustomDialogMessage扩展JDialog实现ActionListener{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
private JPanel myPanel=null;
私有JButton closeButton=null;
私有JButton answerButton=null;
私有JButton plusButton=null;
私有布尔答案=false;
公共布尔getAnswer(){return answer;}
公共int对话宽度=300;
公共高度=100;
String textHeader=“您收到的新消息来自:”;
公共CustomDialogMessage(JFrame框架、布尔模式、字符串myNumero、字符串myMessage){
超级(框架,模态);
未装饰的设置(真实);
挫折背景(新颜色(82,82,82175));
最终维度screenSize=Toolkit.getDefaultToolkit().getScreenSize();
矩形winSize=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
final int taskBarHeight=screenSize.height-winSize.height;
myPanel=newjpanel();
myPanel.setPreferredSize(新维度(dialogWidth、dialogHeight));
myPanel.setBackground(新颜色(1,0,0,0));
myPanel.setLayout(空);
getContentPane().add(myPanel);
JLabel头=新的JLabel(textHeader);
FontMetrics fm=header.getFontMetrics(header.getFont());
stringWidth(“您收到了一条新消息!”);
header.setBounds((dialogWidth-fm.stringWidth(textHeader))/2,0,200,30);
添加(标题);
closeButton=新按钮(“关闭”);
closeButton.addActionListener(此);
关闭按钮.立根(210,75,90,25);
添加(关闭按钮);
answerButton=新按钮(“回答”);
answerButton.addActionListener(此);
回答按钮.立根(0,75,90,25);
myPanel.add(应答按钮);
plusButton=新的JButton(“Plus”);
plusButton.addActionListener(这个);
加钮扣。后退(105,75,90,25);
myPanel.add(plusButton);
包装();
设置位置(屏幕大小.宽度对话框宽度,屏幕大小.高度对话框高度taskBarHeight);
setVisible(真);
}
公共动画(){
维度screenSize=Toolkit.getDefaultToolkit().getScreenSize();
矩形winSize=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
int taskBarHeight=screenSize.height-winSize.height;
int i=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();
    }
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.FontMetrics;
导入java.awt.Graphics;
导入java.awt.GraphicsEnvironment;
导入java.awt.Rectangle;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
公共类弹出消息{
私有整数高度=0;
私有矩形screenRect=GraphicsEnvironment
.getLocalGraphicsEnvironment().getMaximumWindowBounds();
private JPanel panel=new JPanel();
私有JLabel文本;
专用JLabel消息;
私有字符串messageFull;
私有静态JTextArea textArea=新JTextArea();
最终JButton plus=新JButton(“plus”);
最终JButton回复=新JButton(“回复”);
最终按钮关闭=新按钮(“退出”);
最终JButton发送=新JButton(“发送”);
私有int secondClick=0;
私有静态JScrollPane scrollPaneTextArea=新JScrollPane(textArea);
私有维度panelDimension=新维度(300100);
私有JDialog dialog=newjdialog(){
/**
* 
*/
私有静态最终长serialVersionUID=1L;
@凌驾
公共空间涂料(图g){
g、 setClip(0,0,getWidth(),height);
超级油漆(g);
}
};
私有计时器=新计时器(1,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
高度+=5;
if(height==dialog.getHeight()){
timer.stop();
}
dialog.setLocation(screenRect.width-dialog.getWidth(),
screenRect.height-高度);
dialog.repaint();
}
});
私有定时器timer2=新定时器(1,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
高度-=5;
如果(高度==0){
timer.stop();
dialog.dispose();
}
dialog.setLocation(screenRect.width-dialog.getWidth(),
screenRect.height-高度);
dialog.repaint();
}
});
公共void makeUI(字符串编号、字符串消息){
setDefaultCloseOperation(JDialog.DISPOSE\u ON\u CLOSE);
对话框。设置未装饰(true);
setBackground(新颜色(1,0,0,0));
面板。设置首选尺寸(面板尺寸);
面板.立根背景(新颜色(22,83,206,200));
panel.setLayout(空);
对话框.setContentPane(面板);
close.addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(ActionEve)