寫了一個(gè)很簡單的案例,可以參考和修改
成都創(chuàng)新互聯(lián)公司2013年成立,先為秀英等服務(wù)建站,秀英等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為秀英企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
import?java.awt.BorderLayout;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JDialog;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JTextField;
public?class?FromeDemo?extends?JFrame{
JButton?jbutton;
public?FromeDemo()?{
jbutton?=?new?JButton("彈出2個(gè)文本框");
jbutton.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
JDialog?jd?=?new?JDialog();
jd.setBounds(320,?180,?260,?100);
jd.setTitle("彈出文本框");
jd.getContentPane().setLayout(new?GridLayout(2,?2));
jd.add(new?JLabel("文本框一"));
jd.add(new?JTextField(80));
jd.add(new?JLabel("文本框二"));
jd.add(new?JTextField(80));
jd.setModal(true);//確保彈出的窗口在其他窗口前面
jd.setVisible(true);
}
});
add(jbutton,BorderLayout.SOUTH);
setBounds(300,?100,?320,?320);
setTitle("測(cè)試");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public?static?void?main(String?args[])?{
new?FromeDemo();
}
}
package?com.demo;
import?java.awt.BorderLayout;
import?java.awt.Dimension;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.util.Timer;
import?java.util.TimerTask;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JTextField;
public?class?TimerTest?{
private?Timer?timer;
private?JTextField?field;
private?JButton?button;
private?boolean?flag?=?true;
public?TimerTest()?{
timer?=?new?Timer();
addview();
}
private?void?addview()?{
JFrame?frame?=?new?JFrame("Timer?test");
field?=?new?JTextField();
field.setPreferredSize(new?Dimension(0,?30));
button?=?new?JButton("start");
button.setPreferredSize(new?Dimension(100,?30));
button.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
if?(flag)?{
auto();
flag?=?false;
button.setText("stop");
}?else?{
timer.cancel();
flag?=?true;
button.setText("start");
}
}
});
frame.add(field,?BorderLayout.CENTER);
frame.add(button,?BorderLayout.EAST);
frame.setBounds(200,?200,?300,?60);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public?static?void?main(String[]?args)?{
new?TimerTest();
}
private?void?auto()?{
timer.schedule(new?TimerTask()?{
@Override
public?void?run()?{
int?num?=?Integer.parseInt(field.getText().trim());
num?+=?1;
field.setText(num?+?"");
auto();
}
},?1000);
}
}
只要文本框添加監(jiān)聽即可,用ActionListener即會(huì)在文本框中按下回車鍵時(shí)觸發(fā)事件。
下面是監(jiān)聽的代碼:
public void actionPerformed(ActionEvent e)
{
Object o=e.getSource();
if(o==text1) //文本框一號(hào)
{
text2.requestFocus();//文本框2號(hào),請(qǐng)求焦點(diǎn)
}
else if(o==text2)
{
text3.requestFocus();
}
}
程序如下:
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MyFrame extends JFrame implements ActionListener
{
private JButton button;
private JTextField textField;
private JLabel label;
public MyFrame()
{
button = new JButton("Hello world");
textField = new JTextField(15);
label = new JLabel("輸入內(nèi)容:");
setTitle("Simple-Frame");
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 500)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 300)/2
, 500, 300);
setLayout(new FlowLayout());
add(label);
add(textField);
add(button);
setResizable(false);
setVisible(true);
this.button.addActionListener(this);
}
public static void main(String[] args)
{
new MyFrame();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.button)
{
this.textField.setText("Hello World");
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
public class Ttex {
BufferedImage bi = new BufferedImage(300, 200, BufferedImage.TYPE_INT_RGB);
public Ttex(String text) {
Graphics gr = bi.createGraphics();
gr.setColor(Color.white);
gr.fillRect(0, 0, bi.getWidth(), bi.getHeight());
gr.setColor(Color.GRAY);
gr.drawString(text, 10, 10);
}
/**
* 根據(jù)地址來保存文件
*
* @param dir
* 文件地址
*/
private void Save(String dir) {
BufferedImage image = bi;
// 得到系統(tǒng)支持的圖片寫入器
Iterator it = ImageIO.getImageWritersBySuffix("jpg");
ImageWriter iw = null;
if (it.hasNext()) {
try {
ImageOutputStream imgOut = ImageIO
.createImageOutputStream(new File(dir));
iw = (ImageWriter) it.next();
iw.setOutput(imgOut);
IIOImage iioi = new IIOImage(image, null, null);
iw.write(iioi);
imgOut.close();
iw.dispose();
} catch (IOException e1) {
}
}
}
public static void main(String[] args) {
Ttex m = new Ttex("0123456789");
m.Save("D:/1.jpg");
}
}