重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.Color;
成都创新互联是一家专注于成都网站建设、成都网站设计与策划设计,太谷网站建设哪家好?成都创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:太谷等地区。太谷做网站价格咨询:028-86922220
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);
}
/**
* 根据地址来保存文件
*
* @param dir
* 文件地址
*/
private void Save(String dir) {
BufferedImage image = bi;
// 得到系统支持的图片写入器
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");
}
}
package image;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class Exam2 extends JFrame
{
private static final long serialVersionUID = 1L;
public Exam2 ()
{
setTitle ("Exam2");
setLayout (new BorderLayout ());
JMenuBar menuBar = new JMenuBar ();
JMenu[] menu = new JMenu[] { new JMenu ("文件") };
JMenuItem[] menuItem = new JMenuItem[] { new JMenuItem ("新建"), new JMenuItem ("打开"), new JMenuItem ("关闭"), new JMenuItem ("退出") };
menuBar.add (menu[0]);
for ( int i = 0; i 3; i++ )
{
menu[0].add (menuItem[i]);
}
menu[0].add (new JSeparator ());
menuItem[3].addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
System.exit (0);
}
});
menu[0].add (menuItem[3]);
setJMenuBar (menuBar);
JTextArea textArea = new JTextArea ();
JScrollPane scrollPane = new JScrollPane (textArea);
add (scrollPane, BorderLayout.CENTER);
setResizable (false);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setSize (300, 200);
setLocationRelativeTo (null);
setVisible (true);
}
public static void main ( String[] args )
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override
public void run ()
{
new Exam2 ();
}
});
}
}
用网格布局写了个简单的例子!~想美观可使用GridBagLayout布局方式。
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login extends JFrame implements ActionListener {
private static final long serialVersionUID = -2890322100614715274L;
//文本框
private JTextField jtf = null;
private JPasswordField jpf = null;
private JLabel jl = null;
public Login() {
// TODO Auto-generated constructor stub
//获取容器
Container c = this.getContentPane();
//设置布局方式, 网格布局 3行2列
c.setLayout(new GridLayout(3, 2));
jtf = new JTextField();
jpf = new JPasswordField();
JButton b = new JButton("显示");
// 添加按钮监听
b.addActionListener(this);
jl = new JLabel();
c.add(new JLabel("用户名:"));
c.add(jtf);
c.add(new JLabel("密 码:"));
c.add(jpf);
c.add(b);
c.add(jl);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* 事件监听方法.
* pTitle: actionPerformed/p
* pDescription: /p
* @param e
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//获取监听 组件的文本
String str = e.getActionCommand();
if("显示".equals(str)) {
jl.setText("用户名是:" + jtf.getText() + "\n密码是:" + new String(jpf.getPassword()));
}
}
public static void main(String[] args) {
new Login();
}
}
java实现多行文本框换行效果,使用java swing技术,示例如下:
package test;
import java.awt.GridLayout;
import java.awt.LayoutManager;
//import java.util.EmptyStackException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class test {
public static void main(String[] args) {
JFrame f = new JFrame("Welcome To Earth!");
JTextArea jt = new JTextArea(3, 20);
jt.setLineWrap(true);// 如果内容过长。自动换行
JScrollPane scr = new JScrollPane(jt,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// lab.setBounds(10, 10, 120, 20);
// jt.setBounds(130, 10, 150, 100);
JLabel lab = new JLabel("多行输入文本:");
f.setLayout(new GridLayout(2, 1));
f.add(lab);
f.add(scr);
// f.add(jt);
f.setSize(300, 150);
f.setLocation(300, 200);
f.setVisible(true);
}
}