简体   繁体   English

Java切换按钮

[英]Java Switch Button

I have a Swing JFrame panel with 2 text fields and a single button. 我有一个带有2个文本字段和一个按钮的Swing JFrame面板。 I need the button to be able to trigger a change in the text in the different text fields (specifically, from "On" to "Off"). 我需要该按钮才能触发不同文本字段中的文本更改(特别是从“打开”到“关闭”)。 The complicating factor is I need it to only change the text of the last text area that was clicked. 复杂的因素是我只需要更改单击的最后一个文本区域的文本即可。

Below is my code so far. 下面是到目前为止的代码。 Notice that clicking the text fields themselves toggles the text, which I want. 注意,单击文本字段本身会切换我想要的文本。 But I also want the button to toggle the text - but only the text in the last text area the user clicked before clicking the button. 但是我还希望按钮切换文本-但仅单击用户在单击按钮之前单击的最后一个文本区域中的文本。 The way I have it now the button toggles both text fields, which I don't want. 现在,我拥有按钮的方式可以切换两个文本字段,这是我不需要的。

I suspect there may be a simple way to implement this that I'm overlooking. 我怀疑可能有一种简单的方法可以实现我所忽略的目标。

public class SwitchesTest extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SwitchesTest().displayJFrame();
            }
        });
    }

    private void displayJFrame() {
        JFrame frame = new JFrame("Switches");

        JButton button = new JButton("Switch");
        JTextArea switch1 = new JTextArea("On",1,3);
        JTextArea switch2 = new JTextArea("On",1,3);

        frame.getContentPane().setLayout(new GridBagLayout());

        frame.add(switch1);
        frame.add(switch2);
        frame.add(button);

        button.addActionListener(e -> {
            switch1.setText("On".equals(switch1.getText()) ? "Off" : "On");
            switch2.setText("On".equals(switch2.getText()) ? "Off" : "On");
        });

        switch1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                switch1.setText("On".equals(switch1.getText()) ? "Off" : "On");
            }
        });

        switch2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                switch2.setText("On".equals(switch2.getText()) ? "Off" : "On");
            }
        });

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Not tested, but just save the last text area clicked so you can toggle it later. 未经测试,但只需保存最后单击的文本区域,以便稍后进行切换。

Note: using "static" here is starting to be an anti-pattern, imo. 注意:这里使用“静态”开始成为一种反模式imo。 Consider switching this class to an object that is instantiated and initializes itself completely in its ctor. 考虑将此类切换到已实例化并完全在其ctor中初始化自身的对象。

public class SwitchesTest {

   static private JTextArea lastClicked;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                displayJFrame();
            }
        });
    }

    static void displayJFrame() {
        JFrame frame = new JFrame("Switches");

        JButton button = new JButton("Switch");
        JTextArea switch1 = new JTextArea("On",1,3);
        JTextArea switch2 = new JTextArea("On",1,3);

        frame.getContentPane().setLayout(new GridBagLayout());

        frame.add(switch1);
        frame.add(switch2);
        frame.add(button);

        button.addActionListener(e -> {
            if( lastClicked != null ) 
              lastClicked.setText("On".equals( lastClicked.getText()) ? "Off" : "On");
        });

        switch1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                switch1.setText("On".equals(switch1.getText()) ? "Off" : "On");
            lastClicked = switch1;
            }
        });

        switch2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                switch2.setText("On".equals(switch2.getText()) ? "Off" : "On");
            lastClicked = switch2;
            }
        });

But I also want the button to toggle the text - but only the text in the last text area the user clicked before clicking the button. 但是我还希望按钮切换文本-但仅单击用户在单击按钮之前单击的最后一个文本区域中的文本。

Text components already have an API to track which text component last had focus so there is no need to manage this on your own. 文本组件已经具有一个API来跟踪上一个焦点的文本组件,因此无需自己进行管理。

All you need to do us use a TextAction for the ActionListener. 您需要做的所有事情都为ActionListener使用TextAction Then you just use the getFocusedComponent() method of the TextAction to access the last text component that had focus. 然后,您只需使用TextAction的getFocusedComponent()方法即可访问最后一个具有焦点的文本组件。

Something like: 就像是:

button.addActionListener( new TextAction("Switch")
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        JTextComponent component = getFocusedComponent();

     if( component != null )
       component.setText("On".equals( component.getText()) ? "Off" : "On");
 }
});

Of course focus will be for any text component on the frame, so this approach will only work if you only have the two text components. 当然,重点将放在框架上的任何文本组件上,因此,仅当您只有两个文本组件时,此方法才有效。

But with this approach there is no need for any extra listeners. 但是通过这种方法,不需要任何额外的侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM