用于帧间按钮按下的InternalFrameListener?

时间:2015-01-21 19:26:44

标签: java swing jbutton

我需要更新框架并显示按下按钮的次数(按钮的文本要更新) 如果我可以使用actionPreformed()来锁定特定事件(按下按钮,或按下菜单项),那么我认为这应该有帮助......

问题:

  • 按下按钮时会创建更多帧

  • 现有的框架没有更新(我只希望有一个框架)

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class ButtonFrame implements InternalFrameListener, ActionListener
    {
    
    JFrame myFrame = null;
    private int clicked;
    final String F=("Clicked: "+clicked+" Times!");
    
    public static void main(String[] a) 
    {
        (new ButtonFrame()).test();
    
    }
    
    private void test() 
    {
        myFrame = new JFrame("Internal Frame with a Button");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setSize(400,400);
        myFrame.setContentPane(new JDesktopPane());
        JMenuBar Start_Bar = new JMenuBar();
        JMenu Start_Menu = new JMenu("Frame");
        JMenuItem Start_Item = new JMenuItem("Start");
        Start_Item.addActionListener(this);
        Start_Menu.add(Start_Item);
        Start_Bar.add(Start_Menu);
        myFrame.setJMenuBar(Start_Bar);
        myFrame.setVisible(true);
    
    }
    
    public void actionPerformed(ActionEvent Start_Item) 
    {
        JInternalFrame f = new JInternalFrame("Button Frame");
        f.setResizable(true);
        f.setClosable(false);
        f.setMaximizable(true);
        f.setIconifiable(true);
        f.setSize(200,200);
        f.setLocation(100,100);
        f.addInternalFrameListener(this);
        f.setVisible(true);
    
        Button objButton1;
        objButton1=new Button ("Clicked: "+clicked+" Times!");
        objButton1.setBounds(20,90,40,50);
        f.add(objButton1);
        objButton1.addActionListener(this);
    
        myFrame.getContentPane().add(f);
    }
    
    public void actionPreformed(ActionEvent objButton1)
    {
        clicked++;
    }
    
    public void internalFrameActivated(InternalFrameEvent e) 
    {
        System.out.println("Internal Button Ready");
    }
    public void internalFrameClosed(InternalFrameEvent e) 
    {
        System.out.println("Internal frame closed");
    }
    public void internalFrameClosing(InternalFrameEvent e) 
    {
        System.out.println("Internal frame closing");
    }
    public void internalFrameDeactivated(InternalFrameEvent e) 
    {
        System.out.println("Internal frame deactivated");
    }
    public void internalFrameDeiconified(InternalFrameEvent e) 
    { 
        System.out.println("Internal frame deiconified");
    }
    public void internalFrameIconified(InternalFrameEvent e) 
    {
        System.out.println("Internal frame iconified");
    }
    public void internalFrameOpened(InternalFrameEvent e) 
    {
        System.out.println("Internal frame opened");
    }
    }
    

1 个答案:

答案 0 :(得分:1)

根据您的描述,您的解决方案是尖叫隔离和代码责任分离,负责管理按钮的代码应该与管理框架和桌面窗格的代码分开

这样,您可以使用单独的ActionListener以及将功能隔离到按钮类的单个实例

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JDesktopPane dp = new JDesktopPane();

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JMenuBar Start_Bar = new JMenuBar();
                JMenu Start_Menu = new JMenu("Frame");
                JMenuItem Start_Item = new JMenuItem("Start");
                Start_Item.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JInternalFrame f = new JInternalFrame("Button", true, true, true, true);
                        f.setSize(200, 200);
                        f.setLocation(100, 100);
                        f.add(new ButtonPane());
                        f.setVisible(true);
                        dp.add(f);
                    }
                });
                Start_Menu.add(Start_Item);
                Start_Bar.add(Start_Menu);
                frame.setJMenuBar(Start_Bar);

                frame.add(dp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ButtonPane extends JPanel {

        private JButton button;
        private int count = 0;

        public ButtonPane() {
            setLayout(new GridBagLayout());
            button = new JButton("0");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    count++;
                    button.setText(Integer.toString(count));
                }
            });
            add(button);
        }

    }

}