0

我正在尝试制作一个集成 Java 和 Sqlite 的发票软件。我似乎对 Java Swing 中的 get.Text 功能有问题。现在我正试图让用户在文本字段中输入信息,为了变量名,我将其命名为 right#。我正在尝试从文本字段中提取信息并使用名为 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);   

// 文本字段结束

// 纽扣

   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()

4

1 回答 1

4

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

final Component right1= new JTextField(25);

final JTextField right1= new JTextField(25);
于 2013-10-11T03:29:39.430 回答