有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

组件之间的Java swing通信

我在Swing中连接组件时遇到了一些问题,这些组件可能会相互作用或产生动作流。我的计划是在按下按钮时禁用/启用JTextPane,然后输入数字,以便程序可以开始计算。到目前为止,我的处境是:

    private JPanel contentPane;
    protected JTextPane txtpnA;
    protected JTextPane txtpnB;
    protected JTextPane txtpnC;

     /* Button 'a' **/

    JButton btnA = new JButton("a");
    btnA.setBackground(Color.YELLOW);
    btnA.setBounds(47, 54, 89, 23);
    btnA.setActionCommand("a");
    btnA.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        } {
        }
    });
    contentPane.add(btnA);

   /* TextPane 'a' **/

   txtpnA = new JTextPane();
   txtpnA.setBounds(47, 88, 89, 20);
   contentPane.add(txtpnA);
   txtpnA.setBorder(BorderFactory.createLineBorder(Color.black));

方法如下:

   public void actionPerformed(ActionEvent event) {

    String command = event.getActionCommand();
    if(command.equals("a")) 
    {
        txtpnA.setEnabled(false);
    } else if(command.equals("b")) 
    {
        txtpnB.setEnabled(false);
    } else if(command.equals("c")) 
    {
        txtpnC.setEnabled(false);
    }
  }
}

我很难找到关于JComponents之间通信的文章。如果你能提供详细的信息来源,我将不胜感激


共 (2) 个答案

  1. # 1 楼答案

    如果我正确理解你的问题,你想做一个普通的ActionListener,它会被你的按钮调用

    您可以在类中创建一个内部私有类,其中包含这些按钮。并将该内部类的一个实例添加到button的ActionListener

    public class MyClass {
    
        //...//
    
        JButton a = new JButton("a");
        a.setActionCommand("a");
        a.setBounds(0, 0, 50, 50);
        a.addActionListener(new ActionButtons());
    
        //...//
    
        private class ActionButtons implements ActionListener{
    
            @Override
            public void actionPerformed(ActionEvent arg0) {
    
                String action = arg0.getActionCommand();
    
                if(action.equals("a")) {
    
                    System.out.println("a");
    
                }
    
                if(action.equals("b")) {
    
                    System.out.println("b");
    
                }
    
                if(action.equals("c")) {
    
                    System.out.println("c");
    
                }
            }
        }
    }
    

    注意:如果你想在多个类中使用ActionListener,你可以在其他类文件中将其声明为公共类

  2. # 2 楼答案

    我建议您创建一个新类,该类可以处理您对特定组件的请求,并且不使用匿名事件处理程序:

    public class ButtonHandler extends AbstractAction {
        private JComponent componentToDisable;
        public ButtonHandler(JComponent comp, String text) {
            super(text);
            componentToDisable = comp;
        }
        public void actionPerformed(ActionEvent event) {
           componentToDisable.setEnabled(false);
        }
    }
    

    如何使用它:

    /* TextPane 'a' **/
    txtpnA = new JTextPane();
    txtpnA.setBounds(47, 88, 89, 20);
    contentPane.add(txtpnA);
    txtpnA.setBorder(BorderFactory.createLineBorder(Color.black));
    
    JButton btnA = new JButton(new ButtonHandler(textpnA, "a"));
    btnA.setBackground(Color.YELLOW);
    btnA.setBounds(47, 54, 89, 23);
    contentPane.add(btnA);
    

    其他按钮的操作步骤相同

    JButton btnB = new JButton(new ButtonHandler(textpnB, "b"));
    JButton btnC = new JButton(new ButtonHandler(textpnC, "c"));
    

    最后但并非最不重要。安德鲁·汤普森已经提到:

    Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.