0
我已經爲我的JDialog寫了一個動畫,但是如果我在JButton的偵聽器中執行它,它可以工作,但是在我的構造器編號中。我嘗試使用線程和計時器,它不工作。從底部動畫移動JDialog
我的代碼:
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);
}
}
}
你永遠不會調用'動畫()'任何地方。 – supersam654
請詳細解釋一下,關於__您究竟計劃做什麼?__,__究竟發生了什麼?__和__究竟是什麼意思?__ –
如果您將動畫()放在JButton的偵聽器上,我們可以看到JDialog的移動一直到屏幕右側的角落。但我想要構造函數上的這個動畫,但如果我在構造函數上調用animation()它不起作用。當我創建我的JDialog時,我希望它出現在右下角並向上移動。有點像MSN通知。 – g3r4n