为什么这个程序不会显示文本?

时间:2014-04-14 06:26:18

标签: java swing

还出现了一些其他问题,但程序运行时出现了很多错误,没有输出。一个是空指针。现在它还说“类型Talk必须实现继承的抽象方法ActionListener.actionPerformed(ActionEvent)”。它也说“void是变量actionPerformed的无效类型。”

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Talk extends JFrame implements ActionListener{
    JTextField first;
    JTextField second;
    JButton b = new JButton("ok");
    public Talk(){
        JPanel panel = new JPanel();
        add(panel);
        panel.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
        panel.add(new JLabel("First Name"));
        JTextField first = new JTextField(8);
        panel.add(first);
        panel.add(new JLabel("Last Name"));
        JTextField last = new JTextField(8);
        panel.add(last);
        panel.add(b);
        b.addActionListener(this);





public void actionPerformed(ActionEvent e) {
    if (e.getSource()==b){
        String data = first.getText();
        String data2 = second.getText();
        System.out.println("Welcome " + data + data2);
    }

}}

public static void main(String[] args){
    Talk frame = new Talk();
    frame.setTitle("Your Name");
    frame.setSize(500,500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}}

3 个答案:

答案 0 :(得分:2)

  1. 您已尝试在方法中声明方法
  2. 你试图在课堂外声明一种方法......
  3. 这两者是相关的......

        public Talk() { // Start of constructor
            //...still in method
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == b) {
                String data = first.getText();
                String data2 = second.getText();
                System.out.println("Welcome " + data + data2);
            }
    
        }
    } // end of constructor
    

    采用actionPerformed方法并将其移出构造函数...

    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.JPanel;
    import javax.swing.JTextField;
    
    public class Talk extends JFrame implements ActionListener {
    
        JTextField first;
        JTextField second;
        JButton b = new JButton("ok");
    
        public Talk() {
            JPanel panel = new JPanel();
            add(panel);
            panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
            panel.add(new JLabel("First Name"));
            JTextField first = new JTextField(8);
            panel.add(first);
            panel.add(new JLabel("Last Name"));
            JTextField last = new JTextField(8);
            panel.add(last);
            panel.add(b);
            b.addActionListener(this);
        }
    
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == b) {
                String data = first.getText();
                String data2 = second.getText();
                System.out.println("Welcome " + data + data2);
            }
        }
    
        public static void main(String[] args) {
            Talk frame = new Talk();
            frame.setTitle("Your Name");
            frame.setSize(500, 500);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    

    <强>更新

    你也会得到一个NullPointerException,因为你正在影响你的变量......

    // You declare these instance variables here...
    JTextField first;
    JTextField second;
    JButton b = new JButton("ok");
    
    public Talk() {
        But re-declare them here (as local variables)...
        //...
        JTextField first = new JTextField(8);
        //...
        JTextField last = new JTextField(8);
    

    这意味着当actionPerformed方法被调用时firstsecond}仍然是null

    删除本地重新声明......

    public Talk() {
        //...
        first = new JTextField(8);
        //...
        last = new JTextField(8);
    

答案 1 :(得分:1)

试试这个,

你得到null pointer exception,因为你没有使用你声明的类变量(改为使用局部变量)。

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Talk extends JFrame implements ActionListener{
    JTextField first;
    JTextField second;
    JTextField result;
    JLabel res=new JLabel("result");
    JButton b = new JButton("ok");
    public Talk(){
        JPanel panel = new JPanel();
        add(panel);
        panel.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
        panel.add(new JLabel("First Name"));
        first = new JTextField(8);
        panel.add(first);
        panel.add(new JLabel("Last Name"));
        second = new JTextField(8);
        panel.add(second);
        panel.add(b);
        b.addActionListener(this);

        panel.add(res);
        result = new JTextField(8);
        panel.add(result);


    }


public void actionPerformed(ActionEvent e) {
    if (e.getSource()==b){
        String data = first.getText();
        String data2 = second.getText();
        result.setText("Welcome " + data +" "+data2);
        //System.out.println("Welcome " + data + data2);
    }

}

public static void main(String[] args){
    Talk frame = new Talk();
    frame.setTitle("Your Name");
    frame.setSize(700,700);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

}

答案 2 :(得分:0)

这是你的解决方案..你已经在类级别声明变量第一和第二,但没有使用它们

 import javax.swing.*;

    import java.awt.*;
    import java.awt.event.*;

    public class Talk extends JFrame implements ActionListener{
        JTextField first;
        JTextField second;
        JButton b = new JButton("ok");
        public Talk(){
            JPanel panel = new JPanel();
            add(panel);
            panel.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
            panel.add(new JLabel("First Name"));
            first = new JTextField(8);
            panel.add(first);
            panel.add(new JLabel("Last Name"));
             second = new JTextField(8);
            panel.add(second);
            panel.add(b);
            b.addActionListener(this);
        }

    public static void main(String[] args){
        Talk frame = new Talk();
        frame.setTitle("Your Name");
        frame.setSize(500,500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        System.out.println("in actionPerformed1");
        if (arg0.getSource()==b){
            String data = first.getText();
            String data2 = second.getText();
            System.out.println("Welcome " + data + data2);
        }

    }}