从另一个类向标签添加文本-简单逻辑问题 [英] Adding text to a label from another class - Simple Logic Issue

查看:60
本文介绍了从另一个类向标签添加文本-简单逻辑问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为FrameTest的类中有一个标签和一个按钮,当我按下按钮时,将从类Test执行名为buttonpressed的方法.在这种buttonpressed方法中,我将在FrameTest类中找到的标签上设置文本.

I have a label and a button in a class called FrameTest, when i press the button, a method named buttonpressed get's executed from the class Test. In this buttonpressed method i will set a text to the label found in the FrameTest class.

我的问题是标签的文本未设置.原因是我正在创建一个单独的对象来调用buttonpressed方法;

The problem i have is that, the text for the label is not getting set. The reason is that i am creating a separate object to call the buttonpressed method;

public void actionPerformed(ActionEvent arg0) {
                    Test t = new Test();
                    t.buttonpress();
                }

并且我正在Test类的main方法中创建一个单独的对象来创建UI.

and i am creating a separate object in the main method of the Test class to create the UI.

public static void main(String[] args) {

         FrameTest f = new FrameTest();
         f.mainScreen();

    }

完整代码如下;

public class FrameTest extends JFrame {

private JPanel contentPane;
private JLabel lblLabel;
private FrameTest ft = this;

//private FrameTest frame;
/**
 * Launch the application.
 */
public  void mainScreen() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                //FrameTest frame = new FrameTest();
                //setVisible(true);

                FrameTest frame = ft;
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public void writeLabel(String k){
    this.lblLabel.setText(k);

}

public FrameTest() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setExtendedState(JFrame.MAXIMIZED_BOTH); 

    //setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    lblLabel = new JLabel("LABEL");
    contentPane.add(lblLabel, BorderLayout.CENTER);

    JButton btnNewButton = new JButton("Press");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Test t = new Test();
            t.buttonpress();
        }
    });
    contentPane.add(btnNewButton, BorderLayout.WEST);
    //pack();
    setLocationByPlatform(true);
}

}

测试类

public class Test {


public static void main(String[] args) {

     FrameTest f = new FrameTest();
     f.mainScreen();

}

public void buttonpress(){
     FrameTest f = new FrameTest();

     f.writeLabel("Button was pressed");

}

推荐答案

1)不要不必要地扩展JFrame类.

1) Dont extend JFrame class unnecessarily.

2)请勿使用setContentPane(),除非您要这么做.而是简单地JFrame#add(..).

2) dont use setContentPane() unless thats what you want. Rather just simply JFrame#add(..).

3)避开EventQueue并改用SwingUtilities阻止

4)在将JFrame设置为可见之前,请不要忘记拨打JFrame#pack();.

4) Dont forget to call JFrame#pack(); before setting JFrame visible.

5)Java命名约定为CamelCase,因此buttonPress()是正确的,而不是buttonpress()

5) Java naming convention is CamelCase so buttonPress() is correct not buttonpress()

这是我制作的一个示例(基本上是固定的代码):

Here is an example I made (basically your code fixed):

Test.java:(这是将创建您的FrameTest实例并具有更改JLabel文本的方法的主类)

Test.java: (This is the main class which will create an instance of your FrameTest and has the method to change JLabel text)

import javax.swing.SwingUtilities;

public class Test {

    private static FrameTest f;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                f = new FrameTest();
                f.mainScreen();
            }
        });
    }

    void buttonPress() {
        f.writeLabel("Hello");
    }
}

FrameTest.java:(此类将显示JFrame并创建类Test的新实例以调用buttonPress()):

FrameTest.java: (This class will show the JFrame and create a new instance of class Test to call buttonPress()):

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class FrameTest {

    private JPanel panel;
    private JLabel lblLabel;
    private JFrame frame;

    private void initComponents() {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

        panel = new JPanel();
        panel.setBorder(new EmptyBorder(5, 5, 5, 5));
        panel.setLayout(new BorderLayout(0, 0));

        lblLabel = new JLabel("LABEL");
        panel.add(lblLabel, BorderLayout.CENTER);

        JButton btnNewButton = new JButton("Press");
        btnNewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Test t = new Test();
                t.buttonPress();
            }
        });

        panel.add(btnNewButton, BorderLayout.WEST);

        frame.add(panel);

        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }

    public void writeLabel(String k) {
        this.lblLabel.setText(k);
    }

    void mainScreen() {
        initComponents();
    }
}

这篇关于从另一个类向标签添加文本-简单逻辑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆