3个回答
推荐于2017-11-23
展开全部
给text对象和button对象加上事件响应方法即可,直接代码:
import java.awt.FlowLayout;
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.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class SwingTest extends JFrame {
// 确认按钮
private JButton b1 = new JButton("显示一把");
// 入力文本框
private JTextField txt = new JTextField(10);
// 显示文本域
private JTextArea area = new JTextArea(2, 20);
private JPanel jp = new JPanel();
// 事件类
class SwingTestListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
area.setText(txt.getText());
}
}
public SwingTest(String frameName) {
super(frameName);
// 按下按钮动作响应
b1.addActionListener(new SwingTestListener());
// 按下ENTER键响应
txt.addActionListener(new SwingTestListener());
setLayout(new FlowLayout());
add(txt);
add(b1);
jp.setBorder(new TitledBorder("TextArea"));
jp.add(area);
add(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300 , 150);
setLocation(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new SwingTest("SwingTest");
}
}
import java.awt.FlowLayout;
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.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class SwingTest extends JFrame {
// 确认按钮
private JButton b1 = new JButton("显示一把");
// 入力文本框
private JTextField txt = new JTextField(10);
// 显示文本域
private JTextArea area = new JTextArea(2, 20);
private JPanel jp = new JPanel();
// 事件类
class SwingTestListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
area.setText(txt.getText());
}
}
public SwingTest(String frameName) {
super(frameName);
// 按下按钮动作响应
b1.addActionListener(new SwingTestListener());
// 按下ENTER键响应
txt.addActionListener(new SwingTestListener());
setLayout(new FlowLayout());
add(txt);
add(b1);
jp.setBorder(new TitledBorder("TextArea"));
jp.add(area);
add(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300 , 150);
setLocation(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new SwingTest("SwingTest");
}
}
展开全部
给text对象和button对象加上事件响应方法即可,直接代码:
import java.awt.FlowLayout;
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.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class SwingTest extends JFrame {
// 确认按钮
private JButton b1 = new JButton("显示一把");
// 入力文本框
private JTextField txt = new JTextField(10);
// 显示文本域
private JTextArea area = new JTextArea(2, 20);
private JPanel jp = new JPanel();
// 事件类
class SwingTestListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
area.setText(txt.getText());
}
}
public SwingTest(String frameName) {
super(frameName);
// 按下按钮动作响应
b1.addActionListener(new SwingTestListener());
// 按下ENTER键响应
txt.addActionListener(new SwingTestListener());
setLayout(new FlowLayout());
add(txt);
add(b1);
jp.setBorder(new TitledBorder("TextArea"));
jp.add(area);
add(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300 , 150);
setLocation(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new SwingTest("SwingTest");
}
}
import java.awt.FlowLayout;
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.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class SwingTest extends JFrame {
// 确认按钮
private JButton b1 = new JButton("显示一把");
// 入力文本框
private JTextField txt = new JTextField(10);
// 显示文本域
private JTextArea area = new JTextArea(2, 20);
private JPanel jp = new JPanel();
// 事件类
class SwingTestListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
area.setText(txt.getText());
}
}
public SwingTest(String frameName) {
super(frameName);
// 按下按钮动作响应
b1.addActionListener(new SwingTestListener());
// 按下ENTER键响应
txt.addActionListener(new SwingTestListener());
setLayout(new FlowLayout());
add(txt);
add(b1);
jp.setBorder(new TitledBorder("TextArea"));
jp.add(area);
add(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300 , 150);
setLocation(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new SwingTest("SwingTest");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给按钮写一个触法器,点击的时候用文本框的getContent()不确定是不是这个反正就是获取内容的方法,再调用文本区域的相应set方法
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询