不太懂。。 我简化代码一下吧 //LeftPane.java package testGUI;import java.awt.Dimension;import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; public class LeftPane extends JInternalFrame{
private static JScrollPane jp = new JScrollPane();
public static int count=0; public static JPanel panel = new JPanel(); public LeftPane() { panel.setPreferredSize(new Dimension(200,5*100));//订单的数量*每个订单的高度 panel.add(new Item()); panel.add(new Item()); jp.getViewport().add(panel); jp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
} public static void main(String[] args) { new MainView(); } } 点击Item里面的按钮,让info这个JTextArea的内容改变
懒的看代码。给你写了个Demo,你看懂了之后自己改到你的代码里面去吧。主要看按钮的ActionListener [code=java][/code import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class Test { public Test() { init(); } public void init(){ JFrame frame = new JFrame();
JPanel panel1 = new JPanel(); JTextArea ta = new JTextArea(5,20); ta.setText("Before-----"); panel1.add(ta); JPanel panel2 = new JPanel(); JButton btn = new JButton("改变内容"); btn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { JButton b = (JButton)e.getSource();//获取点击的按钮 btn JPanel pan2 = (JPanel)b.getParent();//获取按钮的父容器 panel2 JPanel panel = (JPanel)pan2.getParent();//获取panel JPanel pan1 = (JPanel)panel.getComponent(0);//获取panel1 下标是按照添加的顺序从0开始 JTextArea ta = (JTextArea)pan1.getComponent(0);//获取JTextArea . ta.setText("-----After");//改变文本内容 } }); panel2.add(btn);
你把父组件设置成成员变量不就好了
不太懂。。
我简化代码一下吧
//LeftPane.java
package testGUI;import java.awt.Dimension;import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class LeftPane extends JInternalFrame{
private static JScrollPane jp = new JScrollPane();
public static int count=0; public static JPanel panel = new JPanel(); public LeftPane()
{
panel.setPreferredSize(new Dimension(200,5*100));//订单的数量*每个订单的高度
panel.add(new Item());
panel.add(new Item()); jp.getViewport().add(panel);
jp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
this.add(jp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//Item.java
package testGUI;import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;public class Item extends JPanel implements ActionListener{ private JButton printButton=null; private String info="";
public Item()
{
super();
System.out.println("正在加入订单");
setPreferredSize(new Dimension(200,100));
setBorder(BorderFactory.createLineBorder(Color.black));
setLayout(null);
printButton=new JButton("打印");
printButton.addActionListener(this);
printButton.setBounds(5,60,60,30);
add(printButton);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==printButton)
{
/*
Component tmp=((Component) e.getSource()).getParent().getParent().getParent();
((Component) e.getSource()).getParent().getParent().remove(((Component) e.getSource()).getParent());
*/
System.out.println("Hello");
}
}
public static void main(String[] args)
{
}}
//MainView.java
package testGUI;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;public class MainView extends JFrame{ private JScrollPane rightPanel=null;
private static JTextArea info=null;
private LeftPane leftPanel=new LeftPane(); public MainView()
{
this.setSize(430,500);
this.setVisible(true);
rightPanel=new JScrollPane();
info=new JTextArea();
info.setEditable(false);
info.setLineWrap(true);
rightPanel=new JScrollPane(info);
rightPanel.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
rightPanel.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
leftPanel.setBounds(-5,-33,230,510);
rightPanel.setBounds(225,0,200,475);
this.add(leftPanel);
this.add(rightPanel);
}
public static void main(String[] args)
{
new MainView();
}
}
点击Item里面的按钮,让info这个JTextArea的内容改变
[code=java][/code
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Test {
public Test() {
init();
}
public void init(){
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JTextArea ta = new JTextArea(5,20);
ta.setText("Before-----");
panel1.add(ta);
JPanel panel2 = new JPanel();
JButton btn = new JButton("改变内容");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource();//获取点击的按钮 btn
JPanel pan2 = (JPanel)b.getParent();//获取按钮的父容器 panel2
JPanel panel = (JPanel)pan2.getParent();//获取panel
JPanel pan1 = (JPanel)panel.getComponent(0);//获取panel1 下标是按照添加的顺序从0开始
JTextArea ta = (JTextArea)pan1.getComponent(0);//获取JTextArea .
ta.setText("-----After");//改变文本内容
}
});
panel2.add(btn);
JPanel panel = new JPanel();
panel.add(panel1);
panel.add(panel2);
frame.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(500, 200, 500, 500);
}
public static void main(String[] args) {
new Test();
}
}