簡體   English   中英

我可以使用getActionCommand更改標簽內容,但是不能使用它來更改顏色嗎?

[英]I can use getActionCommand to change the label content, but I can't use it to change the color?

因此,在這里的其他人的幫助下,我終於設法編寫了一個替換標簽“ 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();
    }
}

您需要將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();
         }
      });
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM