格式化GUI /添加动作侦听器时遇到问题

时间:2016-02-15 14:18:23

标签: java swing actionlistener

我通过这个网站进行了广泛的搜索,并通过Java网站试图找到答案,但我找不到特定于我的问题的答案。

我不确定在哪里放置b1b2的动作监听器,我也认为我正在使用的方法可能有问题(虽然代码仍在编译和运行没有动作监听器,一切正常。)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CubeCalc  {

    public static void MakeTitlePage()
    {
        JButton b1 = new JButton("Start");
        b1.setBackground(Color.decode("#5A20DF"));
        b1.setForeground(Color.WHITE);
        b1.setLayout(new GridBagLayout());
        b1.setPreferredSize(new Dimension(150,50));

        JButton b2 = new JButton("Information about the Developer");
        b2.setBackground(Color.decode("#23D123"));
        b2.setForeground(Color.BLACK);
        b2.setLayout(new GridBagLayout());
        b2.setPreferredSize(new Dimension(275,50));

        GridBagConstraints blo = new GridBagConstraints(); 
        blo.fill = GridBagConstraints.HORIZONTAL;
        blo.gridx = 0;
        blo.gridy = 1;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
        JPanel start = new JPanel(new GridBagLayout());
        start.setBackground(Color.BLACK);
        start.setPreferredSize(new Dimension(300,100));
        start.add(b1, blo);

        JPanel info = new JPanel(new GridBagLayout());
        info.setBackground(Color.BLACK);
        info.setPreferredSize(new Dimension(300,100));
        info.add(b2, blo);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
        JFrame window = new JFrame("Cubic Feet Calculator"); //Creates Frame
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            /*  window.add(new JLabel(new ImageIcon("Harold.jpg")));*/
                window.add(start, BorderLayout.NORTH);
                window.add(info, BorderLayout.SOUTH);

        window.pack(); //resizes to minimum possible frame size
        //window.setSize(500,500); //Sets size of frame
        window.setLocationRelativeTo(null);
        window.setVisible(true); //Sets the frame to be visible
        window.setResizable(true);
        window.setBackground(Color.BLACK);

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
    }

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

我应该在哪里添加ActionListener,我的方法应该做些什么?

2 个答案:

答案 0 :(得分:3)

关于代码段的评论:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CubeCalc  {
    public static void MakeTitlePage()
    {
       final JFrame window = new JFrame("Cubic Feet Calculator"); // Creates Frame (created first so the button can reference it for JOptionPane

       JButton b1 = new JButton("Start");
       b1.setBackground(Color.decode("#5A20DF"));
       b1.setForeground(Color.WHITE);
       //b1.setLayout(new GridBagLayout()); << unnecessary
       b1.setPreferredSize(new Dimension(150,50));
       b1.addActionListener(new ActionListener() { // action when button is pressed
            int pressCount=0;
            @Override
            public void actionPerformed(ActionEvent e) {
                switch(++pressCount) {
                    case 1:  JOptionPane.showMessageDialog(window, "Hey, stop pressing me!"); break;
                    case 2:  JOptionPane.showMessageDialog(window, "I said, stop pressing me!!!!"); break;
                    default: JOptionPane.showMessageDialog(window, "Aaaaaaaaargl!!!!"); break;
                }
            }
        });

        JButton b2 = new JButton("Information about the Developer");
        b2.setBackground(Color.decode("#23D123"));
        b2.setForeground(Color.BLACK);
        //b2.setLayout(new GridBagLayout()); << unnecessary
        b2.setPreferredSize(new Dimension(275,50));
        b2.addActionListener(new ActionListener() { // action when button is pressed
            int pressCount=0;
            @Override
            public void actionPerformed(ActionEvent e) {
                switch(++pressCount) {
                    case 1:  JOptionPane.showMessageDialog(window, "There is no information here!"); break;
                    case 2:  JOptionPane.showMessageDialog(window, "Stop asking me for information!!!!"); break;
                    default: JOptionPane.showMessageDialog(window, "Aaaaaaaaargl!!!!"); break;
                }
            }
        });

        GridBagConstraints blo = new GridBagConstraints(); 
        blo.fill = GridBagConstraints.HORIZONTAL;
        blo.gridx = 0;
        blo.gridy = 1;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
        JPanel start = new JPanel(new GridBagLayout());
        start.setBackground(Color.BLACK);
        start.setPreferredSize(new Dimension(300,100));
        start.add(b1, blo);

        JPanel info = new JPanel(new GridBagLayout());
        info.setBackground(Color.BLACK);
        info.setPreferredSize(new Dimension(300,100));
        info.add(b2, blo);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /*  window.add(new JLabel(new ImageIcon("Harold.jpg")));*/
        window.add(start, BorderLayout.NORTH);
        window.add(info, BorderLayout.SOUTH);

        window.setLocationRelativeTo(null);
        window.setResizable(true);
        window.setBackground(Color.BLACK);
        window.pack(); //resizes to minimum possible frame size
        window.setVisible(true); //Sets the frame to be visible
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() { // launch frame on the Event Dispatch Thread
            @Override
            public void run() {
                 MakeTitlePage();
            }
        });
    }
}

效果:

enter image description here

答案 1 :(得分:1)

您可以尝试以下方式:

 b1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                // what do you want to execute...

            }
        });