Java Swing 按钮在其下方的面板上创建了一个复制品
Java Swing button creates a dupe on the panel below it
我们有这个任务,我们制作了一个 GUI window,它根据 RGB-Alpha 值设置面板的颜色。我大部分时间都在使用它,但是当我点击更改颜色按钮时,它会复制(我不确定它是否是唯一的视觉错误)它自己的点击状态。如果 alpha 为 255 则不可见,因为它是完全不透明的,但如果它是透明的,则视觉故障是可见的。
Window/Frame的截图: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();
}
}
在actionPerformed()
方法的最后调用方法f.repaint();
查看更多信息here
我们有这个任务,我们制作了一个 GUI window,它根据 RGB-Alpha 值设置面板的颜色。我大部分时间都在使用它,但是当我点击更改颜色按钮时,它会复制(我不确定它是否是唯一的视觉错误)它自己的点击状态。如果 alpha 为 255 则不可见,因为它是完全不透明的,但如果它是透明的,则视觉故障是可见的。
Window/Frame的截图: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();
}
}
在actionPerformed()
方法的最后调用方法f.repaint();
查看更多信息here