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

时间:2014-06-06 23:22:27

标签: java swing

好的,所以我用一些输入框和一个组合框制作了一个GUI。我想知道如何在按下按钮时让听众知道,然后将输入框和组合框中的所有信息传递到脚本的不同部分......

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);
            }
        }

    }

2 个答案:

答案 0 :(得分:1)

ActionListener添加JButton非常简单。

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

因为您想知道按钮何时触发回调,如下所示:

private JButton btnConfirm;

通过这种方式,您将能够判断触发actionListener()方法的按钮是什么时候:

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

答案 1 :(得分:1)

首先看看How to Use Buttons, Check Boxes, and Radio ButtonsHow to Write an Action Listener

基本上你需要用你的按钮注册ActionListener ......

您可以通过多种方式实现这一目标......

你可以

使用更传统的目标类......

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

在此上下文中,您需要提供对要修改的组件的引用,以便ActionHandler可以与其进行交互,个人而言,最好通过{{1}完成},但那只是我...

interface

这样做的好处是你可以插入一个动作处理程序,根据你的需要改变动作。如果您使用public class ActionHandler implements ActionListener { private dogedice dice; public ActionHandler(dogedice dice) { this.dice = dice; } public void actionPerformed(ActionEvent evt) { //... } } 而不是实现引用,则进一步将操作与程序

分离

你可以

使用anonymous class

interface

这样做的好处是,你不会得到另一个&#34;另一个&#34;在类中,btnConfirm = new JButton("Turn Up"); btnConfirm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // handle turn up action... } }); 可以引用父类的所有内部字段,并且因为您已直接附加它,您可以假设唯一要生成ActionListener的是ActionEvent按钮。

缺点是你在没有物理修改代码的情况下失去了改变动作处理方式的灵活性

你可以

利用Actions API,这有点像第一个选项,因为你(通常应该)创建另一个专门用于处理&#34; Turn Up&#34;事件,但这些是自包含的实体。也就是说,它们包含配置UI元素所需的所有信息,并在触发时执行所需的操作。

如果可以通过菜单,按钮或击键在UI中重复操作,这非常好用