使用Java创建配色方案GUI

时间:2018-11-19 05:27:26

标签: java user-interface colors

我的名字是Abel,我是Java的新手。我正在尝试创建一个程序,该程序将允许我将int值输入Jtextfields并使用它们来更改面板底部行的颜色。我尝试将jtextfield中的值转换为ints。 IDE说我没有明显的错误,但仍然无法更改颜色。我想在完成程序之前先弄清楚这一点。 这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class App1 extends JFrame implements ActionListener {

    JPanel  jp, jp1, jp2, jp3, jp4, jp5;
    JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6;
    JButton jbrplus, jbrneg, jbgplus, jbgneg, jbbplus, jbbneg;
    int value, value1, value2;
    String text, text1, text2;

    public static void main(String[] args) {

        App1 KF = new App1();

    }

    App1() {
        this.setTitle("Application 1");
        this.setSize(800, 600);

        jp = new JPanel();
        this.add(jp);
        jp.setLayout(new GridLayout(2,1));

        jp1 = new JPanel();
        jp.add(jp1);
        jp1.setLayout(new GridLayout(1, 3));

        jp2 = new JPanel();
        jp1.add(jp2);
        jtf1 = new JTextField("RED");
        jtf1.setEditable(false);
        jp2.add(jtf1);
        jbrplus = new JButton("+");
        jp2.add(jbrplus);
        jtf2 = new JTextField("    0    ");
        jtf2.setEditable(true);
        jp2.add(jtf2);
        jbrneg = new JButton("-");
        jp2.add(jbrneg);
        jp2.setBackground(Color.RED);

        jp3 = new JPanel();
        jp1.add(jp3);
        jtf3 = new JTextField("Green");
        jtf3.setEditable(false);
        jp3.add(jtf3);
        jbgplus = new JButton("+");
        jp3.add(jbgplus);
        jtf4 = new JTextField("    0    ");
        jtf4.setEditable(true);
        jp3.add(jtf4);
        jbgneg = new JButton("-");
        jp3.add(jbgneg);
        jp3.setBackground(Color.GREEN);

        jp4 = new JPanel();
        jp1.add(jp4);
        jtf5 = new JTextField("Blue");
        jtf5.setEditable(false);
        jp4.add(jtf5);
        jbbplus = new JButton("+");
        jp4.add(jbbplus);
        jtf6 = new JTextField("    0    ");
        jtf6.setEditable(true);
        jp4.add(jtf6);
        jbbneg = new JButton("-");
        jp4.add(jbbneg);
        jp4.setBackground(Color.BLUE);

        jp5 = new JPanel();
        jp.add(jp5);
        jp5.setBackground(new Color(value, value1 , value2));
        this.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jtf2 && e.getSource() == jtf4 && e.getSource() == jtf6) {
                    text = jtf2.getText();
                    text1 = jtf4.getText();
                    text2 = jtf6.getText();
                    value = Integer.parseInt(text);
                    value1 = Integer.parseInt(text1);
                    value2 = Integer.parseInt(text2);
                    jp5.setBackground(new Color(value, value1, value2));
            }
        }
    }

有人可以告诉我我在做什么错吗?

2 个答案:

答案 0 :(得分:0)

actionPerformed()中,针对e.getSource()jtf2jtf4检查jtf6,仅在源相等时才执行操作同时访问所有值

您可能想要“如果来源是第一个文本字段,则 OR ,来源是第二个 OR ,来源是第三个文本字段。


尽管App1实现了ActionListener,但您在哪里都不叫xxx.addActionListener(app1)。您将需要连接jtf2jtf4jtf6才能将操作事件发送到应用程序实例。完成此操作后,由于它们是唯一关联到该ActionListener的东西,因此您实际上不需要检查是哪个事件产生了该事件,因为您可以响应任何来源。

答案 1 :(得分:0)

这里缺少的东西是: 1)动作监听器需要绑定“ +”或“-”按钮(此映射丢失)。

2)如果(e.getSource() == jtf2 && e.getSource() == jtf4 && e.getSource() == jtf6)需要修改,请在此将&&替换为||

此代码应为您工作。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class App1 extends JFrame implements ActionListener {

    JPanel jp, jp1, jp2, jp3, jp4, jp5;
    JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6;
    JButton jbrplus, jbrneg, jbgplus, jbgneg, jbbplus, jbbneg;
    int value, value1, value2;
    String text, text1, text2;

    public static void main(String[] args) {

        App1 KF = new App1();

    }

    App1() {
        this.setTitle("Application 1");
        this.setSize(800, 600);

        jp = new JPanel();
        this.add(jp);
        jp.setLayout(new GridLayout(2, 1));

        jp1 = new JPanel();
        jp.add(jp1);
        jp1.setLayout(new GridLayout(1, 3));

        jp2 = new JPanel();
        jp1.add(jp2);
        jtf1 = new JTextField("RED");
        jtf1.setEditable(false);
        jp2.add(jtf1);
        jbrplus = new JButton("+");
        jbrplus.addActionListener(this); // Add actionListener Step#1
        jp2.add(jbrplus);
        jtf2 = new JTextField("0");
        jtf2.setEditable(true);

        jp2.add(jtf2);
        jbrneg = new JButton("-");
        jbrneg.addActionListener(this);
        jp2.add(jbrneg);
        jp2.setBackground(Color.RED);

        jp3 = new JPanel();
        jp1.add(jp3);
        jtf3 = new JTextField("Green");
        jtf3.setEditable(false);
        jp3.add(jtf3);
        jbgplus = new JButton("+");
        jbgplus.addActionListener(this);
        jp3.add(jbgplus);
        jtf4 = new JTextField("0");
        jtf4.setEditable(true);
        jp3.add(jtf4);
        jbgneg = new JButton("-");
        jbgneg.addActionListener(this);
        jp3.add(jbgneg);
        jp3.setBackground(Color.GREEN);

        jp4 = new JPanel();
        jp1.add(jp4);
        jtf5 = new JTextField("Blue");
        jtf5.setEditable(false);
        jp4.add(jtf5);
        jbbplus = new JButton("+");
        jbbplus.addActionListener(this);
        jp4.add(jbbplus);
        jtf6 = new JTextField("0");
        jtf6.setEditable(true);
        jp4.add(jtf6);
        jbbneg = new JButton("-");
        jbbneg.addActionListener(this);
        jp4.add(jbbneg);
        jp4.setBackground(Color.BLUE);

        jp5 = new JPanel();
        jp.add(jp5);
        jp5.setBackground(new Color(value, value1, value2));
        this.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jbrplus || e.getSource() == jbrneg || e.getSource() == jbgplus || 
                e.getSource() == jbgneg || e.getSource() == jbbplus || e.getSource() == jbbneg) {// Condition modification Step#2           
            text = jtf2.getText();
            text1 = jtf4.getText();
            text2 = jtf6.getText();
            value = Integer.parseInt(text);
            value1 = Integer.parseInt(text1);
            value2 = Integer.parseInt(text2);
            jp5.setBackground(new Color(value, value1, value2));
        }
    }
}