栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java Swing在运行时添加/删除jButton

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Swing在运行时添加/删除jButton

原始答案 总体上不错,但是在这种情况下做的却不同

为了跟踪添加的任意数量

JButtons
,您需要将它们保留在列表中。

因此,在创建新按钮之后,将侦听器添加到该按钮,然后将其添加到窗格中,然后需要将该新按钮保存在列表中。

这样,您可以跟踪已添加的所有按钮。

您还可以使用

Map<String, JButton>
将按钮名称映射到按钮的。

例:

private Map<String, JButton> dynamicButtons;public void addButton(String name) {    JButton b = new JButton(name);    b.addActionListener(someAction);    yourPanel.add(b);    dynamicButtons.put(name, b);    yourPanel.invalidate();}public void removeButton(String name) {    Button b = dynamicButtons.remove(name);    yourPanel.remove(b);    yourPanel.invalidate();}

以下是一个完整的类,可让您动态添加和删除按钮。这并不是您想要的,但它应该可以使您真正接近。

特定情况的代码:

import java.awt.BorderLayout;import java.awt.Component;import java.awt.Dimension;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.Jframe;import javax.swing.JOptionPane;import javax.swing.JPanel;public class Exampleframe extends Jframe {    private JButton add, remove;    private JPanel dynamicButtonPane, addRemovePane;    private boolean waitingForLocationClick;    public Exampleframe() {        super("Dynamic button example");        waitingForLocationClick = false;        add = new JButton("Add Button");        add.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {     addButton(JOptionPane  .showInputDialog("Name of the new button:")); }        });        remove = new JButton("Remove Button");        remove.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {     lookingToRemove = true; }        });        JPanel mainPane = new JPanel(new BorderLayout());        dynamicButtonPane = new JPanel();        dynamicButtonPane.setLayout(null);        dynamicButtonPane.setPreferredSize(new Dimension(300, 300));        addRemovePane = new JPanel();        addRemovePane.add(add);        addRemovePane.add(remove);        mainPane.add(dynamicButtonPane, BorderLayout.NORTH);        mainPane.add(addRemovePane, BorderLayout.SOUTH);        add(mainPane);        pack();        setDefaultCloseOperation(EXIT_ON_CLOSE);        setVisible(true);        dynamicButtonPane.addMouseListener(pointSelectorListener);    }    private JButton buttonToPlace;    public void addButton(String name) {        JButton b = new JButton(name);        b.setActionCommand(name);        b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {     if (lookingToRemove) {         if (e.getSource() instanceof JButton) {  dynamicButtonPane.remove((Component) e.getSource());  dynamicButtonPane.validate();  dynamicButtonPane.repaint();         }     } else         JOptionPane.showMessageDialog(Exampleframe.this, "This is " + e.getActionCommand()); }        });        waitingForLocationClick = true;        lookingToRemove = false;        buttonToPlace = b;    }    public void putButtonAtPoint(Point p) {        System.out.println("Placing a button at: " + p.toString());        dynamicButtonPane.add(buttonToPlace);        buttonToPlace.setBounds(new Rectangle(p, buttonToPlace     .getPreferredSize()));        dynamicButtonPane.validate();        buttonToPlace = null;        waitingForLocationClick = false;    }    private boolean lookingToRemove = false;    private final MouseListener pointSelectorListener = new MouseAdapter() {        @Override        public void mouseClicked(MouseEvent e) { if (waitingForLocationClick) {     putButtonAtPoint(e.getPoint()); } else {     System.out.println("Not in waiting state"); }        }    };    public static void main(String[] args) {        new Exampleframe();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号