为什么我的Textarea.get文本不起作用

时间:2013-10-11 03:23:10

标签: java swing components jtextfield

我正在尝试制作集成了Java和Sqlite的发票软件。  我似乎遇到了Java Swing中get.Text功能的问题。现在我正在尝试将用户输入信息输入到文本字段中,为了变量名称,我将其命名为#。我试图从文本字段中提取信息,并使用名为Showinfo的自定义类将其输入到数组中。只需单击操作按钮即可完成所有这些操作。 任何有关此过程的帮助和提示都会受到赞赏,但我的主要关注点仍然是.getText函数失败

注意:我正在运行JDK 1.7.0_40。

package com.zetcode;
import java.applet.*;
import java.awt.*;
import java.awt.Component; 
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.SpringLayout;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
public class Test extends JFrame {

public Test() 
 {

setSize(600,600);
setTitle("InChoice");
setDefaultCloseOperation(EXIT_ON_CLOSE);
final Toolkit toolkit = getToolkit() ; 
Dimension size = toolkit.getScreenSize();
setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
Container contentpane= getContentPane();
SpringLayout layout = new SpringLayout();
 contentpane.setLayout(layout);

//文字开始

Component left1= new JLabel("Party Name");
final Component right1= new JTextField(25);
 Component left2= new JLabel("Date        ");
final Component right2= new JTextField(25);
Component left3= new JLabel("Time start        ");
final Component right3= new JTextField(25);
Component left4= new JLabel("Time start        ");
final Component right4= new JTextField(25);
Component left5= new JLabel("Location  ");
final Component right5= new JTextField(25);
Component left6= new JLabel("Price        ");
final Component right6= new JTextField(25);
    Component left7= new JLabel("Show Type        ");
final Component right7= new JTextField(25);

//Component left5= new JLabel("Left");
//Component right5= new JTextField(25);

 contentpane.add(left1);
 contentpane.add(left2);
 contentpane.add(left3);
 contentpane.add(left4);
 contentpane.add(left5);
 contentpane.add(left6);
 contentpane.add(left7);
    /*contentpane.add(left5);
     */
contentpane.add(right1);
contentpane.add(right2);
contentpane.add(right3);
contentpane.add(right4);
contentpane.add(right5);
contentpane.add(right6);
    contentpane.add(right7);
  /*contentpane.add(right5);
  */
layout.putConstraint(SpringLayout.WEST,left1,15,SpringLayout.WEST,contentpane);
layout.putConstraint(SpringLayout.NORTH,left1,25, SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.NORTH,right1,25, SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST, right1, 20, SpringLayout.EAST, left1);

layout.putConstraint(SpringLayout.WEST,left2,15, SpringLayout.WEST, contentpane);
layout.putConstraint(SpringLayout.NORTH,left2,50, SpringLayout.NORTH, contentpane);
layout.putConstraint(SpringLayout.NORTH,right2,50, SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST, right2, 20, SpringLayout.EAST, left2);
layout.putConstraint(SpringLayout.WEST, left3,15, SpringLayout.WEST, contentpane);
layout.putConstraint(SpringLayout.NORTH, left3,75, SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.NORTH, right3,75,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST, right3, 20, SpringLayout.EAST, left3);
layout.putConstraint(SpringLayout.WEST,left4, 15, SpringLayout.WEST, contentpane);
layout.putConstraint(SpringLayout.NORTH,left4,100,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.NORTH,right4,100,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST, right4, 20, SpringLayout.EAST, left4);
layout.putConstraint(SpringLayout.WEST,left5, 15,SpringLayout.WEST,contentpane);
layout.putConstraint(SpringLayout.NORTH,left5,125,SpringLayout.NORTH, contentpane);
layout.putConstraint(SpringLayout.NORTH,right5,125,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST, right5, 20, SpringLayout.EAST, left5);      
layout.putConstraint(SpringLayout.WEST,left6,15,SpringLayout.WEST,contentpane);
layout.putConstraint(SpringLayout.NORTH,left6,150,SpringLayout.NORTH, contentpane);
layout.putConstraint(SpringLayout.NORTH,right6,150,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST,right6,20, SpringLayout.EAST, left5);   
layout.putConstraint(SpringLayout.WEST, left7, 15, SpringLayout.WEST, contentpane);
layout.putConstraint(SpringLayout.NORTH,left7,175, SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.NORTH,right7,175,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST,right7,20,SpringLayout.EAST,left5);   

// TEXT FIELD END

//按钮

   final JPanel panel = new JPanel();
    getContentPane().add(panel);
   panel.setLayout(null);
   JButton action = new JButton("Action");     
   action.setBounds(300,250,80,30); 
   action.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent event) {
 String A= right1.getText(0,25);
 String B= right2;
 String C= right3;
 String D= right4;
String E= right5;
String F= right6;
String G= right7;
Showinfo Bloop = new Showinfo();
//System.out.println("blah");
}
});     
JButton close = new JButton("Close");   
 setBounds(150,250,80,30); 
 close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});

contentpane.add (close);    
contentpane.add (action);
layout.putConstraint(SpringLayout.WEST, action, 15, SpringLayout.WEST,contentpane);
layout.putConstraint(SpringLayout.NORTH,action,200,SpringLayout.NORTH,contentpane);

 layout.putConstraint(SpringLayout.NORTH, close,200, SpringLayout.NORTH, contentpane);
 layout.putConstraint(SpringLayout.WEST, close, 45, SpringLayout.EAST, left5);      

JTextField Potato= new JTextField(" ",25) ;
   Potato. setBounds(200,100,120,40);

   panel.add(Potato);   


JMenuBar menubar = new JMenuBar();
ImageIcon image = new ImageIcon("exit.png");
JMenu menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);


JMenuItem menuClose = new JMenuItem("Close",image);
menuClose.setMnemonic(KeyEvent.VK_C);
menuClose.setToolTipText("Exit Inchoice");
menuClose.addActionListener(new ActionListener( ){
    public void actionPerformed(ActionEvent event) {
        System.exit(0);

    }
});


JMenu port = new JMenu ("Import");
port.setMnemonic (KeyEvent.VK_M);

JMenuItem news= new JMenuItem("Import newsfeed list .....");
JMenuItem bookm= new JMenuItem("Import Bookmarks...");
JMenuItem mail= new JMenuItem("Import Mail ...") ;

menu.add (menuClose);   

menubar.add(menu);

setJMenuBar(menubar);
menubar.add(port);

port.add(news);

port.add(bookm);

port.add(mail);

}

   public static void main(String[] args) {
   Test cos = new Test();
    cos.setVisible(true);
   Test buttons = new Test();
   buttons.setVisible(true);

}

}

//我一直收到错误信息找不到符号方法getText()

1 个答案:

答案 0 :(得分:4)

您的问题是,所有JTextField都定义为Component,因此您只能访问Component类中定义的方法。没有Component#getText(int, int)之类的东西。确保所有声明类型都与您尝试分配给它的值的类型相匹配。例如:

final Component right1= new JTextField(25);

final JTextField right1= new JTextField(25);