ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 如何向按钮添加动作?

2019-09-29 17:00:16  阅读:368  来源: 互联网

标签:actionlistener java swing jbutton


我正在尝试为我创建的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,让第一个消失.怎么样?

解决方法:

对于您的原始问题:

How to add action to a button?

你可能想检查How to write an Action Listener.

对于你的第二个问题:

Now I want to enter another JFrame I made, and make the first disappear. How?

请检查两种方法:)

选项1(推荐)

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

我也看到你正在使用Null Layout(因为setBounds()方法),你可能也想摆脱它,请参阅Why is it frowned upon to use a null layout in Swing?Null Layout is Evil以了解原因,你应该使用Layout Manager或它们的组合,如图所示以下代码基于@AndrewThompson的answer(与上面的评论相同),但有点修改后可以使用JFrame而不是JOptionPane,所以通过提升他的原始答案给予他信任!

这产生以下输出:

enter image description here 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来防止用户在第一个上做某事,你应该考虑使用JOptionPane或者这个第二个JFrame只包含一些你不想要的信息.一直都在那里(像弹出一样).

选项2(不推荐)

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

enter image description here 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(),如果您想要返回上一个状态或在关闭第二个JFrame时重新打开此状态

上面的两个代码都被称为Minimal, Complete, and Verifiable example (MCVE)或Runnable Example或Short, Self Contained, Correct Example (SSCCE),这些代码可以复制粘贴并看到与我相同的输出,当您的代码出错时,这些示例非常方便,因为我们可以看到您的位置错误是或者能够更容易和/或更快地找到它们.

您应该考虑阅读我提供的所有链接(包括这些链接),并为将来的问题做出类似我上面所做的事情,这样你就可以避免混淆,你会得到更多,更快,更好的回复.

标签:actionlistener,java,swing,jbutton
来源: https://codeday.me/bug/20190929/1832411.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有