Java JTextField或JLabel未更新的JPanel
我曾试图找到答案,但我没有找到答案。我对java相当陌生。我有4个类(1个带有JFrame的main和3个JPanel)。 main类创建JFrame并向其中添加3个面板。 我试图完成的是从另一个面板(panelB)中的ActionEvent更新一个面板(panelA)中的JLabel或JTextField。 panelB中的ActionEvent在panelA中运行一个方法,该方法运行setText()方法和repaint()方法。我无法使用新文本更新JLabel或JTextField 这是我的代码:Java JTextField或JLabel未更新的JPanel,java,swing,constructor,jbutton,jlabel,Java,Swing,Constructor,Jbutton,Jlabel,我曾试图找到答案,但我没有找到答案。我对java相当陌生。我有4个类(1个带有JFrame的main和3个JPanel)。 main类创建JFrame并向其中添加3个面板。 我试图完成的是从另一个面板(panelB)中的ActionEvent更新一个面板(panelA)中的JLabel或JTextField。 panelB中的ActionEvent在panelA中运行一个方法,该方法运行setText()方法和repaint()方法。我无法使用新文本更新JLabel或JTextField 这是我
App.java BottomPanel.java MiddlePanel.java
感谢您的帮助。在OKButtonListener中,您创建了一个新的BottomPanel实例,该实例与您添加到JFrame中的BottomPanel无关。
您将需要在JFrame中添加的底部面板的实际引用。d1rk将其锁定。这里有一种实现这种效果的方法
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class App {
public static void main(String[] args) {
JFrame nameFrame = new JFrame("Name Form");
nameFrame.setLayout(new BorderLayout());
nameFrame.setSize(300,150);
nameFrame.setLocationRelativeTo(null);
nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BottomPanel bottomPanel = new BottomPanel();
MiddlePanel middlePanel = new MiddlePanel(bottomPanel);
nameFrame.add(middlePanel, BorderLayout.CENTER);
nameFrame.add(bottomPanel, BorderLayout.SOUTH);
nameFrame.setVisible(true);
}
}
class BottomPanel extends JPanel {
private JLabel welcomeLabel = new JLabel("Old Text");
BottomPanel() {
super(new FlowLayout());
add(welcomeLabel);
}
public void setText(String text) {
welcomeLabel.setText(text);
repaint();
}
}
class MiddlePanel extends JPanel {
private BottomPanel bottomPanel;
MiddlePanel(BottomPanel bottomPanel) {
super(new BorderLayout());
this.bottomPanel = bottomPanel;
JButton okButton = new JButton("OK");
okButton.setSize(20, 20);
OKButtonListener okButtonListener = new OKButtonListener();
okButton.addActionListener(okButtonListener);
add(okButton, BorderLayout.EAST);
}
class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//BottomPanel bottomPanel = new BottomPanel();
bottomPanel.setText("New Text");
}
}
}
顺便说一下-ok按钮。设置大小(20,20)代码>不要这样做。布局管理器将忽略它,如果不忽略它,它将是脆弱的。
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class BottomPanel extends JPanel {
private JLabel welcomeLabel = new JLabel("Old Text");
BottomPanel() {
super(new FlowLayout());
add(welcomeLabel);
}
public void setText(String text) {
welcomeLabel.setText(text);
repaint();
}
}
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.Graphics;
public class MiddlePanel extends JPanel {
MiddlePanel() {
super(new BorderLayout());
JButton okButton = new JButton("OK");
okButton.setSize(20, 20);
OKButtonListener okButtonListener = new OKButtonListener();
okButton.addActionListener(okButtonListener);
add(okButton, BorderLayout.EAST);
}
class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
BottomPanel bottomPanel = new BottomPanel();
bottomPanel.setText("New Text");
}
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class App {
public static void main(String[] args) {
JFrame nameFrame = new JFrame("Name Form");
nameFrame.setLayout(new BorderLayout());
nameFrame.setSize(300,150);
nameFrame.setLocationRelativeTo(null);
nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BottomPanel bottomPanel = new BottomPanel();
MiddlePanel middlePanel = new MiddlePanel(bottomPanel);
nameFrame.add(middlePanel, BorderLayout.CENTER);
nameFrame.add(bottomPanel, BorderLayout.SOUTH);
nameFrame.setVisible(true);
}
}
class BottomPanel extends JPanel {
private JLabel welcomeLabel = new JLabel("Old Text");
BottomPanel() {
super(new FlowLayout());
add(welcomeLabel);
}
public void setText(String text) {
welcomeLabel.setText(text);
repaint();
}
}
class MiddlePanel extends JPanel {
private BottomPanel bottomPanel;
MiddlePanel(BottomPanel bottomPanel) {
super(new BorderLayout());
this.bottomPanel = bottomPanel;
JButton okButton = new JButton("OK");
okButton.setSize(20, 20);
OKButtonListener okButtonListener = new OKButtonListener();
okButton.addActionListener(okButtonListener);
add(okButton, BorderLayout.EAST);
}
class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//BottomPanel bottomPanel = new BottomPanel();
bottomPanel.setText("New Text");
}
}
}