日期:2014-05-20 浏览次数:20953 次
import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class TestGetBtn extends JFrame { public TestGetBtn() { window(); } public void window() { this.setLayout(null); MyPanel p = new MyPanel(20,20,this); this.setSize(300,240); this.setVisible(true); this.setResizable(false); this.setTitle("123456789"); this.setDefaultCloseOperation(3); } class MyPanel{ JButton btn = new JButton("OK"); JTextField txt = new JTextField(); public MyPanel(int x,int y,JFrame jf) { JPanel panel = new JPanel(); panel.setLayout(null); panel.setBounds(x, y, 300, 240); btn.setBounds(150, 70, 100, 100); txt.setBounds(30, 30, 100, 30); panel.add(btn); panel.add(txt); jf.add(panel); } } public static void main(String[] args) { new TestGetBtn(); } }
public class TestGetBtn { public static void main(String[] args) { MainFrame f=new MainFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } class MainFrame extends JFrame{ public MainFrame(){ setSize(500,300); setLocation(150,200); setTitle("主界面"); setResizable(false); MainPanel p=new MainPanel(); this.add(p); } } class MainPanel extends JPanel{ private JLabel label1=new JLabel("测试"); private JTextField text1=new JTextField(30); private JButton b1=new JButton("确定"); public MainPanel(){ setLayout(null); label1.setBounds(60, 50, 100, 30); text1.setBounds(190, 50, 200, 30); b1.setBounds(150,100,100,40); add(label1); add(text1); add(b1); b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, text1.getText()); } }); } }
------解决方案--------------------
你可以为多个文本框增加监听阿,都由另一个panel来响应,刚学Java不久还是应该多自己做一些例子尽量自己先解决这样才能进步的更快
------解决方案--------------------
主要还是解决让PanelB能认识PanelA的问题,VE里把组件定义成成员,通过get获取,你要在B里用A的组件值,把A的对象带给B,在B中去通过GET获得相应的组件,再去取它文本