从另一个类向标签添加文本-简单逻辑问题 [英] Adding text to a label from another class - Simple Logic Issue
问题描述
我在名为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屋!