代码中无法容纳按钮

发布于 2024-10-29 15:46:32 字数 800 浏览 2 评论 0原文

我想制作工作按钮并将其放入下面的代码中。我的问题是,无论我在哪里放置动作监听器或其他东西,它总是会给我带来奇怪的错误。我想让按钮 b1 在按下时打印文本。我真的很感激帮助。我只是初学者。

public class Simulator {

public static void main(String[] args) {

    boolean suc1,suc2,suc3,suc4,suc5,suc6,suc7;

        JFrame f = new JFrame("Simulator");
        f.setSize(500, 400);
        f.setResizable(false);
        f.setLayout(null);
        f.setVisible(true);
        JButton b1 = new JButton("Start");
        JButton b2 = new JButton("Clear");
        JButton b3 = new JButton("Find");
        JButton b4 = new JButton("Stop");
        b1.setBounds(20,335,80,25);
        b2.setBounds(110,335,80,25);
        b3.setBounds(200,335,80,25);
        b4.setBounds(395,335,80,25);
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);

}
}

I want to make working button and fit it in this code i have below. My problem is that anywhere i put actionlisteners or other stuff it always gives me weird errors. I want to make my button b1 to print text when i press it. I would really appreciate help. Im just beginner.

public class Simulator {

public static void main(String[] args) {

    boolean suc1,suc2,suc3,suc4,suc5,suc6,suc7;

        JFrame f = new JFrame("Simulator");
        f.setSize(500, 400);
        f.setResizable(false);
        f.setLayout(null);
        f.setVisible(true);
        JButton b1 = new JButton("Start");
        JButton b2 = new JButton("Clear");
        JButton b3 = new JButton("Find");
        JButton b4 = new JButton("Stop");
        b1.setBounds(20,335,80,25);
        b2.setBounds(110,335,80,25);
        b3.setBounds(200,335,80,25);
        b4.setBounds(395,335,80,25);
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-11-05 15:46:32
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Simulator {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                JFrame f = new JFrame("Simulator");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // Unnecessary, just set the preferred size of the
                // custom(?) component that renders the simulation,
                // use layouts, & pack() the frame when done.
                //f.setSize(500, 400);

                f.setResizable(false);

                // Bad idea.  Will cause trouble until fixed.
                //f.setLayout(null);

                // Play with the numbers for different effects.
                // If the edge of the buttons is not supposed to align
                // with the 'simulation' panel, borders can be added to
                // the elements to create that effect.

                JPanel gui = new JPanel(new BorderLayout(5,5));
                gui.setBorder( new EmptyBorder(10,30,10,30) );

                JPanel simulation = new JPanel();
                simulation.setPreferredSize(new Dimension(450,300));
                simulation.setBackground(Color.WHITE);
                gui.add(simulation, BorderLayout.CENTER);

                JPanel buttonPanel = new JPanel( new BorderLayout(50,5) );
                gui.add(buttonPanel, BorderLayout.SOUTH);

                JPanel westButtons = new JPanel(new GridLayout(1,0,10,10));

                JButton b1 = new JButton("Start");
                JButton b2 = new JButton("Clear");
                JButton b3 = new JButton("Find");
                JButton b4 = new JButton("Stop");

                westButtons.add(b1);
                westButtons.add(b2);
                westButtons.add(b3);

                buttonPanel.add(westButtons, BorderLayout.WEST);

                buttonPanel.add(b4, BorderLayout.EAST);

                f.setContentPane(gui);

                f.pack();
                f.setVisible(true);
            }
        };
        // ensure the UI is built on the EDT.
        SwingUtilities.invokeLater(r);
    }
}

GUI 截图

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Simulator {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                JFrame f = new JFrame("Simulator");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // Unnecessary, just set the preferred size of the
                // custom(?) component that renders the simulation,
                // use layouts, & pack() the frame when done.
                //f.setSize(500, 400);

                f.setResizable(false);

                // Bad idea.  Will cause trouble until fixed.
                //f.setLayout(null);

                // Play with the numbers for different effects.
                // If the edge of the buttons is not supposed to align
                // with the 'simulation' panel, borders can be added to
                // the elements to create that effect.

                JPanel gui = new JPanel(new BorderLayout(5,5));
                gui.setBorder( new EmptyBorder(10,30,10,30) );

                JPanel simulation = new JPanel();
                simulation.setPreferredSize(new Dimension(450,300));
                simulation.setBackground(Color.WHITE);
                gui.add(simulation, BorderLayout.CENTER);

                JPanel buttonPanel = new JPanel( new BorderLayout(50,5) );
                gui.add(buttonPanel, BorderLayout.SOUTH);

                JPanel westButtons = new JPanel(new GridLayout(1,0,10,10));

                JButton b1 = new JButton("Start");
                JButton b2 = new JButton("Clear");
                JButton b3 = new JButton("Find");
                JButton b4 = new JButton("Stop");

                westButtons.add(b1);
                westButtons.add(b2);
                westButtons.add(b3);

                buttonPanel.add(westButtons, BorderLayout.WEST);

                buttonPanel.add(b4, BorderLayout.EAST);

                f.setContentPane(gui);

                f.pack();
                f.setVisible(true);
            }
        };
        // ensure the UI is built on the EDT.
        SwingUtilities.invokeLater(r);
    }
}

Sceenshot of GUI

ゝ杯具 2024-11-05 15:46:32

尝试

    JButton b1 = new JButton("Start");
    b1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Button b1 was pressed");

        }
    });

编辑:

似乎对我来说效果很好。这是完整的 main() 方法

    public static void main(String[] args) {

        boolean suc1,suc2,suc3,suc4,suc5,suc6,suc7;

        JFrame f = new JFrame("Simulator");
        f.setSize(500, 400);
        f.setResizable(false);
        f.setLayout(null);
        f.setVisible(true);
        JButton b1 = new JButton("Start");
        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Button b1 was pressed");

            }
        });
        JButton b2 = new JButton("Clear");
        JButton b3 = new JButton("Find");
        JButton b4 = new JButton("Stop");
        b1.setBounds(20,335,80,25);
        b2.setBounds(110,335,80,25);
        b3.setBounds(200,335,80,25);
        b4.setBounds(395,335,80,25);
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);
    }

在此处输入图像描述

Try

    JButton b1 = new JButton("Start");
    b1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Button b1 was pressed");

        }
    });

EDIT:

Seems to work fine for me. Here's the full main() method

    public static void main(String[] args) {

        boolean suc1,suc2,suc3,suc4,suc5,suc6,suc7;

        JFrame f = new JFrame("Simulator");
        f.setSize(500, 400);
        f.setResizable(false);
        f.setLayout(null);
        f.setVisible(true);
        JButton b1 = new JButton("Start");
        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Button b1 was pressed");

            }
        });
        JButton b2 = new JButton("Clear");
        JButton b3 = new JButton("Find");
        JButton b4 = new JButton("Stop");
        b1.setBounds(20,335,80,25);
        b2.setBounds(110,335,80,25);
        b3.setBounds(200,335,80,25);
        b4.setBounds(395,335,80,25);
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);
    }

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文