在一个JPanel如何获取主界面JFrame其他JPanel的信息

cut21 2008-05-17 05:26:26
我在一个JFrame里面加了多个JPanel,每个JPanel都用不同的类编写的。
我想在其中一个JPanel(这里叫做jPanel1)里面添加一个按钮监听来控制另外一个JPanel(比如jPanel2)里面文本框显示的内容。
请问是否能只在jPanel类里面做相应的操作就可以获取jPanel2,并对其进行修改。
...全文
485 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
馒头虫 2011-05-08
  • 打赏
  • 举报
回复
JFrame jf=(JFrame)getRootPane().getParent();
zwgs1985 2008-05-18
  • 打赏
  • 举报
回复
CustomizePanel_1的代码
import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CustomizePanel_1 extends JPanel {

private JButton btn = new JButton();

public CustomizePanel_1() {
super();
this.init();
}

private void init() {
this.setLayout(new BorderLayout());
this.add(new JLabel("CustomizePanel_1"));
this.add(this.btn, BorderLayout.SOUTH);
}

public JButton getControlBtn() {
return this.btn;
}
}

CustomizePanel_2的代码
import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CustomizePanel_2 extends JPanel {

private JTextField tf = new JTextField();

public CustomizePanel_2() {
super();
this.init();
}

private void init() {
this.setLayout(new BorderLayout());
this.add(new JLabel("CustomizePanel_2"));
this.add(this.tf, BorderLayout.SOUTH);
}

public JTextField getTextField() {
return this.tf;
}
}

TestClass的代码
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class TestClass extends JFrame {

public TestClass() {
super();
this.init();
}

private void init() {
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(1, 2));
CustomizePanel_1 customizePanel_1 = new CustomizePanel_1();
contentPane.add(customizePanel_1);
final CustomizePanel_2 customizePanel_2 = new CustomizePanel_2();
contentPane.add(customizePanel_2);
JButton btn = customizePanel_1.getControlBtn();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JTextField textField = customizePanel_2.getTextField();
String text = textField.getText();
// your logoc
if (text == null || "".equals(text)) {
textField.setText("change text");
}
}
});
}

/**
* @param args
*/
public static void main(String[] args) {
TestClass tc = new TestClass();
tc.setVisible(true);
tc.pack();
tc.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

}
实现方式不是很好,你看下是不是你想要得效果
cut21 2008-05-17
  • 打赏
  • 举报
回复
我还不是很明白
用getParent()方法获取了父组件后如何再获取另外一个jPanel并对其进行操作?
OneLoveQ 2008-05-17
  • 打赏
  • 举报
回复
.getParent()方法
我这里有一个例子
JFRAME里面有一个JPanel里面又有一个JPanel,这个JPanel里面有一个关闭按钮。
也是分开写的。
((JFrame)((JRootPane)((JLayeredPane)((JPanel)((JPanel)SearchAll.this.getParent()).getParent()).getParent()).getParent()).getParent()).dispose();
cut21 2008-05-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 OneLoveQ 的回复:]
先取得它的父组件!然后再找找呗
[/Quote]
如何获取父组件,用什么方法?
OneLoveQ 2008-05-17
  • 打赏
  • 举报
回复
先取得它的父组件!然后再找找呗

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧