【问题标题】:Creating a Color scheme GUI with Java使用 Java 创建配色方案 GUI
【发布时间】:2019-04-21 11:12:51
【问题描述】:

我的名字是 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));
            }
        }
    }

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

【问题讨论】:

  • 您在哪里将 ActionListener 注册到每个 GUI 按钮?或者他们应该如何沟通?

标签: java user-interface colors


【解决方案1】:

actionPerformed() 中,检查e.getSource()jtf2jtf4jtf6,并仅当源同时等于所有值时才执行操作 .

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


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

【讨论】:

  • 谢谢。我忘了添加 xxx.addActionListener()。这有助于解决问题。
【解决方案2】:

您在这里缺少的几件事是: 1) Action Listener 需要绑定 '+' 或 '-' 按钮(此映射缺失)。

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));
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2011-06-14
    • 1970-01-01
    • 2018-04-05
    • 2015-04-09
    • 2021-06-18
    相关资源
    最近更新 更多