在Java中怎么在文本域输入特定文字,实现单击一哈按钮,在另一个文本框内显示特定的内容?
这里是代码packageshape;importjavax.swing.*;importjavax.swing.event.*;importjavax.swing.tex...
这里是代码
package shape;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class frame extends JFrame {
JLabel lable1;
JTextArea text2;
JTextField text1;
JButton button;
/**
*
*/
private static final long serialVersionUID = 1L;
public void shapeSearch(boolean b){
this.setTitle("图形属性查询器");
this.setSize(400,250);
this.setResizable(b);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void imputDemo(){
JLabel lable1=new JLabel("图形名称:");
JTextArea text2=new JTextArea();
text2.setText(null);
text2.setLineWrap(true);
text2.setBounds(20, 20,300, 200);
JTextField text1=new JTextField(10);
JButton button = new JButton("查询");
JScrollPane scroll=new JScrollPane(text2);
FlowLayout seeText=new FlowLayout(FlowLayout.CENTER);
seeText.setHgap(20);
seeText.setVgap(60);
this.setLayout(seeText);
this.add(lable1);
this.add(text1);
this.add(button);
this.add(scroll);
ButtonHandle handle=new ButtonHandle();
button.addActionListener(handle);
DocumentWriter dm=new DocumentWriter();
Document doc=text1.getDocument();
doc.addDocumentListener(dm);
}
class ButtonHandle implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getActionCommand().equals(button)){
if(text1.getText()=="圆形"){
text2.setText("该圆形的半径是3\n"+"周长是18.84\n"+"面积是28.26");
}
else if(text1.getText()=="矩形"){
text2.setText("该矩形的长是5,宽是4\n"+"周长是18\n"+"面积是20");
text2.setVisible(true);
}
else if(text1.getText()=="三角形"){
text2.setText("该三角形的底边是3,高是4,斜边是5\n"+"周长是12\n"+"面积是6");
text2.setVisible(true);
}
}
}
}
class DocumentWriter implements DocumentListener{
public void changedUpdate(DocumentEvent e){}
public void insertUpdate(DocumentEvent e){}
public void removeUpdate(DocumentEvent e){}
}
public static void main(String[] args)throws Exception{
frame littleFrame =new frame();
littleFrame.shapeSearch(false);
littleFrame.imputDemo();
}
}
比如在文本域里输入三角形,点击查询按钮,下面的文本框没反应,考虑的好长时间都不知道问题出在哪里求大神帮忙 展开
package shape;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class frame extends JFrame {
JLabel lable1;
JTextArea text2;
JTextField text1;
JButton button;
/**
*
*/
private static final long serialVersionUID = 1L;
public void shapeSearch(boolean b){
this.setTitle("图形属性查询器");
this.setSize(400,250);
this.setResizable(b);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void imputDemo(){
JLabel lable1=new JLabel("图形名称:");
JTextArea text2=new JTextArea();
text2.setText(null);
text2.setLineWrap(true);
text2.setBounds(20, 20,300, 200);
JTextField text1=new JTextField(10);
JButton button = new JButton("查询");
JScrollPane scroll=new JScrollPane(text2);
FlowLayout seeText=new FlowLayout(FlowLayout.CENTER);
seeText.setHgap(20);
seeText.setVgap(60);
this.setLayout(seeText);
this.add(lable1);
this.add(text1);
this.add(button);
this.add(scroll);
ButtonHandle handle=new ButtonHandle();
button.addActionListener(handle);
DocumentWriter dm=new DocumentWriter();
Document doc=text1.getDocument();
doc.addDocumentListener(dm);
}
class ButtonHandle implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getActionCommand().equals(button)){
if(text1.getText()=="圆形"){
text2.setText("该圆形的半径是3\n"+"周长是18.84\n"+"面积是28.26");
}
else if(text1.getText()=="矩形"){
text2.setText("该矩形的长是5,宽是4\n"+"周长是18\n"+"面积是20");
text2.setVisible(true);
}
else if(text1.getText()=="三角形"){
text2.setText("该三角形的底边是3,高是4,斜边是5\n"+"周长是12\n"+"面积是6");
text2.setVisible(true);
}
}
}
}
class DocumentWriter implements DocumentListener{
public void changedUpdate(DocumentEvent e){}
public void insertUpdate(DocumentEvent e){}
public void removeUpdate(DocumentEvent e){}
}
public static void main(String[] args)throws Exception{
frame littleFrame =new frame();
littleFrame.shapeSearch(false);
littleFrame.imputDemo();
}
}
比如在文本域里输入三角形,点击查询按钮,下面的文本框没反应,考虑的好长时间都不知道问题出在哪里求大神帮忙 展开
1个回答
展开全部
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.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
public class frame extends JFrame
{
JLabel lable1;
JTextArea text2;
JTextField text1;
JButton button;
/**
*
*/
private static final long serialVersionUID = 1L;
public void shapeSearch ( boolean b )
{
this.setTitle ("图形属性查询器");
this.setSize (400, 250);
this.setResizable (b);
this.setVisible (true);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void imputDemo ()
{
JLabel lable1 = new JLabel ("图形名称:");
text2 = new JTextArea ();
text2.setText (null);
text2.setLineWrap (true);
text2.setBounds (20, 20, 300, 200);
text1 = new JTextField (10);
JButton button = new JButton ("查询");
JScrollPane scroll = new JScrollPane (text2);
FlowLayout seeText = new FlowLayout (FlowLayout.CENTER);
seeText.setHgap (20);
seeText.setVgap (60);
this.setLayout (seeText);
this.add (lable1);
this.add (text1);
this.add (button);
this.add (scroll);
ButtonHandle handle = new ButtonHandle ();
button.addActionListener (handle);
DocumentWriter dm = new DocumentWriter ();
Document doc = text1.getDocument ();
doc.addDocumentListener (dm);
this.setVisible (true);
}
class ButtonHandle implements ActionListener
{
public void actionPerformed ( ActionEvent event )
{
if (event.getActionCommand ().equals ("查询"))
{
if ("圆形".equals (text1.getText ()))
{
text2.setText ("该圆形的半径是3\n" + "周长是18.84\n" + "面积是28.26");
}
else if ("矩形".equals (text1.getText ()))
{
text2.setText ("该矩形的长是5,宽是4\n" + "周长是18\n" + "面积是20");
text2.setVisible (true);
}
else if ("三角形".equals (text1.getText ()))
{
text2.setText ("该三角形的底边是3,高是4,斜边是5\n" + "周长是12\n" + "面积是6");
text2.setVisible (true);
}
}
}
}
class DocumentWriter implements DocumentListener
{
public void changedUpdate ( DocumentEvent e )
{}
public void insertUpdate ( DocumentEvent e )
{}
public void removeUpdate ( DocumentEvent e )
{}
}
public static void main ( String[] args ) throws Exception
{
frame littleFrame = new frame ();
littleFrame.shapeSearch (false);
littleFrame.imputDemo ();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
public class frame extends JFrame
{
JLabel lable1;
JTextArea text2;
JTextField text1;
JButton button;
/**
*
*/
private static final long serialVersionUID = 1L;
public void shapeSearch ( boolean b )
{
this.setTitle ("图形属性查询器");
this.setSize (400, 250);
this.setResizable (b);
this.setVisible (true);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void imputDemo ()
{
JLabel lable1 = new JLabel ("图形名称:");
text2 = new JTextArea ();
text2.setText (null);
text2.setLineWrap (true);
text2.setBounds (20, 20, 300, 200);
text1 = new JTextField (10);
JButton button = new JButton ("查询");
JScrollPane scroll = new JScrollPane (text2);
FlowLayout seeText = new FlowLayout (FlowLayout.CENTER);
seeText.setHgap (20);
seeText.setVgap (60);
this.setLayout (seeText);
this.add (lable1);
this.add (text1);
this.add (button);
this.add (scroll);
ButtonHandle handle = new ButtonHandle ();
button.addActionListener (handle);
DocumentWriter dm = new DocumentWriter ();
Document doc = text1.getDocument ();
doc.addDocumentListener (dm);
this.setVisible (true);
}
class ButtonHandle implements ActionListener
{
public void actionPerformed ( ActionEvent event )
{
if (event.getActionCommand ().equals ("查询"))
{
if ("圆形".equals (text1.getText ()))
{
text2.setText ("该圆形的半径是3\n" + "周长是18.84\n" + "面积是28.26");
}
else if ("矩形".equals (text1.getText ()))
{
text2.setText ("该矩形的长是5,宽是4\n" + "周长是18\n" + "面积是20");
text2.setVisible (true);
}
else if ("三角形".equals (text1.getText ()))
{
text2.setText ("该三角形的底边是3,高是4,斜边是5\n" + "周长是12\n" + "面积是6");
text2.setVisible (true);
}
}
}
}
class DocumentWriter implements DocumentListener
{
public void changedUpdate ( DocumentEvent e )
{}
public void insertUpdate ( DocumentEvent e )
{}
public void removeUpdate ( DocumentEvent e )
{}
}
public static void main ( String[] args ) throws Exception
{
frame littleFrame = new frame ();
littleFrame.shapeSearch (false);
littleFrame.imputDemo ();
}
}
更多追问追答
追问
那个还是有问题,点击那个按钮没反应
追答
没反应么?你有没有复制粘贴全部我的代码呢?我试过了,可以的
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询