简体   繁体   English

需要帮助制作按钮中继信息

[英]Need help making button relay information

Okay, so I have made a GUI with some input boxes and a combo box. 好的,我制作了一个带有一些输入框和一个组合框的GUI。 I am wondering how I would go about having a listener know when the button is pressed then relay all the information in the imput boxes and combo box into different parts of the script... 我想知道如何让听众知道何时按下按钮,然后将输入框和组合框中的所有信息中继到脚本的不同部分...

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

    public class dogedice extends JFrame implements ActionListener {
        private static final long serialVersionUID = 1L;
        private JPanel contentPane;
        private JTextField textField;
        private JComboBox combo;

        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        dogedice frame = new dogedice();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        public dogedice() {
            setTitle("DogeDice Bot");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);

            JPanel panel = new JPanel();
            contentPane.add(panel, BorderLayout.WEST);
            GridBagLayout gbl_panel = new GridBagLayout();
            gbl_panel.columnWidths = new int[]{0, 0};
            gbl_panel.rowHeights = new int[]{0, 0};
            gbl_panel.columnWeights = new double[]{0.0, 1.0};
            gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
            panel.setLayout(gbl_panel);

            //Every new Label however needs every part that says "user" or on the Password: "pass" changed to something unique.
            JLabel userTag = new JLabel("Username:");
            GridBagConstraints gbc_userTag = new GridBagConstraints();
            gbc_userTag.insets = new Insets(0, 0, 0, 5);
            gbc_userTag.anchor = GridBagConstraints.EAST;
            gbc_userTag.gridx = 0;//Here are your x + y coords
            gbc_userTag.gridy = 0;//Adding to x moves left, adding to y moves down
            panel.add(userTag, gbc_userTag);

            //Every new textfield needs only the * part to change for it to be valid. (gbc_* =)
            textField = new JTextField();
            GridBagConstraints gbc_Username = new GridBagConstraints();
            gbc_textField.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField.gridx = 1;
            gbc_textField.gridy = 0;
            panel.add(textField, gbc_textField);
            textField.setColumns(10);

            JLabel startTag = new JLabel("Starting Bid:");
            GridBagConstraints gbc_startTag = new GridBagConstraints();
            gbc_startTag.insets = new Insets(0, 0, 0, 5);
            gbc_startTag.anchor = GridBagConstraints.EAST;
            gbc_startTag.gridx = 0;
            gbc_startTag.gridy = 2;
            panel.add(startTag, gbc_startTag);

            textField = new JTextField();
            GridBagConstraints gbc_StartBid = new GridBagConstraints();
            gbc_textField.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField.gridx = 1;
            gbc_textField.gridy = 2;
            panel.add(textField, gbc_textField);
            textField.setColumns(10);

            JLabel multTag = new JLabel("Multiplier:");
            GridBagConstraints gbc_multTag = new GridBagConstraints();
            gbc_multTag.insets = new Insets(0, 0, 0, 5);
            gbc_multTag.anchor = GridBagConstraints.EAST;
            gbc_multTag.gridx = 0;
            gbc_multTag.gridy = 3;
            panel.add(multTag, gbc_multTag);

            textField = new JTextField();
            GridBagConstraints gbc_Multiplier = new GridBagConstraints();
            gbc_textField.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField.gridx = 1;
            gbc_textField.gridy = 3;
            panel.add(textField, gbc_textField);
            textField.setColumns(10);

            JLabel minTag = new JLabel("Min Remaining:");
            GridBagConstraints gbc_minTag = new GridBagConstraints();
            gbc_minTag.insets = new Insets(0, 0, 0, 5);
            gbc_minTag.anchor = GridBagConstraints.EAST;
            gbc_minTag.gridx = 0;
            gbc_minTag.gridy = 4;
            panel.add(minTag, gbc_minTag);

            textField = new JTextField();
            GridBagConstraints gbc_MinRemaining = new GridBagConstraints();
            gbc_textField.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField.gridx = 1;
            gbc_textField.gridy = 4;
            panel.add(textField, gbc_textField);
            textField.setColumns(10);

            textField = new JTextField();
            GridBagConstraints gbc_Password = new GridBagConstraints();
            gbc_textField.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField.gridx = 1;
            gbc_textField.gridy = 1;
            panel.add(textField, gbc_textField);
            textField.setColumns(10);

            JLabel passTag = new JLabel("Password:");
            GridBagConstraints gbc_passTag = new GridBagConstraints();
            gbc_passTag.insets = new Insets(0, 0, 0, 5);
            gbc_passTag.anchor = GridBagConstraints.EAST;
            gbc_passTag.gridx = 0;
            gbc_passTag.gridy = 1;
            panel.add(passTag, gbc_passTag);

            textField = new JTextField();
            GridBagConstraints gbc_Odds = new GridBagConstraints();
            gbc_textField.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField.gridx = 1;
            gbc_textField.gridy = 5;
            panel.add(textField, gbc_textField);
            textField.setColumns(10);

            JLabel oddsTag = new JLabel("Odds %:");
            GridBagConstraints gbc_oddsTag = new GridBagConstraints();
            gbc_oddsTag.insets = new Insets(0, 0, 0, 5);
            gbc_oddsTag.anchor = GridBagConstraints.EAST;
            gbc_oddsTag.gridx = 0;
            gbc_oddsTag.gridy = 5;
            panel.add(oddsTag, gbc_oddsTag);

            textField = new JTextField();
            GridBagConstraints gbc_ComboBox = new GridBagConstraints();
            gbc_textField.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField.gridx = 1;
            gbc_textField.gridy = 6;
            panel.add(textField, gbc_textField);
            textField.setColumns(10);

            //This is the Combo Box
            combo = new JComboBox<String>(new String[]{"BTC","LTC","PPC","NMC","XPM","FTC","ANC","DOGE","NXT"});
            combo.addActionListener(this);
            GridBagConstraints gbc_list = new GridBagConstraints();
            gbc_list.fill = GridBagConstraints.HORIZONTAL;
            gbc_list.gridx = 1;
            gbc_list.gridy = 7;
            panel.add(combo, gbc_list);

            JLabel maxTag = new JLabel("MaxBet:");
            GridBagConstraints gbc_maxTag = new GridBagConstraints();
            gbc_maxTag.insets = new Insets(0, 0, 0, 5);
            gbc_maxTag.anchor = GridBagConstraints.EAST;
            gbc_maxTag.gridx = 0;
            gbc_maxTag.gridy = 6;
            panel.add(maxTag, gbc_maxTag);

            JPanel panel_1 = new JPanel();
            contentPane.add(panel_1, BorderLayout.SOUTH);
            panel_1.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));

            JButton btnConfirm = new JButton("Turn Up");
            panel_1.add(btnConfirm);

        JScrollPane scrollPane = new JScrollPane();
            contentPane.add(scrollPane, BorderLayout.CENTER);
            JTextArea textArea = new JTextArea("Current Balance");
            textArea.setColumns(1);
            scrollPane.setViewportView(textArea);

        JScrollPane scrollPanel = new JScrollPane();//This will hold the information the bot sends over such as win/loose or error
            contentPane.add(scrollPane, BorderLayout.CENTER);
            JTextArea textAreal = new JTextArea("Input bot information here...");
            textArea.setColumns(20);
            scrollPane.setViewportView(textArea);

            pack();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == combo) {
                System.out.println(combo.getSelectedIndex()+1);
            }
        }

    }

It's quite straightforward to add an ActionListener to a JButton . JButton添加ActionListener很简单。

// ...
btnConfirm = new JButton("Turn Up");
btnConfirm.addActionListener(this);
panel_1.add(btnConfirm);
// ...

Because you'll want to know when the button is triggering the callback, like so: 因为您想知道按钮何时触发回调,所以像这样:

private JButton btnConfirm;

This way, you'll be able to tell when it's that button that triggered the actionListener() method: 这样,您就可以知道何时触发该actionListener()方法的按钮是:

if (event.getSource() == btnConfirm) {
    // Handle "Turn Up" button press here
}

Start by taking a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener 首先看一下如何使用按钮,复选框和单选按钮以及如何编写动作侦听器

Essentially you need to register an ActionListener with your button... 本质上,您需要使用按钮注册一个ActionListener

There are a number of ways you might be able to achieve this... 您可以通过多种方式实现这一目标...

You Could 你可以

Use the more traditional, purpose class... 使用更传统的目标类...

public class ActionHandler implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
        if ("Turn Up".evt.getActionCommand()) {
            // Handle Turn Up...
        }
    }
}

In this context, you'll be required to provide a reference to the component you want to modify so that the ActionHandler can interact with it, personally, this is best done via an interface , but that's just me... 在这种情况下,您将需要提供要修改的组件的引用,以便ActionHandler可以与其进行交互,最好是通过一个interface来完成,但这仅是我...

public class ActionHandler implements ActionListener {
    private dogedice dice;
    public ActionHandler(dogedice dice) {
        this.dice = dice;
    }
    public void actionPerformed(ActionEvent evt) {
        //...
    }
}

The benefit of this is you can plug an play the action handler, changing what the action does depending on your needs. 这样做的好处是您可以插入一个动作处理程序,根据需要更改动作。 If you use an interface instead of an implementation reference, you further decouple the action from the program 如果使用interface而不是实现参考,则将操作与程序进一步分离

You Could 你可以

Use use an anonymous class 使用使用匿名类

btnConfirm = new JButton("Turn Up");
btnConfirm.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        // handle turn up action...
    }
});

The benefit to this is, you don't end up with "another" class, the ActionListener can reference all the internal fields of the parent class and, because you've attached it directly, you can assume that the only thing that is going to generate the ActionEvent is the btnConfirm button. 这样做的好处是,您不必以“另一个”类结尾, ActionListener可以引用父类的所有内部字段,并且由于直接附加了它,因此可以假定唯一要做的事情btnConfirm按钮可以生成ActionEvent

The downside is you lose the flexibility to change how the action is handled without physically modifying the code 缺点是您失去了在不实际修改代码的情况下更改操作方式的灵活性

You Could 你可以

Take advantage of the Action s API , this is a little like the first option, in that you (should normally) create another class specifically designed to handle the "Turn Up" event, but these are self contained entities. 利用Action的API ,这有点类似于第一个选项,因为您(通常应该)创建另一个专门设计用于处理“ Turn Up”事件的类,但是它们是自包含的实体。 That is, they carry all the information required to configure the UI element and perform the required action when triggered. 也就是说,它们包含配置UI元素并在触发时执行所需操作所需的所有信息。

This is really good where the action may be repeated in the UI via menus, buttons or key strokes 在通过菜单,按钮或按键在UI中重复操作的地方,这确实非常好

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

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