2013-06-01 43 views
2

因此,在這裏的其他人的幫助下,我最終設法編寫了一個按鈕來替換標籤「Hello World!」。到「你好宇宙!」然後再回來。我使用下面的代碼,並使用相同的方式嘗試和更改顏色,但它沒有按預期工作。我一直在尋找這個小時,但無濟於事。感謝您的閱讀,任何幫助!我可以使用getActionCommand來更改標籤內容,但我不能用它來更改顏色?

代碼:

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(); 
    } 
} 
+0

我能想到的唯一的事情是,你可能不使用EDT來改變顏色(事件指派線程),你開始使用默認的線程是EDT,但不是Awt-Eventque-0。 EDT是唯一可以執行某些任務的人。 – user2097804

回答

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(); 
     } 
     }); 
    } 
} 
+0

我在發佈問題後立即解決了問題,但無論如何謝謝! – BitLion

+0

我一直在嘗試你說的第二個項目符號,但我很困惑:你如何添加匿名內部偵聽器或私有內部類?對不起,如果我看起來有點慢,謝謝。 – BitLion

+0

@BitLion:例如 –

相關問題