簡體   English   中英

JFrame顏色變化

[英]JFrame color change

我有一個JFrame出現在計時器的末尾。 下面的代碼彈出框架和聲音。 從菜單中設置框架的顏色,然后將其提供給框架。 我需要框架在默認顏色和我在菜單中選擇的顏色之間切換。 提前致謝

new Thread(new Runnable()
      {
        public void run()
        {
            JFrame frame= new JFrame(); 
    frame.setVisible(true);
            frame.setSize(600, 400);
            frame.setLocation(200, 200);
            frame.setTitle("ALARM");
            frame.getContentPane().setBackground(GUI.this.timerPanel.colorButton.getBackground());  // *This is the source for the color i select in the menu* 
            JLabel welcome = new JLabel("",SwingConstants.CENTER);
            welcome.setFont(new Font("Serif", Font.PLAIN, 48));
            welcome.setText("ALARM ALARM ALARM");
            frame.add(welcome);
            new SoundEngine().playSound();





        }
      })

        .start();

Swing不是Thread友好的,請嘗試了解SwingUtility.InvokeLater

要更改Color使用JFrame.setBackGround(color)

現在,您將如何切換?

對我而言,最好的方法是創建一個名為UtilClass

public class Util{
     private static int ser=0;
     private static Color[] backColor=new Color[]{Color.red,Color.green,Color.white};
     public static void setBC(JFrame frame){
         frame.setBackGround(backColor[ser++%backColor.lenght]);
}

現在,在擴展的 JFrame類或部分上,只需調用Util.setBC(frame)

它在這三種顏色之間變化,您可以根據需要添加更多或什至刪除soem。

暫無
暫無

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

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