我定义了两个继承JFrame的类A和B!我在类A 中设定了定义了面板的大小 为300,400. 还有一个按钮事件。我在类B中定义了面板大小 300,500 也有一个按钮。
我现在是让 类A 的实例a可见。然后点击实例a中的按钮,则让a.Visible(false);并让类B的实例b显示出来。然后点击实例b的按钮。让a.Visible(true);
并且让实例b.Visible(false);请问改怎么在做
比如:class A{
A(){
button("按钮1".addActionListener(this); }
public void actionPerformed(ActionEvent e)
{
该怎么写?
}
}
class B{
B(){
button("按钮2".addActionListener(this); }
public void actionPerformed(ActionEvent e)
{
该怎么写?
}
} 请比较懂的各位java高手给我讲解讲解。感激不尽。写的好加分。
我现在是让 类A 的实例a可见。然后点击实例a中的按钮,则让a.Visible(false);并让类B的实例b显示出来。然后点击实例b的按钮。让a.Visible(true);
并且让实例b.Visible(false);请问改怎么在做
比如:class A{
A(){
button("按钮1".addActionListener(this); }
public void actionPerformed(ActionEvent e)
{
该怎么写?
}
}
class B{
B(){
button("按钮2".addActionListener(this); }
public void actionPerformed(ActionEvent e)
{
该怎么写?
}
} 请比较懂的各位java高手给我讲解讲解。感激不尽。写的好加分。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;import javax.swing.JButton;
import javax.swing.JFrame;public class Test {
public static void main(String[] args){
new A();
}
}class A{
JFrame frame;
JButton button;
public A(){
frame=new JFrame();
frame.setTitle("A");
button=new JButton("ButtonA");
frame.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
B b=new B();
JFrame frameB=b.getFrame();
frameB.setVisible(true);
frame.setVisible(false);
}
});
frame.setVisible(true);
frame.pack();
}
public JFrame getFrame(){
return frame;
}
}class B{
JFrame frame;
JButton button;
public B(){
frame=new JFrame();
frame.setTitle("B");
button=new JButton("ButtonB");
frame.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
A a=new A();
JFrame frameA=a.getFrame();
frameA.setVisible(true);
frame.setVisible(false);
}
});
frame.setVisible(true);
frame.pack();
}
public JFrame getFrame(){
return frame;
}
}
我的的条件还有一个没写:就是一共只产生2个JFrame对象。效果是:先让A对象显示。然后点击按钮后B对象显示,并且A对象不显示。再点按钮B对象不显示。A对象显示(这里的A 还是最开始的的A 不是新创建的)。
你这个写的确是不停的产生新的对象。只是把以前的对象设置为不可见。所以看上去是只有1个对象在屏幕上
如果你能继续完成我 就把分都给你。如果确实也没人完成。我也只好送给你 以报熬夜之苦。
在两个类中都定义一个JFrame类型的成员变量来保存这个frame
比如:
class A extends JFrame{
JFrame friend;
public void setFriend(JFrame frame){
this.friend = frame;
} public void actionPerformed(){
if(friend != null){
this.setVisible(false);
this.friend.setVisible(true);
}
}
}
在类B中也仿照这个写就行了
把它们对方互相设置一下
我是楼主 我昨天自己写了2个类://QQ.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;public class QQ extends JFrame implements ActionListener {
String currentPattern; public QQ( ){
setTitle("QQ用户登录");
setSize(330,245);
Container contentPane=getContentPane();
contentPane.setLayout(null);
JButton btnSet=new JButton("设置↓");
btnSet.setBounds(100,180,80,20);
contentPane.add(btnSet);
btnSet.addActionListener(this); //事件侦听
}
public void actionPerformed(ActionEvent e)
{
QQShow show=new QQShow();
this.setVisible(false);
show.setVisible(true);
}
public static void main(String [] args){
QQ qq=new QQ();
qq.setVisible(true);
qq.setResizable(false);
qq. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//QQShow.javaimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;public class QQShow extends JFrame implements ActionListener {
String currentPattern; public QQShow( ){
setTitle("QQ用户登录");
setSize(330,500);
Container contentPane=getContentPane();
contentPane.setLayout(null);
JButton btnSet=new JButton("设置");
btnSet.setBounds(100,180,80,20);
contentPane.add(btnSet);
btnSet.addActionListener(this); //事件侦听
}
public void actionPerformed(ActionEvent e)
{
QQ qq=new QQ();
this.setVisible(false);
qq.setVisible(true); }
}我这样做 总是产生新的JFrame对象。 只是由于设置了不可见所以看上去只有一个对象在上面。我现在要做的就是如何在2个类的事件里调用对方的setVisible属性。效果是:先让A对象显示。然后点击按钮后B对象显示,并且A对象不显示。再点按钮B对象不显示。A对象显示(这里的A 还是最开始的的A 不是新创建的)。
//QQ.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;public class QQ extends JFrame implements ActionListener {
String currentPattern;JFrame show = null; //加一个属性!注意看这里public QQ( ){setTitle("QQ用户登录");
setSize(330,245);Container contentPane=getContentPane();
contentPane.setLayout(null); JButton btnSet=new JButton("设置↓");
btnSet.setBounds(100,180,80,20);
contentPane.add(btnSet);
btnSet.addActionListener(this); //事件侦听
}
public void actionPerformed(ActionEvent e)
{
//这里稍微改一下
if( show == null ){
show=new QQShow(this);
}
this.setVisible(false);
show.setVisible(true);
}public static void main(String [] args){
QQ qq=new QQ();
qq.setVisible(true);
qq.setResizable(false);
qq. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//QQShow.javaimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;public class QQShow extends JFrame implements ActionListener {
String currentPattern;
JFrame qq = null;public QQShow(JFrame qq ){//构造方法加一个参数
this.qq = qq;
setTitle("QQ用户登录");
setSize(330,500);
Container contentPane=getContentPane();
contentPane.setLayout(null);
JButton btnSet=new JButton("设置");
btnSet.setBounds(100,180,80,20);
contentPane.add(btnSet);
btnSet.addActionListener(this); //事件侦听
}
public void actionPerformed(ActionEvent e)
{
if( qq != null ){//这里判断一下,省的出错
this.setVisible(false);
qq.setVisible(true);
}
}
}