Java,JTextArea()问题?
怎么在一个类中设置另一个类里的JTextArea(),我在一个类中定义了JTextArea(),下载我想在另一个类中用setText()方法设置前面的那个JTextAre...
怎么在一个类中设置另一个类里的JTextArea(),我在一个类中定义了JTextArea(),下载我想在另一个类中用setText()方法设置前面的那个JTextArea(),怎么设置?
定义了一个类A ,然后再A里定义了jt = new JTextArea(),又定义了类B,然后我现在B类中,用jt.setText();给其设置内容,但是不可以,怎么解决? 展开
定义了一个类A ,然后再A里定义了jt = new JTextArea(),又定义了类B,然后我现在B类中,用jt.setText();给其设置内容,但是不可以,怎么解决? 展开
2个回答
展开全部
我帮你改好了
package birthdaytree;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class BirthdayTree extends JFrame {
private static final long serialVersionUID = -1908556835892773567L;
private JTextArea jt;
public JTextArea getJt() {
return jt;
}
public BirthdayTree() {
JScrollPane jtPane = new JScrollPane(jt = new JTextArea());
jt.setFont(new Font("Serif", Font.PLAIN, 16));
jt.setLineWrap(true);
jt.setWrapStyleWord(true);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("365天生日性格大全!");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("蔬菜");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("水果");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("礼品");
DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("家用小物件");
root.add(node1);
root.add(node2);
root.add(node3);
root.add(node4);
DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("白菜");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("大蒜");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("土豆");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("苹果");
node2.add(leafnode);
leafnode = new DefaultMutableTreeNode("香蕉");
node2.add(leafnode);
leafnode = new DefaultMutableTreeNode("西瓜");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("礼品");
node3.add(leafnode);
leafnode = new DefaultMutableTreeNode("茅台酒");
node3.add(leafnode);
leafnode = new DefaultMutableTreeNode("营养麦片");
node3.add(leafnode);
leafnode = new DefaultMutableTreeNode("保健食品");
node3.add(leafnode);
leafnode = new DefaultMutableTreeNode("味精");
node4.add(leafnode);
leafnode = new DefaultMutableTreeNode("酱油");
node4.add(leafnode);
leafnode = new DefaultMutableTreeNode("洗洁精");
node4.add(leafnode);
leafnode = new DefaultMutableTreeNode("保鲜袋");
node4.add(leafnode);
JTree tree = new JTree(root);
tree.setEditable(false); // 设置JTree为不可编辑
tree.addMouseListener(new MouseHandle(this));// 在JTree中加入检测Mouse事件,以便取得节点名称
JScrollPane js = new JScrollPane();
js.setViewportView(tree); // 将树组件添加到滚动条组件中
setLayout(new BorderLayout());
getContentPane().add(jtPane, BorderLayout.CENTER);
getContentPane().add(js, BorderLayout.WEST);
this.setSize(900, 500);
this.setVisible(true);
}
public static void main(String[] args) {
new BirthdayTree();
}
}
class MouseHandle extends MouseAdapter {
private BirthdayTree birthdayTree;
// 处理Mouse点选事件
@Override
public void mouseClicked(MouseEvent e) {
String nodeName;
JTree tree = (JTree) e.getSource(); // 当单击鼠标,在右边会显示出相应的数据信息
int rowLocation = tree.getRowForLocation(e.getX(), e.getY());
TreePath treepath = tree.getPathForRow(rowLocation);
if (null == treepath) {
return;
}
TreeNode treenode = (TreeNode) treepath.getLastPathComponent();
nodeName = treenode.toString();
getBirthdayTree().getJt().setText(nodeName);
}
public BirthdayTree getBirthdayTree() {
return birthdayTree;
}
public MouseHandle(BirthdayTree birthdayTree) {
this.birthdayTree = birthdayTree;
}
}
package birthdaytree;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class BirthdayTree extends JFrame {
private static final long serialVersionUID = -1908556835892773567L;
private JTextArea jt;
public JTextArea getJt() {
return jt;
}
public BirthdayTree() {
JScrollPane jtPane = new JScrollPane(jt = new JTextArea());
jt.setFont(new Font("Serif", Font.PLAIN, 16));
jt.setLineWrap(true);
jt.setWrapStyleWord(true);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("365天生日性格大全!");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("蔬菜");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("水果");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("礼品");
DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("家用小物件");
root.add(node1);
root.add(node2);
root.add(node3);
root.add(node4);
DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("白菜");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("大蒜");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("土豆");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("苹果");
node2.add(leafnode);
leafnode = new DefaultMutableTreeNode("香蕉");
node2.add(leafnode);
leafnode = new DefaultMutableTreeNode("西瓜");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("礼品");
node3.add(leafnode);
leafnode = new DefaultMutableTreeNode("茅台酒");
node3.add(leafnode);
leafnode = new DefaultMutableTreeNode("营养麦片");
node3.add(leafnode);
leafnode = new DefaultMutableTreeNode("保健食品");
node3.add(leafnode);
leafnode = new DefaultMutableTreeNode("味精");
node4.add(leafnode);
leafnode = new DefaultMutableTreeNode("酱油");
node4.add(leafnode);
leafnode = new DefaultMutableTreeNode("洗洁精");
node4.add(leafnode);
leafnode = new DefaultMutableTreeNode("保鲜袋");
node4.add(leafnode);
JTree tree = new JTree(root);
tree.setEditable(false); // 设置JTree为不可编辑
tree.addMouseListener(new MouseHandle(this));// 在JTree中加入检测Mouse事件,以便取得节点名称
JScrollPane js = new JScrollPane();
js.setViewportView(tree); // 将树组件添加到滚动条组件中
setLayout(new BorderLayout());
getContentPane().add(jtPane, BorderLayout.CENTER);
getContentPane().add(js, BorderLayout.WEST);
this.setSize(900, 500);
this.setVisible(true);
}
public static void main(String[] args) {
new BirthdayTree();
}
}
class MouseHandle extends MouseAdapter {
private BirthdayTree birthdayTree;
// 处理Mouse点选事件
@Override
public void mouseClicked(MouseEvent e) {
String nodeName;
JTree tree = (JTree) e.getSource(); // 当单击鼠标,在右边会显示出相应的数据信息
int rowLocation = tree.getRowForLocation(e.getX(), e.getY());
TreePath treepath = tree.getPathForRow(rowLocation);
if (null == treepath) {
return;
}
TreeNode treenode = (TreeNode) treepath.getLastPathComponent();
nodeName = treenode.toString();
getBirthdayTree().getJt().setText(nodeName);
}
public BirthdayTree getBirthdayTree() {
return birthdayTree;
}
public MouseHandle(BirthdayTree birthdayTree) {
this.birthdayTree = birthdayTree;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询