簡體   English   中英

Java Swing 按鈕在其下方的面板上創建一個欺騙

[英]Java Swing button creates a dupe on the panel below it

我們有這個任務,我們在其中創建一個 GUI 窗口,該窗口根據 RGB-Alpha 值設置面板的顏色。 我讓它大部分工作,但是當我點擊更改顏色按鈕時,它會復制(我不確定它是否唯一的視覺錯誤)它自己的點擊狀態。 如果 alpha 為 255,它是不可見的,因為它是完全不透明的,但如果它是透明的,則視覺故障是可見的。

窗口/框架截圖: https ://imgur.com/a/Vf1Hb2B

這是我當前的代碼:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
 
public class ColorCalc implements ActionListener {
    //Containers
    private JFrame f;
    private JPanel colorPanel;
    //Components
    private JLabel l1,l2,l3,l4;
    private JTextField redTf,greenTf,blueTf,alphaTf;
    private JButton bCompute,bClear;
 
    public ColorCalc()
    {
        //Containers
        f = new JFrame("My Color Calculator");
        f.setPreferredSize(new Dimension(400, 250));//Set Frame Size
 
        colorPanel = new JPanel();
        colorPanel.setPreferredSize(new Dimension(300, 200));//Set Panel Size
 
        //Components
        l1 = new JLabel("Red:");
        l2 = new JLabel("Green:");
        l3 = new JLabel("Blue:");
        l4 = new JLabel("Alpha:");
        
        redTf = new JTextField("0", 3);
        greenTf = new JTextField("0", 3);
        blueTf = new JTextField("0", 3);
        alphaTf = new JTextField("0", 3);
        
        bCompute = new JButton("Compute");
        bClear = new JButton("Clear");
        //Action Listeners
        bCompute.addActionListener(this);
        bClear.addActionListener(this);
    }
    public void startApp()
    {
        //Labels Panel
        JPanel lPanel = new JPanel();
        lPanel.setLayout(new GridLayout(5,1));
        lPanel.add(l1);
        lPanel.add(l2);
        lPanel.add(l3);
        lPanel.add(l4);
        lPanel.add(bCompute);
        //Text Fields Panel
        JPanel tfPanel = new JPanel();
        tfPanel.setLayout(new GridLayout(5,1));
        tfPanel.add(redTf);
        tfPanel.add(greenTf);
        tfPanel.add(blueTf);
        tfPanel.add(alphaTf);
        tfPanel.add(bClear);
        //Labels + TextFields Panel
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(1,2));
        inputPanel.add(lPanel);
        inputPanel.add(tfPanel);
        //Add all panels to Frame
        f.setLayout(new GridLayout(2,1));
        f.add(inputPanel);
        f.add(colorPanel);
                
        f.setLocationRelativeTo(null);
        f.pack();
        f.setVisible(true);
 
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == bCompute)
        {
            //Set Default Values
            int red = 255;
            int green = 255;
            int blue = 255;
            int alpha = 255;
            try 
            {
                //Get Values and Parse to Integer
                red = Integer.parseInt(redTf.getText());
                green = Integer.parseInt(greenTf.getText());
                blue = Integer.parseInt(blueTf.getText());
                alpha = Integer.parseInt(alphaTf.getText());
                
            } catch (NumberFormatException nfe)//If Parsing failed due to invalid types
            {
                red = 255; green = 255; blue = 255; alpha = 255; //Set values to white
                JOptionPane.showMessageDialog(null, "Invalid Inputs (RGB,Alpha values 0-255)");
            }
            
            if(red < 256 && green < 256 && blue < 256 && alpha < 256)//Max Value: 255
            {
              if(red > -1 && green > -1 && blue > -1 && alpha > -1)//Min Value: 0
                    colorPanel.setBackground(new Color(red, green, blue, alpha));
                else
                    JOptionPane.showMessageDialog(null, "Invalid Inputs (RGB,Alpha values 0-255)");    
            }                  
            else
                JOptionPane.showMessageDialog(null, "Invalid Inputs (RGB,Alpha values 0-255)");
        }
        else if(e.getSource() == bClear)
        {
            //Set all values to 0 and set panel to white
            redTf.setText("0");
            greenTf.setText("0");
            blueTf.setText("0");
            alphaTf.setText("0");
            colorPanel.setBackground(Color.WHITE);
        }
    }
    public static void main(String[] args) 
    {
        ColorCalc colCalc = new ColorCalc();
        colCalc.startApp();
    }
}

調用方法f.repaint(); actionPerformed()方法的末尾

在此處查看更多信息

暫無
暫無

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

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