如何为按钮添加动作?

时间:2016-01-06 13:10:24

标签: java swing jbutton actionlistener

我正在尝试输入JButon我创建的事件:

    JButton botton1=new JButton("welcom to my show db! lets start");
    botton1.setFont(new Font ("Eras Medium ITC",Font.BOLD,20));
    this.add(botton1);

    JPanel Basic_panel=new JPanel();
    Basic_panel.setName("SHOW DB ");
    Basic_panel.setBounds(x,y,width,hight);

    botton1.addActionListener(this) ;
}
          public void actionPerformed(ActionEvent e) {
                if (e.getSource()==botton1){

现在我要输入另一个JFrame我做的,让第一个消失。怎么样?

1 个答案:

答案 0 :(得分:3)

原始问题:

  

如何为按钮添加操作?

您可能需要查看How to write an Action Listener

关于你的第二个问题:

  

现在我要输入另一个JFrame,然后让第一个消失。怎么样?

请检查两种方法:)

选项1(推荐)

如果你想以正确的方式做,你应该使用@AndrewThompson在他CardLayout上面推荐的comment

我也看到你正在使用Null布局(因为setBounds()方法),你可能也想摆脱它,看{4}}和Why is it frowned upon to use a null layout in Swing?知道为什么,insted您应该使用Null Layout is Evil或它们的组合,如以下代码所示,基于@ AndrewThompson' s Layout Manager(与上面评论中链接的相同)但稍微修改后即可使用使用JFrame代替JOptionPane,所以也可以通过提升他的原始答案给予他信任!

这会产生以下输出:

answer enter image description here

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

class CardLayoutDemo {
    JButton button1, button2;

    CardLayoutDemo() {
        JFrame gui = new JFrame("CardLayoutDemo");
        button1 = new JButton("Go to pane 2");
        button2 = new JButton("Go to pane 1");
        JPanel pane1 = new JPanel();
        pane1.setLayout(new BoxLayout(pane1, BoxLayout.PAGE_AXIS));
        JPanel pane2 = new JPanel();
        pane2.setLayout(new BoxLayout(pane2, BoxLayout.PAGE_AXIS));
        final CardLayout cl = new CardLayout();
        final JPanel cards = new JPanel(cl);

        pane1.add(new JLabel("This is my pane 1"));
        pane1.add(button1);

        pane2.add(new JLabel("This is my pane 2"));
        pane2.add(button2);

        gui.add(cards);
        cards.add(pane1, "frame1");
        cards.add(pane2, "frame2");

        ActionListener al = new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
                if (ae.getSource() == button1) {
                    cl.show(cards, "frame2");
                } else if (ae.getSource() == button2) {
                    cl.show(cards, "frame1");
                }
            }
        };

        button1.addActionListener(al);
        button2.addActionListener(al);

        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.pack();
        gui.setVisible(true);
    }
    public static void main(String[] args) {
        new CardLayoutDemo();
    }
}

使用此选项,您只有1个JFrame,但您可以通过不同的视图进行更改,并且您不会在任务栏上使用多个窗口来惹恼用户。

此处还有一个提示:如果您要打开第二个JFrame以防止用户在第一个上做某事,您应该考虑使用enter image description here或第二个{{} {1}}只会包含一些您不希望在一直存在的信息(像弹出窗口一样)。

选项2(不推荐)

但如果你真的真的想要使用多个JFrame s(JOptionPane),你可以not recommended。当您正在调用要创建的新JFrame时。例如,以下代码生成此输出:

dispose() enter image description here

JFrame

在这种情况下,您可能需要考虑enter image description here,如果您想要关闭第二个import java.awt.*; import javax.swing.*; import java.awt.event.*; public class TwoJFrames { JFrame frame; JButton button; TwoJFrames() { frame = new JFrame("1st frame"); button = new JButton("Click me!"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new AnotherFrame(); frame.dispose(); } }); frame.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String args[]) { new TwoJFrames(); } class AnotherFrame { JFrame frame2; JLabel label; AnotherFrame() { frame2 = new JFrame("Second Frame"); label = new JLabel("This is my second frame"); frame2.add(label); frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame2.pack(); frame2.setVisible(true); } } }

,请回到之前的状态或重新打开此状态

我的上述两个代码都称为setVisible()或Runnable示例或Minimal, Complete, and Verifiable example (MCVE),当您的代码出现错误时,这些代码可以复制粘贴并看到与我相同的输出,这些示例非常方便,因为我们可以看到您的错误在哪里,或者能够更容易和/或更快地找到它们。

你应该考虑阅读我提供的所有链接(包括这些链接)以及将来的问题,以便做出像我上面所做的那样,这样你就可以避免混淆,并且你会得到更多,更快,更好的回应。