首页 > 解决方案 > 使用 Java 创建配色方案 GUI

问题描述

我的名字是 Abel,我是 Java 新手。我正在尝试创建一个程序,该程序允许我将 int 值输入 Jtextfields 并使用它们来更改面板底行的颜色。我尝试将 jtextfield 中的值转换为整数。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));
            }
        }
    }

谁能告诉我我做错了什么?

标签: javauser-interfacecolors

解决方案


您在这里缺少的几件事是: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));
        }
    }
}

推荐阅读