Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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 JButton在JFrame中响应,但不在JPanel中响应_Java_Swing_Jbutton_Actionlistener - Fatal编程技术网

Java JButton在JFrame中响应,但不在JPanel中响应

Java JButton在JFrame中响应,但不在JPanel中响应,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,我是java新手,我正试图弄清楚动作监听器和按钮是如何工作的。我发现,如果直接将JButton放入JFrame对象中,就可以得到一个工作的JButton。但是如果我把它放在JFrame中的JPanel中,它不会响应。为什么呢 Main.java public class Main { private static Frame f = new Frame(); public static void main(String[] args) {} } public class Fra

我是java新手,我正试图弄清楚动作监听器和按钮是如何工作的。我发现,如果直接将JButton放入JFrame对象中,就可以得到一个工作的JButton。但是如果我把它放在JFrame中的JPanel中,它不会响应。为什么呢

Main.java

public class Main {
    private static Frame f = new Frame();
    public static void main(String[] args) {}
}
public class Frame extends JFrame {
    private final int WIDTH = 640, HEIGHT = 480;
    private Panel p = new Panel();

    Frame() {
        super("Java Program");   
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(WIDTH, HEIGHT);
        this.setLayout(null);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public void paint(Graphics g) {
        super.paint(g);
        p.paintComponent(g);
    }
}
public class Panel extends JPanel {
    JButton b = new JButton("Button");

    Panel() {
        b.setBounds(0,  0, 200, 100);
        add(b);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b.setText("Pressed");
            }
        });
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        b.paint(g);
    }
}
Frame.java

public class Main {
    private static Frame f = new Frame();
    public static void main(String[] args) {}
}
public class Frame extends JFrame {
    private final int WIDTH = 640, HEIGHT = 480;
    private Panel p = new Panel();

    Frame() {
        super("Java Program");   
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(WIDTH, HEIGHT);
        this.setLayout(null);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public void paint(Graphics g) {
        super.paint(g);
        p.paintComponent(g);
    }
}
public class Panel extends JPanel {
    JButton b = new JButton("Button");

    Panel() {
        b.setBounds(0,  0, 200, 100);
        add(b);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b.setText("Pressed");
            }
        });
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        b.paint(g);
    }
}
Panel.java

public class Main {
    private static Frame f = new Frame();
    public static void main(String[] args) {}
}
public class Frame extends JFrame {
    private final int WIDTH = 640, HEIGHT = 480;
    private Panel p = new Panel();

    Frame() {
        super("Java Program");   
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(WIDTH, HEIGHT);
        this.setLayout(null);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public void paint(Graphics g) {
        super.paint(g);
        p.paintComponent(g);
    }
}
public class Panel extends JPanel {
    JButton b = new JButton("Button");

    Panel() {
        b.setBounds(0,  0, 200, 100);
        add(b);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b.setText("Pressed");
            }
        });
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        b.paint(g);
    }
}

我不是一个秋千专家,所以我不能真正解释为什么它不起作用。好像一个没有反应的按钮被画在你的按钮上面。我对其进行了一些调整,以下是一些修改,以使其正常工作:

  • 将面板添加到框架:
    Add(p)
  • 删除
    this.setLayout(null)行,它似乎弄乱了框架
  • 要设置框架的大小,请使用setPreferredSize:
    this.setPreferredSize(新尺寸(宽度、高度))
  • 您还需要在框架构造函数的末尾调用
    pack()
  • 您需要从
    面板中删除
    b.paint(g)
    。paintComponent()
    ,这似乎就是绘制“无响应”按钮的原因(请参见答案末尾的图片)

  • 或者,您可以从框架中删除paint(),它只做JFrame的一个

以下是修改后的工作版本:

class Frame extends JFrame {
    private final int WIDTH = 640, HEIGHT = 480;
    private Panel p = new Panel();

    Frame() {
        super("Java Program");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        // add the panel to the frame
        add(p);

        pack();
    }

}

class Panel extends JPanel {
    JButton b = new JButton("Button");

    Panel() {
        b.setBounds(0,  0, 200, 100);
        add(b);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b.setText("Pressed");
            }
        });
    }


    // You can also get rid of this method, 
    // I just leave it here to show that I removed the b.paint(g) line
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
}
如果您将
b.paint(g)
留在
Panel.paintComponent()
中,下面是相同的代码显示的内容,您可以看到有两个按钮,角落中的一个不起作用


您从未将面板
p
添加到框架中。。。也不要对这样的元素显式调用
paintComponent
。Swing将自动执行此操作。并且不要覆盖框架的paint()或面板的paintComponent()。。没有理由这么做。阅读Swing教程中关于工作示例的部分,以开始学习。这些示例将展示一种更好的代码结构方式。@Obicere发现得很好<代码>此.setLayout(空)框架代码中的此部分在添加组件后也可能会导致问题。要更快获得更好的帮助,请发布或。顺便说一句,您从哪里获得有关如何编写Java代码的信息?我问你是因为你应该改变它。上述代码段中(可能)导致问题的代码行比严格且编写良好的代码行还要多。