如何使用GUI使文本显示和消失?

时间:2017-03-23 11:40:03

标签: java user-interface

初学者。 我试图制作一个简单的GUI程序,当我点击一个按钮时,会出现一些东西,如果我再次点击,它将消失。 我在这里遇到麻烦,因为Eclipse告诉我我有一个例外,但我找不到它

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;



    public class event extends JFrame {


    private JLabel label;
    private JLabel label2;
    private JLabel label3;
    private JButton Load;
    private JButton Edit;
    private JButton Delete;
    private int x = 0, y = 0, z = 0;

    public event() {
        setLayout(new FlowLayout());

        Load = new JButton("Load");
        add(Load);

        Edit = new JButton("Edit");
        add(Edit);

        Delete = new JButton("Delete");
        add(Delete);

        label = new JLabel("");
        add(label);

        label2 = new JLabel("");
        add(label2);

        label3 = new JLabel("");
        add(label3);

        event e = new event();
        Load.addActionListener((ActionListener) e);

        event2 ev = new event2();
        Edit.addActionListener((ActionListener) ev);

        event3 eve = new event3();
        Delete.addActionListener((ActionListener) eve);

    }

    public class events implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            if (x == 0) {
                label.setText("Load file");
                x=1;
            } else if (x == 1) {
                label.setText("");
                x=0;
            }

        }
    }

    public class event2 implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            if (y == 0) {
                label2.setText("Edit file");
                y=1;
            } else if (y == 1) {
                label2.setText("");
                y=0;
            }
        }

    }

    public class event3 implements ActionListener {
        public void actionPerformed(ActionEvent eve) {
            if (z == 0) {
                label3.setText("Delete file");
                z=1;
            } else if (z == 1) {
                label3.setText("");
                z=0;
            }
        }
    }

    public static void main(String args[]) {

        event gui = new event();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.setSize(600, 200);
        gui.setTitle("BASIC GUI");
    }
}

2 个答案:

答案 0 :(得分:1)

在外部类“event”的构造函数中扩展Frame你意外地创建了同一个类的另一个对象(而不是“events”对象),这里是提到的行

event e = new event();
Load.addActionListener((ActionListener) e);

可能会导致一个主要问题:

  • 它创建了一个无限循环的对象创建,它调用另一个构造函数,它创建另一个对象......因此在某些时候,StackOverflowException

我重命名了外部类(Frame)GUITest,第一个ActionEventHandler事件1,依此类推等等

这段代码对我有用

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class GUITest extends JFrame {

  private JLabel label;
  private JLabel label2;
  private JLabel label3;
  private JButton Load;
  private JButton Edit;
  private JButton Delete;
  private int x = 0, y = 0, z = 0;


  public GUITest() {
    setLayout(new FlowLayout());

    Load = new JButton("Load");
    add(Load);

    Edit = new JButton("Edit");
    add(Edit);

    Delete = new JButton("Delete");
    add(Delete);

    label = new JLabel("");
    add(label);

    label2 = new JLabel("");
    add(label2);

    label3 = new JLabel("");
    add(label3);

    event1 e1 = new event1();
    Load.addActionListener((ActionListener) e1);

    event2 e2 = new event2();
    Edit.addActionListener((ActionListener) e2);

    event3 e3 = new event3();
    Delete.addActionListener((ActionListener) e3);

  }

  public class event1 implements ActionListener {
    public void actionPerformed(ActionEvent e) {

      if(x == 0) {
        label.setText("Load file");
        x = 1;
      } else if(x == 1) {
        label.setText("");
        x = 0;
      }

    }
  }

  public class event2 implements ActionListener {
    public void actionPerformed(ActionEvent ev) {
      if(y == 0) {
        label2.setText("Edit file");
        y = 1;
      } else if(y == 1) {
        label2.setText("");
        y = 0;
      }
    }

  }

  public class event3 implements ActionListener {
    public void actionPerformed(ActionEvent eve) {
      if(z == 0) {
        label3.setText("Delete file");
        z = 1;
      } else if(z == 1) {
        label3.setText("");
        z = 0;
      }
    }
  }


  public static void main(String args[]) {
    GUITest gui = new GUITest();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setVisible(true);
    gui.setSize(600, 200);
    gui.setTitle("BASIC GUI");
  }
}

答案 1 :(得分:1)

您最终处于无限循环中,因为您正在new event() - 类的构造函数中创建event。 constuctor-method调用创建一个新事件,它创建一个新事件,创建一个新事件,依此类推......这将产生StackOverflowError

这是我从控制台获得的:

Exception in thread "main" java.lang.StackOverflowError
at sun.awt.CGraphicsConfig.nativeGetBounds(Native Method)
at sun.awt.CGraphicsConfig.getBounds(CGraphicsConfig.java:56)
at java.awt.Window.init(Window.java:505)
at java.awt.Window.<init>(Window.java:537)
at java.awt.Frame.<init>(Frame.java:420)
at java.awt.Frame.<init>(Frame.java:385)
at javax.swing.JFrame.<init>(JFrame.java:189)
at uitest.event.<init>(event.java:19)
at uitest.event.<init>(event.java:40)
at uitest.event.<init>(event.java:40)
at uitest.event.<init>(event.java:40)
at uitest.event.<init>(event.java:40)
at uitest.event.<init>(event.java:40)
...

你看到event.<init>(构造函数)被反复调用。 我敢打赌你想使用ActionListener - 类events而不是事件类本身。

public event() {
    setLayout(new FlowLayout());

    Load = new JButton("Load");
    add(Load);

    Edit = new JButton("Edit");
    add(Edit);

    Delete = new JButton("Delete");
    add(Delete);

    label = new JLabel("");
    add(label);

    label2 = new JLabel("");
    add(label2);

    label3 = new JLabel("");
    add(label3);

    /*
     * Use events instead of event here.
     */
    events e = new events();
    Load.addActionListener((ActionListener) e);

    event2 ev = new event2();
    Edit.addActionListener((ActionListener) ev);

    event3 eve = new event3();
    Delete.addActionListener((ActionListener) eve);

}