求大神帮忙!JAVA中布局后的接收问题。
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class zhujiemian {
JFrame JJ =new JFrame();
JPanel JP = new JPanel();
public void zong(){
JJ.setVisible(true);
JJ.add(JP);
JJ.setTitle("QQ登录程序");
JJ.setSize(500,300);
JJ.setLocation(200,100);
}
public void denglu(){
JP.setLayout(null);
JButton JB1 = new JButton("登录");
JButton JB2 = new JButton("退出");
JB1.setBounds(100,150,90,28);
JB2.setBounds(300,150,90,28);
JP.add(JB1);
JP.add(JB2);
JB1.addMouseListener(new JB1Listener());
JB2.addMouseListener(new JB2Listener());
}
class JB1Listener implements MouseListener{
public void mouseReleased(MouseEvent e){
JFrame JF =new JFrame();
JF.setVisible(true);
JF.setTitle("QQ登录程序");
JF.setSize(200,400);
JF.setLocation(1150,30);
JJ.setVisible(false);
JPanel JP = new JPanel();
JF.add(JP);
JP.setLayout(null);
JButton jb1 = new JButton("空间 ");
jb1.setBounds(80,60,30,28);
JP.add(jb1);
JButton jb2 = new JButton("微博 ");
jb2.setBounds(130,60,30,28);
JP.add(jb2);
JLabel jb = new JLabel("未央不见");
jb.setBounds(90,0,70,70);
JP.add(jb);
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
}
class JB2Listener implements MouseListener{
public void mouseReleased(MouseEvent e) {
System.exit(0);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
}
public void Label(){
JLabel JL1 = new JLabel("QQ号码");
JL1.setBounds(100,28,50,50);
JTextField JT1 = new JTextField(10);
JT1.setBounds(165,40,150,25);
JLabel JL2 = new JLabel(" 密码");
JL2.setBounds(100,68,50,50);
JTextField JT2 = new JTextField(10);
JT2.setBounds(165,80,150,25);
JP.add(JL1);
JP.add(JL2);
JP.add(JT1);
JP.add(JT2);
JT1.getText();
JT2.getText();
}
public static void main(String args[]){
zhujiemian z = new zhujiemian();
z.Label();
z.zong();
z.denglu();
}
}
我知道我的布局太差了。求大神指导,我想在JB1Listener中 接收我自己定义的Label方法中的JT1和JT2里面的内容
------解决方案--------------------你把JLabel定义在方法内部当然不容易获取该对象了。
如果你吧JLabel定义为类的成员对象,在方法内初始化,就能获取该对象了
------解决方案--------------------
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
class zhujiemian {
JFrame JJ=new JFrame();
JPanel JP=new JPanel();
JTextField JT1, JT2;
public void zong() {
JJ.setVisible(true);
JJ.add(JP);
JJ.setTitle("QQ登录程序");
JJ.setSize(500,300);
JJ.setLocation(200,100);
}
public void denglu() {
JP.setLayout(null);
JButton JB1=new JButton("登录");
JButton JB2=new JButton("退出");
JB1.setBounds(100,150,90,28);
JB2.setBounds(300,150,90,28);
JP.add(JB1);
JP.add(JB2);
JB1.addMouseListener(new JB1Listener());
JB2.addMouseListener(new JB2Listener());
}
class JB1Listener implements MouseListener {
public void mouseReleased(MouseEvent e) {
System.out.println(JT1.getText());
System.out.println(JT2.getText());
JFrame JF=new JFrame();
JF.setVisible(true);
JF.setTitle("QQ登录程序");
JF.setSize(200,400);
JF.setLocation(1150,30);
JJ.setVisible(false);
JPanel JP=new JPanel();
JF.add(JP);
JP.setLayout(null);
JButton jb1=new JButton("空间 ");
jb1.setBounds(80,60,30,28);