我如何在buttonhandler中引用我的JLabel消息?

时间:2016-10-30 17:23:42

标签: button jframe jpanel jtextfield rgb

基本上!我需要使用jtextfields更改最初设置为红色的消息的颜色,除了改变颜色的位之外,所有代码都已完成,因为我不知道如何引用消息。我知道它需要进入@overide动作执行位,但它说它不能识别什么消息'是的,我已经尝试过message.setForeground(new Color(a,b,c));但是,对于在这里做什么的任何帮助都会很棒,谢谢。

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



public class CE203_2016_Ex1 extends JFrame {

    JTextField tred, tgreen, tblue;

    public CE203_2016_Ex1() {

        JPanel panel1 = new JPanel();       //creates panels for the boxes that will hold the rgb values
        JPanel panel2 = new JPanel();

        tred = new JTextField("Red", 10);
        tgreen = new JTextField("Green", 10);             //creates boxes for rgb values
        tblue = new JTextField("Blue", 10);

        panel1.add(tred);
        panel1.add(tgreen);                            //adding jtextfields to panels
        panel1.add(tblue);

        add(panel1, BorderLayout.SOUTH); //adding panels to frame
        add(panel2, BorderLayout.CENTER);

        JLabel message = new JLabel("hello");  //text
        message.setForeground(new Color(255, 0, 0));     //original text set to red

        JButton goButton = new JButton("Change");       //adds button to change colour
        panel1.add(goButton);
        panel2.add(message);
        goButton.addActionListener(new ButtonHandler(this));
    }

    class ButtonHandler implements ActionListener {

        private CE203_2016_Ex1 theApp;

        public ButtonHandler(CE203_2016_Ex1 theApp) {
            this.theApp = theApp;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int a = Integer.parseInt(theApp.tred.getText());
            int b = Integer.parseInt(theApp.tgreen.getText());
            int c = Integer.parseInt(theApp.tblue.getText());


        }
    }

    public static void main(String[] args) {
        JFrame app = new CE203_2016_Ex1();
        app.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        app.setSize(700, 700);
        app.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:0)

您需要在应用中公开消息标签。这意味着您必须将JLabel消息作为应用程序的一个字段,然后添加一个getter来检索它。这样,任何有权访问您应用的内容都可以访问消息标签。

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



public class CE203_2016_Ex1 extends JFrame {

    JTextField tred, tgreen, tblue;
    JLabel message;

    public CE203_2016_Ex1() {

        JPanel panel1 = new JPanel();       //creates panels for the boxes that will hold the rgb values
        JPanel panel2 = new JPanel();

        tred = new JTextField("Red", 10);
        tgreen = new JTextField("Green", 10);             //creates boxes for rgb values
        tblue = new JTextField("Blue", 10);

        panel1.add(tred);
        panel1.add(tgreen);                            //adding jtextfields to panels
        panel1.add(tblue);

        add(panel1, BorderLayout.SOUTH); //adding panels to frame
        add(panel2, BorderLayout.CENTER);

        message = new JLabel("hello");  //text
        message.setForeground(new Color(255, 0, 0));     //original text set to red

        JButton goButton = new JButton("Change");       //adds button to change colour
        panel1.add(goButton);
        panel2.add(message);
        goButton.addActionListener(new ButtonHandler(this));
    }

    public JLabel getMessageLabel() {
        return message;
    }

    class ButtonHandler implements ActionListener {

        private CE203_2016_Ex1 theApp;

        public ButtonHandler(CE203_2016_Ex1 theApp) {
            this.theApp = theApp;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int a = Integer.parseInt(theApp.tred.getText());
            int b = Integer.parseInt(theApp.tgreen.getText());
            int c = Integer.parseInt(theApp.tblue.getText());
            theApp.getMesssageLabel().setForground(new Color(a, b, c));
        }
    }

    public static void main(String[] args) {
        JFrame app = new CE203_2016_Ex1();
        app.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        app.setSize(700, 700);
        app.setVisible(true);
    }
}

重要的部分是:

JTextField tred, tgreen, tblue;

// Added field message for later use
JLabel message;

public CE203_2016_Ex1() {

// Enables you to get the message label from your app later on
public JLabel getMessageLabel() {
    return message;
}

class ButtonHandler implements ActionListener {

int c = Integer.parseInt(theApp.tblue.getText());
// Gets the message label from your App and sets its foreground color
theApp.getMesssageLabel().setForground(new Color(a, b, c));

答案 1 :(得分:0)

您尝试从scope外部访问变量message,因为您在构造函数中声明了JLabel,它只能由构造函数访问。所以你想在构造函数之外声明JLabel和JTextFields一样,我也建议为它申请private