[英]How to communicate with GUI from another class
我有理解如何从另一个类与GUI通信的问题。
我们有一个类,whitch创建框架:
public class TestFrame extends javax.swing.JFrame {
javax.swing.JLabel label;
public TestFrame() {
initComponents();
label=sampleLabel;
}
private void initComponents() {
sampleLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
sampleLabel.setText("anotherText");
getContentPane().add(sampleLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 60, -1, -1));
pack();
}
private javax.swing.JLabel sampleLabel;
}
Main class instantiate CreateFrame and tryes to overwrite label text:
public class Main {
public static void main(String[] args) {
TestFrame frame = new TestFrame();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setVisible(true);
}
});
frame.label.setText("anotherText");
}
}
当然,上面的代码不起作用。 但我是Java的新手,我需要以某种方式从另一个类访问标签(如示例中所示......)
最新编辑:上面的代码确实有效:)希望帮助......
以下图片显示相同的帧。 唯一改变的是JLabel文本。
请帮忙。
如果您只是想通过访问其setText()方法来更改标签。 您所要做的就是以下内容(保持其他所有内容相同):
public void makeFrames() {
CreateFrame frame = new CreateFrame("Label1");
frame.label.setText("new Label");
}
以下是一个快速入侵,可以看到标签的变化:
public class Main {
JButton button;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Main object = new Main();
object.makeFrames();
}
});
}
public void makeFrames() {
final CreateFrame frame = new CreateFrame("Label1");
button = new JButton("Click");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.label.setText("new Label");
}
});
frame.frame.add(BorderLayout.NORTH, button);
}
}
单击按钮时,标签将更改为新标签。
编辑2 :(对main()所做的更改,按钮声明为静态,因此可以从main()中访问
public class Main {
static JButton button;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final CreateFrame frameFromMain = new CreateFrame("Label1");
button = new JButton("Click");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frameFromMain.label.setText("new Label");
}
});
frameFromMain.frame.add(BorderLayout.NORTH, button);
}
});
}
}
请记住,您访问CreateFrame类中的标签就像访问类的任何其他成员一样。 如果您将变量声明为静态,则可以直接访问它:
public class CreateFrame {
JFrame frame;
static JLabel label;
// the rest of the class remains the same
}
public class Main {
static JButton button;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
CreateFrame frameFromMain = new CreateFrame("Label1");
button = new JButton("Click");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateFrame.label.setText("new Label");
}
});
frameFromMain.frame.add(BorderLayout.NORTH, button);
}
});
}
}
编辑3:
如果您不想要该按钮,请删除该按钮的代码并执行以下操作:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
CreateFrame frameFromMain = new CreateFrame("Label1");
CreateFrame.label.setText("new Label"); // accessing label directly from main()
}
});
}
编辑4:使用OP的代码:
public class TestFrame extends javax.swing.JFrame {
javax.swing.JLabel label;
public TestFrame() {
initComponents();
label=sampleLabel;
}
private void initComponents() {
sampleLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
sampleLabel.setText("anotherText");
add(sampleLabel);
pack();
}
private javax.swing.JLabel sampleLabel;
}
然后你的main()看起来像这样:
public class Main {
public static void main(String[] args) {
final TestFrame frame = new TestFrame();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setVisible(true);
}
});
frame.label.setText("Text From Main");
}
}
我做了很少的改变。 此代码运行完美,并按照您的要求执行。 我摆脱了你的“getContentPane”,因为你在Java 6,7中不需要它。我也摆脱了你的布局设置,因为我自己并不熟悉它们。 您需要学习导入java类。
您似乎处于学习Java的早期阶段。 我建议你坚持使用命令行程序,直到你在转向Swing之前弄清楚Java是如何工作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.