如果您对JavaSwing中的字幕效果和javaswing字体感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解JavaSwing中的字幕效果的各种细节,并对javaswing字体进行深入的分析,
如果您对Java Swing中的字幕效果和java swing字体感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Java Swing中的字幕效果的各种细节,并对java swing字体进行深入的分析,此外还有关于java GUI编程(swing)之六swing中放置图片、JAVA Swing中的Google Map、Java Swing中的GoogleEarth、Java swing中的keyListener使用事例的实用技巧。
本文目录一览:- Java Swing中的字幕效果(java swing字体)
- java GUI编程(swing)之六swing中放置图片
- JAVA Swing中的Google Map
- Java Swing中的GoogleEarth
- Java swing中的keyListener使用事例
Java Swing中的字幕效果(java swing字体)
如何在Java Swing中实现字幕效果
答案1
小编典典这是使用的示例javax.swing.Timer
。
import java.awt.EventQueue;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Timer;/** @see http://stackoverflow.com/questions/3617326 */public class MarqueeTest { private void display() { JFrame f = new JFrame("MarqueeTest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String s = "Tomorrow, and tomorrow, and tomorrow, " + "creeps in this petty pace from day to day, " + "to the last syllable of recorded time; ... " + "It is a tale told by an idiot, full of " + "sound and fury signifying nothing."; MarqueePanel mp = new MarqueePanel(s, 32); f.add(mp); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); mp.start(); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new MarqueeTest().display(); } }); }}/** Side-scroll n characters of s. */class MarqueePanel extends JPanel implements ActionListener { private static final int RATE = 12; private final Timer timer = new Timer(1000 / RATE, this); private final JLabel label = new JLabel(); private final String s; private final int n; private int index; public MarqueePanel(String s, int n) { if (s == null || n < 1) { throw new IllegalArgumentException("Null string or n < 1"); } StringBuilder sb = new StringBuilder(n); for (int i = 0; i < n; i++) { sb.append('' ''); } this.s = sb + s + sb; this.n = n; label.setFont(new Font("Serif", Font.ITALIC, 36)); label.setText(sb.toString()); this.add(label); } public void start() { timer.start(); } public void stop() { timer.stop(); } @Override public void actionPerformed(ActionEvent e) { index++; if (index > s.length() - n) { index = 0; } label.setText(s.substring(index, index + n)); }}
java GUI编程(swing)之六swing中放置图片
JAVA Swing中的Google Map
这是我的代码。没有编译错误,但是我没有得到想要的输出:该地图未出现。我想在我的JPanel中打开Google静态地图,也想将其保存在本地驱动器上。这是我正在使用的代码。请指导我要去哪里。
try { String imageUrl = "http://maps.google.com/staticmap?center=40,26&zoom=1&size=150x112&maptype=satellite&key=ABQIAAAAgb5KEVTm54vkPcAkU9xOvBR30EG5jFWfUzfYJTWEkWk2p04CHxTGDNV791-cU95kOnweeZ0SsURYSA&format=jpg"; String destinationFile = "image.jpg"; str = destinationFile; URL url = new URL(imageUrl); InputStream is = url.openStream(); OutputStream os = new FileOutputStream(destinationFile); byte[] b = new byte[2048]; int length; while ((length = is.read(b)) != -1) { os.write(b, 0, length); } is.close(); os.close();} catch (IOException e) { e.printStackTrace(); System.exit(1);}lp2_1.setIcon(new ImageIcon((new ImageIcon("image.jpg")).getImage() .getScaledInstance(630, 600, java.awt.Image.SCALE_SMOOTH)));
答案1
小编典典我只是尝试了一下,它就像一个魅力:
import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URL;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;public class Snippet { public static void main(String[] args) throws IOException { JFrame test = new JFrame("Google Maps"); try { String imageUrl = "http://maps.google.com/staticmap?center=40,26&zoom=1&size=150x112&maptype=satellite&key=ABQIAAAAgb5KEVTm54vkPcAkU9xOvBR30EG5jFWfUzfYJTWEkWk2p04CHxTGDNV791-cU95kOnweeZ0SsURYSA&format=jpg"; String destinationFile = "image.jpg"; String str = destinationFile; URL url = new URL(imageUrl); InputStream is = url.openStream(); OutputStream os = new FileOutputStream(destinationFile); byte[] b = new byte[2048]; int length; while ((length = is.read(b)) != -1) { os.write(b, 0, length); } is.close(); os.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } test.add(new JLabel(new ImageIcon((new ImageIcon("image.jpg")).getImage().getScaledInstance(630, 600, java.awt.Image.SCALE_SMOOTH)))); test.setVisible(true); test.pack(); }}
lp2_1实际发生了什么,如果您没有在面板上得到地图,则可能是此控件的问题。
Java Swing中的GoogleEarth
是否可以在Java Swing应用程序中嵌入GoogleEarth视图?如果是这样,请您指点一下如何做的教程?
Java swing中的keyListener使用事例
最近在学习Java swing,写了一个域内聊天室,实现用户登录ip,端口号之后,进入聊天窗口聊天:
通过菜单条增加了几个功能,边框,字体,颜色和文件传输。风格里的样式都可以通过自己选择来设置。
介绍以上。
但在实现发送的功能时,想要增加默认回车发送消息功能:
原来的send按钮监听事件代码:
private class sendListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String str = editMsg.getText();
try {
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
if(str.equals(" ") || str.equals("") || str==null){
JOptionPane.showMessageDialog(null, "输入不能为空,请重新输入!", "提示",JOptionPane.OK_OPTION);
}else{
dos.writeUTF(str);
}
dos.flush();
editMsg.setText("");
} catch (IOException e1) {
e1.printStackTrace();
}
}
中间的用户不可编辑的消息显示区,增加一个键盘监听事件:
editMsg.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent key1) {
return;
}
@Override
public void keyReleased(KeyEvent key2) {
int code = key2.getKeyCode();
if(code==10){
editMsg.setText("");
}
return;
}
@Override
public void keyPressed(KeyEvent key3) {
int code=key3.getKeyCode();
if(code==10){
String str = editMsg.getText();
try {
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
if(str.equals(" ") || str.equals("") || str==null){
JOptionPane.showMessageDialog(null, "输入不能为空,请重新输入", "提示",JOptionPane.OK_OPTION);
}else{
dos.writeUTF(str);
}
dos.flush();
editMsg.setText("");
} catch (IOException e1) {
e1.printStackTrace();
}
}
return;
}
});
keyTyped为按键敲击事件,keyReleased为按键按下之后释放时的方法,keyPressed为按钮按下之后,释放之前的方法。
在keyPRessed方法中,每按一次按键时,先获取keyCode编码,确定回车的ASCII码值,再来判断是否发送消息。
思路是正确的,但在此出现了一个问题,每次回车发送消息之后,会有一个自动换行的操作,这也导致判断消息为空的代码失效。
解决办法:在keyReleased的方法中同样再判断一次回车的ASCII码值,再次清空输入框,问题得到解决。
总结:在键盘的监听中,主要通过判断按键的执行顺序以及按键的ASCII值来采取对应操作。
关于Java Swing中的字幕效果和java swing字体的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于java GUI编程(swing)之六swing中放置图片、JAVA Swing中的Google Map、Java Swing中的GoogleEarth、Java swing中的keyListener使用事例等相关内容,可以在本站寻找。
本文标签: