2

因此,在其他人的帮助下,我终于设法编写了一个交替标签“Hello World!”的按钮。到“你好宇宙!” 然后再回来。我使用下面的代码,并使用相同的方式尝试更改颜色,但它没有按预期工作。我一直在寻找几个小时,但无济于事。感谢您的阅读,有什么帮助!

代码:

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

public class Javagame extends JPanel implements ActionListener{
    protected JButton changetext;
    protected JButton red;
    protected JButton green;
    private JLabel label;

    public Javagame() {
        add(changetext = new JButton("Button!"));
        changetext.setPreferredSize(new Dimension(50, 50));
        changetext.setActionCommand("change");

        add(red = new JButton("Red"));
        red.setPreferredSize(new Dimension(50, 50));
        red.setActionCommand("changecolorRed");

        add(green = new JButton("Green"));
        green.setPreferredSize(new Dimension(50, 50));
        green.setActionCommand("changecolorGreen");

        changetext.addActionListener(this);

        label = new JLabel("Hello World!", SwingConstants.CENTER);
        label.setFont(new Font("Arial", Font.BOLD, 20));
        label.setForeground(new Color(0x009900));
        setLayout(new BorderLayout());
        add(label, BorderLayout.CENTER);
        add(changetext, BorderLayout.NORTH);
        add(red, BorderLayout.WEST);
        add(green, BorderLayout.EAST);
    }
    public void actionPerformed(ActionEvent e) {
        if ("change".equals(e.getActionCommand())) {
            label.setText("Hello Universe!");   
            changetext.setActionCommand("changeBack");
        }
        if ("changeBack".equals(e.getActionCommand())) {
            label.setText("Hello World!");
            changetext.setActionCommand("change");
        }
        if ("changecolorRed".equals(e.getActionCommand())) {
            label.setForeground(new Color(0xFF0000));
        }
        if ("changecolorGreen".equals(e.getActionCommand())) {
            label.setForeground(new Color(0x009900));
        }
    }
    private static void createWindow(){
        JFrame frame = new JFrame("Javagame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(500,500));

        JPanel panel = new JPanel(new BorderLayout());

        Javagame newContentPane = new Javagame();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        createWindow();
    }
}
4

1 回答 1

1

您需要将 ActionListeners 添加到按钮才能工作。

这通常通过一个简单的方法调用来完成:red.addActionListener(someListener);

还:

  • 摆脱您的setPreferredsize(...)方法调用,而是让组件设置自己的大小。如果需要,您最多可以覆盖getPreferredSize(),但尽量限制它。
  • 避免让您的 GUI 代码实现您的侦听器接口,因为这会导致代码混乱和难以管理。最好使用匿名内部侦听器或私有内部类或独立侦听器类。

例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class JavaGame2 extends JPanel {
   private static final int PREF_W = 600;
   private static final int PREF_H = 400;
   private static final Font LABEL_FONT = new Font("Arial", Font.BOLD, 20);
   private static final Color[] FOREGROUNDS = { new Color(0x009900),
         new Color(0x990000), new Color(0x000099), new Color(0x999900),
         new Color(0x990099), new Color(0x009999) };
   private static final String[] LABEL_TEXTS = { "Hello World!",
         "Goodbye World!", "Hola Todo el Mundo!", "Hasta la Vista Baby!",
         "Whatever!!" };

   private JButton changetextButton;
   private JButton changeColorButton;
   private JLabel label;
   private int labelTextIndex = 0;
   private int foregroundIndex = 0;

   public JavaGame2() {
      label = new JLabel(LABEL_TEXTS[labelTextIndex], SwingConstants.CENTER);
      label.setFont(LABEL_FONT);
      label.setForeground(FOREGROUNDS[foregroundIndex]);

      // example of anonymous inner ActionListener:
      changetextButton = new JButton("Change Text");
      changetextButton.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent evt) {
            labelTextIndex++;
            labelTextIndex %= LABEL_TEXTS.length;
            label.setText(LABEL_TEXTS[labelTextIndex]);
         }
      });

      // example of use of an anonymous AbstractAction:
      changeColorButton = new JButton(new AbstractAction("Change Color") {

         @Override
         public void actionPerformed(ActionEvent e) {
            foregroundIndex++;
            foregroundIndex %= FOREGROUNDS.length;
            label.setForeground(FOREGROUNDS[foregroundIndex]);
         }
      });

      setLayout(new BorderLayout());
      add(changetextButton, BorderLayout.NORTH);
      add(changeColorButton, BorderLayout.SOUTH);
      add(label, BorderLayout.CENTER);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      JavaGame2 mainPanel = new JavaGame2();

      JFrame frame = new JFrame("Java Game 2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
于 2013-06-01T18:15:02.357 回答