java文本选择代码 java设置文本内容

java文本框格式的代码import java.awt.Color;
【java文本选择代码 java设置文本内容】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");
}
}
java代码问题求助,下面的代码是想要点击按钮后从本地选取文件,并将文件内容显示在文本区 。大家帮忙看看我编译时报了
p185_6.java:50: 警告: catch 子句无法访问
catch(IOException e2){System.out.println("文件读写错");}
改正后文件可以在页面内显示 , 就是位置有点问题 。
可以把add(jta,BorderLayout.SOUTH);改成add(jta,BorderLayout.CENTER);
如果想使用滚动条,可以把JTextArea放在JScrollPane里,然后再放在JFrame中 。
add(jta,BorderLayout.SOUTH);改成add(new JScrollPane(jta),BorderLayout.CENTER);
java中怎么编写多项选择题代码import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
public class Choose extends JFrame implements ItemListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Label l1, l2;
TextField t1, t2;
CheckboxGroup checkboxGroup = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox("QQ", checkboxGroup, false);
Checkbox checkbox2 = new Checkbox("MSN", checkboxGroup, false);
Checkbox checkbox3 = new Checkbox("ICQ", checkboxGroup, false);
Choice c;
public Choose() {
super("简单小程序");
this.setLayout(new FlowLayout());
l1 = new Label("选择你常用的软件:");
l2 = new Label("选择你喜欢的水果:");
checkbox1.addItemListener(this);
checkbox2.addItemListener(this);
checkbox3.addItemListener(this);
t1 = new TextField(20);
t2 = new TextField(20);
c = new Choice();
c.addItemListener(this);
c.add("苹果");
c.add("橘子");
c.add("香蕉");
c.add("梨子");
this.add(l1);
this.add(checkbox1);
this.add(checkbox2);
this.add(checkbox3);
this.add(t1);
this.add(l2);
this.add(c);
this.add(t2);
this.setSize(450, 200);
this.setVisible(true);
}
public static void main(String[] args) {
new Choose();
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == checkbox1) {
t1.setText("你常用的软件是:"checkbox1.getLabel());
}
if (e.getSource() == checkbox2) {
t1.setText("你常用的软件是:"checkbox2.getLabel());
}
if (e.getSource() == checkbox3) {
t1.setText("你常用的软件是:"checkbox3.getLabel());
}
t2.setText("你喜欢的水果是:"c.getSelectedItem());// 得到选中的下拉列表值
}
}
求java源代码 。使用swing或AWT 。实现功能:点击按钮,选择一个txt文本文件,并将txt中搞定了
package com.monubia;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visitfor details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Txt extends javax.swing.JFrame {
private JButton jButton_open;
private JTextArea jTextArea1;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Txt inst = new Txt();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public Txt() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jButton_open = new JButton();
getContentPane().add(jButton_open);
jButton_open.setText("Open");
jButton_open.setBounds(155, 114, 92, 49);
jButton_open.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
jButton_openMouseClicked(evt);
}
});
}
{
jTextArea1 = new JTextArea();
getContentPane().add(jTextArea1);
jTextArea1.setBounds(0, 0, 384, 262);
}
pack();
setSize(400, 300);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
private void jButton_openMouseClicked(MouseEvent evt) {
//点击了打开
JFileChooser open=new JFileChooser();
FileNameExtensionFilter txt= new FileNameExtensionFilter("Txt File", "txt");
open.setFileFilter(txt);
int ret=open.showOpenDialog(this);
if(ret==JFileChooser.APPROVE_OPTION)
{
jButton_open.setOpaque(false);
jButton_open.setVisible(false);
System.out.println(open.getSelectedFile().getAbsolutePath());
try {
BufferedReader br=new BufferedReader(new FileReader(open.getSelectedFile().getAbsolutePath()));
String line=null;
while((line=br.readLine())!=null)
{
jTextArea1.append(line "\n");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
关于java文本选择代码和java设置文本内容的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读