Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
除了按钮,一切都正常。图形用户界面Java_Java_Swing_Button_Layout - Fatal编程技术网

除了按钮,一切都正常。图形用户界面Java

除了按钮,一切都正常。图形用户界面Java,java,swing,button,layout,Java,Swing,Button,Layout,好的,我可以得到文本字段,普通文本,甚至图像来显示,但我不能得到一个按钮来显示。我不确定我做错了什么,因为我已经为其他人做了同样的步骤。任何帮助都很好,谢谢 package EventHandling2; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swi

好的,我可以得到文本字段,普通文本,甚至图像来显示,但我不能得到一个按钮来显示。我不确定我做错了什么,因为我已经为其他人做了同样的步骤。任何帮助都很好,谢谢

package EventHandling2;

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 EventHandling.GUITest;

public class EventMain extends JFrame{

    private JLabel label;
    private JButton button;

    public static void main(String[] args) {
        EventMain gui = new EventMain ();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
        //gui.setSize(600, 300);
        gui.setVisible(true);
        gui.setTitle("Button Test");
    }

    public void EventMain(){
        setLayout(new FlowLayout());

        button = new JButton ("click for text");
        add(button);

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

        Events e = new Events();
        button.addActionListener(e);
    }

    public class Events implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            label.setText("Now you can see words");
        }
    }
}
actionListener(e)包含一个小的控制结构错误:

public void actionPerformed(ActionEvent e) {
        label.setText("Now you can see words");
}
改为:

public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
           label.setText("Now you can see words");
        }
}
actionListener(e)包含一个小的控制结构错误:

public void actionPerformed(ActionEvent e) {
        label.setText("Now you can see words");
}
改为:

public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
           label.setText("Now you can see words");
        }
}

问题在于方法:
void EventMain()


构造函数没有返回类型。只需删除“void”。代码将很好地工作

问题在于方法:
void EventMain()


构造函数没有返回类型。只需删除“void”。代码将很好地工作

首先,您必须删除
EventMain
的构造函数中的
void
关键字。然后,创建
JPanel
并向其中添加组件,然后将
JPanel
添加到
JFrame.contentPane

以下代码应该可以工作:

public class EventMain extends JFrame {

    private final JLabel label;
    private final JButton button;

    public static void main(String[] args) {
        EventMain gui = new EventMain();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x
                                                            // close program
        gui.setSize(600, 300);
        gui.setTitle("Button Test");
        gui.setVisible(true);

    }

    public EventMain() {
        // setLayout(new FlowLayout());
        JPanel panel = new JPanel(new FlowLayout());
        button = new JButton("click for text");
        panel.add(button);

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

        Events e = new Events();
        button.addActionListener(e);

        this.getContentPane().add(panel);
    }

    public class Events implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            label.setText("Now you can see words");
        }
    }
}

首先,您必须删除
EventMain
的构造函数中的
void
关键字。然后,创建
JPanel
并向其中添加组件,然后将
JPanel
添加到
JFrame.contentPane

以下代码应该可以工作:

public class EventMain extends JFrame {

    private final JLabel label;
    private final JButton button;

    public static void main(String[] args) {
        EventMain gui = new EventMain();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x
                                                            // close program
        gui.setSize(600, 300);
        gui.setTitle("Button Test");
        gui.setVisible(true);

    }

    public EventMain() {
        // setLayout(new FlowLayout());
        JPanel panel = new JPanel(new FlowLayout());
        button = new JButton("click for text");
        panel.add(button);

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

        Events e = new Events();
        button.addActionListener(e);

        this.getContentPane().add(panel);
    }

    public class Events implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            label.setText("Now you can see words");
        }
    }
}

不要设置顶级容器的大小。而是布局内容&调用
pack()
(就在
setVisible(true)
之前)。若要更快地获得更好的帮助,请发布一个。我已经尝试过这样做,但按钮仍然没有显示“我已经尝试过这样做”。若要更快地获得更好的帮助,请发布一个。@AndrewThompson发生了什么事?除了未使用的
import EventHandling.GUITest之外,它简短、自包含、正确且可编译@johnchen902.。正确且可编译,除了..“不,没有例外..”。。它要么是SSCCE,要么不是。不要设置顶级容器的大小。而是布局内容&调用
pack()
(就在
setVisible(true)
之前)。若要更快地获得更好的帮助,请发布一个。我已经尝试过这样做,但按钮仍然没有显示“我已经尝试过这样做”。若要更快地获得更好的帮助,请发布一个。@AndrewThompson发生了什么事?除了未使用的
import EventHandling.GUITest之外,它简短、自包含、正确且可编译@johnchen902.。正确且可编译,除了..“不,没有例外..”。。它要么是SSCCE要么不是。我这样做了,但我仍然看不到任何按钮。谢谢你的帮忙。我做了,但我还是没看到纽扣。谢谢你的接球。是的,就是这样!谢谢。@LukeWorthing或致电
gui.EventMain()
Yep就是这样!谢谢。@LukeWorthing或致电
gui.EventMain()