本篇文章给大家谈谈javaswing文本域,以及Java文本域对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、java swing中TextArea显示不出数据2、用java编写一个登录界面,用SWING组件3、java,swing,awt,图形用户界面:怎么搜索一个本地文本文件并将该文件内容在图形界面的文本区显示4、java的swing如何编写点击一次按键在JTextArea显示一行字5、java Swing 如何改变表格中字体的颜色
java swing中TextArea显示不出数据
1请问你有没有吧你的textArea 添加到JFrame 里面呢?
2 如果代码的逻辑没问题的话,最好是能打个断点调试一下这一行textArea.setText(logresult);,看下数据放进去了吗?
一般呢是先把JTextArea 添加到contentPane然后再把contentPane添加到Jframe里面,
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(90, 55, 225, 71);
textArea.setText(logresult);//
contentPane.add(textArea);
给你一段代码参考一下
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
//import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
public class MainThread extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainThread frame = new MainThread();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainThread() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton button = new JButton(“\u5F00\u59CB”);
button.setBounds(51, 182, 93, 23);
contentPane.add(button);
JButton button_1 = new JButton(“\u505C\u6B62”);
button_1.setBounds(278, 182, 93, 23);
contentPane.add(button_1);
JTextArea textArea = new JTextArea();
textArea.setBounds(90, 55, 225, 71);
contentPane.add(textArea);
}
}
用java编写一个登录界面,用SWING组件
import java.awt.Color;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
/**
* 一个简单的Swing窗口,输入内容单击“确定”按钮后,在文本域中显示输入的内容。
* 单击“取消”按钮,清空页面内容。
* @author yzg
*
*/
public class Register extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel nameLabel;
private JTextArea context;
private JTextField name;
private JLabel pLabel;
JList speciality;
JLabel mLabel;
String[] data = { “计算机”, “英语”, “机械”, “化工” };
ButtonGroup bg;
JRadioButton male;
JRadioButton female;
JLabel fLabel;
JCheckBox faverite1;
JCheckBox faverite2;
JCheckBox faverite3;
JCheckBox faverite4;
public Register(String title) {
super(title);
this.getContentPane().setLayout(null);
// 下面两行是取得屏幕的高度和宽度
double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setLocation(new Point((int) (lx / 2) – 150, (int) (ly / 2) – 200));// 设定窗口出现位置
this.setSize(340, 440);// 设定窗口大小
}
public void showWin() {
// 确保窗体有一个好的外观装饰
// setDefaultLookAndFeelDecorated(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
// 姓名
nameLabel = new JLabel(“姓名 :”);
nameLabel.setBounds(30, 10, 50, 25);
name = new JTextField();
name.setBounds(80, 10, 120, 20);
name.setBorder(BorderFactory.createLineBorder(Color.BLUE));
name.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
if (name.getText().length() 6) {
name.setText(name.getText().substring(0, 6));
}
}
});
// 专业 组合框
pLabel = new JLabel(“专业 :”);
pLabel.setBounds(30, 40, 50, 25);
speciality = new JList(data);
speciality.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
speciality.setBounds(80, 40, 80, 85);
speciality.setBorder(BorderFactory.createLineBorder(Color.GREEN));
mLabel = new JLabel(“性别 :”);
mLabel.setBounds(30, 130, 50, 25);
// 性别 单选框
bg = new ButtonGroup();
male = new JRadioButton(“男”);
female = new JRadioButton(“女”);
bg.add(male);
bg.add(female);
male.setBounds(80, 130, 60, 25);
female.setBounds(140, 130, 60, 25);
fLabel = new JLabel(“爱好 :”);
fLabel.setBounds(30, 160, 50, 25);
// 爱好 复选框
faverite1 = new JCheckBox(“音乐”);
faverite2 = new JCheckBox(“足球”);
faverite3 = new JCheckBox(“高尔夫”);
faverite4 = new JCheckBox(“游戏”);
faverite1.setBounds(80, 160, 60, 25);
faverite2.setBounds(140, 160, 60, 25);
faverite3.setBounds(200, 160, 65, 25);
faverite4.setBounds(265, 160, 60, 25);
// 内容 文本区域
JLabel conLabel = new JLabel(“输入的内容 :”);
conLabel.setBounds(30, 250, 90, 25);
context = new JTextArea();
context.setBounds(30, 270, 260, 100);
context.setBorder(BorderFactory.createLineBorder(Color.black));
// 确定按钮
JButton ok = new JButton(“确定”);
ok.setBounds(50, 190, 60, 25);
ok.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
StringBuffer sb = new StringBuffer();
sb.append(nameLabel.getText()).append(name.getText());
sb.append(“\n”);
int index = speciality.getSelectedIndex();
if (index = 0) {
sb.append(pLabel.getText()).append(data[index]);
} else {
sb.append(pLabel.getText());
}
sb.append(“\n”);
sb.append(mLabel.getText());
if (male.isSelected()) {
sb.append(“男”);
}
if (female.isSelected()) {
sb.append(“女”);
}
sb.append(“\n”);
sb.append(fLabel.getText());
if (faverite1.isSelected()) {
sb.append(“音乐 “);
}
if (faverite2.isSelected()) {
sb.append(“足球 “);
}
if (faverite3.isSelected()) {
sb.append(“高尔夫 “);
}
if (faverite4.isSelected()) {
sb.append(“游戏 “);
}
context.setText(sb.toString());
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
// 取消按钮
JButton cancel = new JButton(“取消”);
cancel.setBounds(120, 190, 60, 25);
cancel.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
name.setText(“”);
speciality.clearSelection();
if (faverite1.isSelected()) {
faverite1.setSelected(false);
}
if (faverite2.isSelected()) {
faverite2.setSelected(false);
}
if (faverite3.isSelected()) {
faverite3.setSelected(false);
}
if (faverite4.isSelected()) {
faverite4.setSelected(false);
}
context.setText(“”);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
this.getContentPane().add(nameLabel);
this.getContentPane().add(name);
this.getContentPane().add(pLabel);
this.getContentPane().add(speciality);
this.getContentPane().add(mLabel);
this.getContentPane().add(male);
this.getContentPane().add(female);
this.getContentPane().add(fLabel);
this.getContentPane().add(faverite1);
this.getContentPane().add(faverite2);
this.getContentPane().add(faverite3);
this.getContentPane().add(faverite4);
this.getContentPane().add(conLabel);
this.getContentPane().add(context);
this.getContentPane().add(ok);
this.getContentPane().add(cancel);
// this.pack();
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
Register reg = new Register(“Register”);
reg.showWin();
}
}
java,swing,awt,图形用户界面:怎么搜索一个本地文本文件并将该文件内容在图形界面的文本区显示
需要用到的知识点:
掌握IO知识,可以做到:
搜索文件,读取文本内容,写入文本内容
掌握swing/awt的事件处理机制,可以实现:
点击某个按钮, 会触发相应的事件处理
效果图
参考代码 : 注意填写3个方法
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//该窗口继承自JFrame. 实现了ActionListener接口
public class DemoFrame extends JFrame implements ActionListener {
JTextArea jta;//文本域
JButton jbRead, jbWrite;//按钮
public DemoFrame() {
jta = new JTextArea();
add(jta);
jbRead = new JButton(“搜索文件并显示内容”);
jbRead.addActionListener(this);
jbWrite = new JButton(“修改内容并保存”);
jbWrite.addActionListener(this);
JPanel jp = new JPanel();
jp.add(jbRead);
jp.add(jbWrite);
add(jp, BorderLayout.SOUTH);
setTitle(“窗口”);// 窗口标题
setSize(360, 285);// 窗口大小
setLocationRelativeTo(null);// 窗口居中
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 通常添加这行代码,点击窗口右下角的关闭时会结束程序
setVisible(true);
}
// main方法
public static void main(String[] args) {
new DemoFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
if (jb == jbRead) {
getFilePath();// 搜索文件
jta.setText(readFile(filePath));// 读取文件并显示
} else if (jb == jbWrite) {
if (filePath != null) {
String str = jta.getText();
writeFile(str, filePath);//写入文件
}else {
JOptionPane.showMessageDialog(null, “请先点击搜索文件并显示按钮”);
}
}
}
// 文件路径
private String filePath;
//需要完成方法1 搜索得到文件路径
private void getFilePath() {
filePath = “d://info.txt”;
}
//需要完成方法2 读取文件得到文本内容
private String readFile(String fp) {
return “这是读取得到的文本内容”;
}
// 需要完成方法3 把内容写入文件
private void writeFile(String str, String fp) {
JOptionPane.showMessageDialog(null, “保存成功”);
}
}
java的swing如何编写点击一次按键在JTextArea显示一行字
lz 你好
具体代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class ShowInfo extends JFrame{
private JTextArea show;
private JButton go;
//初始化窗口
public ShowInfo(){
super(“ShowInfo”);
setLayout(new FlowLayout(FlowLayout.CENTER, 50, 30));
show = new JTextArea(10,30);
go = new JButton(“GO”);
//添加按钮事件监听 当按下按钮时 响应
go.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
readInfo();
}
});
getContentPane().add(show);
getContentPane().add(go);
setSize(450,350);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(3);
}
//读取文件内容到JtextArea中
public void readInfo(){
try {
BufferedReader br = new BufferedReader(new FileReader(new File(“info.txt”)));
show.append(br.readLine()+”\n”);
br.close();
}
catch (Exception ex) {
}
}
public static void main (String[] args) {
new ShowInfo();
}
}
ps:记得在源代码的当前目录下建立一个info.txt的文档 文档内容: hello,world
运行效果:
希望能帮助你哈
java Swing 如何改变表格中字体的颜色
JTable是继承自JComponent这个类,所有继承自该类的都有一个方法叫:setForeground(Color r)
使用该方法即改变字体颜色。如你创建了一个JTable的对象table,那么使用:table.setForeground(Color.red); 即可把表格里的字体全部设置为红色。需要自定义颜色就这样:table.setForeground(new Color(r,g,b)); r,g,b分别是三原色,取值都在0-255之间
javaswing文本域的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java文本域、javaswing文本域的信息别忘了在本站进行查找喔。