java中上怎样使字一个一个的写在面板上去
3个回答
展开全部
用线程一个一个地写上去
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class TestFrame extends JFrame {
private JLabel jl;
public TestFrame() {
setName("一个一个字地显示");
setBounds(200,200,300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jl = new JLabel();
jl.setHorizontalTextPosition((int) TOP_ALIGNMENT);
jl.setBackground(Color.red);
jl.setSize(300, 300);
jl.setVerticalAlignment(SwingConstants.TOP);
add(jl);
setVisible(true);
}
private void setLabelText(String str){
jl.setText(str);
}
private class WriterWord implements Runnable{
private String words;
public WriterWord(String words) {
this.words = words;
}
@Override
public void run() {
try {
for (int i = 0; i < words.length(); i++) {
setLabelText(words.substring(0,i));
Thread.sleep(1000);//隔一秒设置label文本
}
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
TestFrame tf = new TestFrame();
WriterWord ww = tf.new WriterWord("一个一个地打字");
Thread t = new Thread(ww);
t.start();//运行线程
}
}
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class TestFrame extends JFrame {
private JLabel jl;
public TestFrame() {
setName("一个一个字地显示");
setBounds(200,200,300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jl = new JLabel();
jl.setHorizontalTextPosition((int) TOP_ALIGNMENT);
jl.setBackground(Color.red);
jl.setSize(300, 300);
jl.setVerticalAlignment(SwingConstants.TOP);
add(jl);
setVisible(true);
}
private void setLabelText(String str){
jl.setText(str);
}
private class WriterWord implements Runnable{
private String words;
public WriterWord(String words) {
this.words = words;
}
@Override
public void run() {
try {
for (int i = 0; i < words.length(); i++) {
setLabelText(words.substring(0,i));
Thread.sleep(1000);//隔一秒设置label文本
}
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
TestFrame tf = new TestFrame();
WriterWord ww = tf.new WriterWord("一个一个地打字");
Thread t = new Thread(ww);
t.start();//运行线程
}
}
展开全部
看一下下边的例子,在文本框中输入内容,按下按钮,就会在面板上写字。
----------------------------------------------------------------------
import java.awt.BorderLayout;
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;
public class App extends JFrame {
public App() {
getContentPane().setLayout(new BorderLayout(0, 0));
MyPanel panel = new MyPanel();
getContentPane().add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
}
class MyPanel extends JPanel implements ActionListener {
private JTextField textField;
private String str = "";
public MyPanel() {
setLayout(null);
textField = new JTextField();
textField.setBounds(12, 93, 170, 19);
add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("button");
btnNewButton.addActionListener(this);
btnNewButton.setBounds(194, 92, 91, 21);
add(btnNewButton);
}
public void actionPerformed(ActionEvent e) {
str = textField.getText();
updateUI();
}
public void paint(Graphics g) {
super.paint(g);
g.drawString(str, 10, 10);
}
}
----------------------------------------------------------------------
import java.awt.BorderLayout;
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;
public class App extends JFrame {
public App() {
getContentPane().setLayout(new BorderLayout(0, 0));
MyPanel panel = new MyPanel();
getContentPane().add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
}
class MyPanel extends JPanel implements ActionListener {
private JTextField textField;
private String str = "";
public MyPanel() {
setLayout(null);
textField = new JTextField();
textField.setBounds(12, 93, 170, 19);
add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("button");
btnNewButton.addActionListener(this);
btnNewButton.setBounds(194, 92, 91, 21);
add(btnNewButton);
}
public void actionPerformed(ActionEvent e) {
str = textField.getText();
updateUI();
}
public void paint(Graphics g) {
super.paint(g);
g.drawString(str, 10, 10);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用一个timer就可以了啊,过多少毫秒一触发,总共多少字,触发多少次后退出,都可以啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询