java界面程序 点击一个按钮,在文本框中出现你好
packagereproj2;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;...
package reproj2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class N19 extends JFrame implements ActionListener {
JButton jb;
JTextArea jta;
public static void main(String[] args) {
// TODO Auto-generated method stub
N19 n=new N19("N19");
}
public N19(String title){
this.setBounds(200, 300, 300, 400);
this.setTitle(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jb=new JButton("点我");
jb.setActionCommand("nh");
JTextArea jta=new JTextArea();
jb.addActionListener(this);
this.add(jb,BorderLayout.NORTH);
this.add(jta);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stu
if(e.getActionCommand().equals("nh"))
{
String input="你好";
jta.append(input+"\n");
}
}
}
//点下去没用没反应 展开
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class N19 extends JFrame implements ActionListener {
JButton jb;
JTextArea jta;
public static void main(String[] args) {
// TODO Auto-generated method stub
N19 n=new N19("N19");
}
public N19(String title){
this.setBounds(200, 300, 300, 400);
this.setTitle(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jb=new JButton("点我");
jb.setActionCommand("nh");
JTextArea jta=new JTextArea();
jb.addActionListener(this);
this.add(jb,BorderLayout.NORTH);
this.add(jta);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stu
if(e.getActionCommand().equals("nh"))
{
String input="你好";
jta.append(input+"\n");
}
}
}
//点下去没用没反应 展开
展开全部
你试一下下面的,已经改好:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class N19 extends JFrame implements ActionListener {
JButton jb;
JTextArea jta;
public static void main(String[] args) {
// TODO Auto-generated method stub
N19 n=new N19("N19");
}
public N19(String title){
this.setBounds(200, 300, 300, 400);
this.setTitle(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jb=new JButton("点我");
jb.setActionCommand("nh");
jta=new JTextArea();
jb.addActionListener(this);
this.add(jb,BorderLayout.NORTH);
this.add(jta,BorderLayout.CENTER);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stu
if(e.getActionCommand().equals("nh"))
{
String input = "你好";
jta.append(input+"\n");
}
}
}
原因是jta是类变量后,构造函数里面就不能是 JTextArea jta=new JTextArea();了
而应该是jta=new JTextArea();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询