在JBUILDER2006下运行下面的代码..为什么画面不会自动显示出来?要点下全屏或拉下边框才能显示...
还有..为什么在点jButton3时.响应的动作会分两步执行??
谢谢!!
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.MouseAdapter;public class test2 extends JFrame {
public test2() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
} public static void main(String[] args) {
test2 test2 = new test2();
} private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
show();
setSize(800, 600);
jButton1.setText("jButton1");
jButton1.addActionListener(new test2_jButton1_actionAdapter(this));
jButton1.addMouseListener(new test2_jButton1_mouseAdapter(this));
jButton3.addMouseListener(new test2_jButton3_mouseAdapter(this));
jButton3.addActionListener(new test2_jButton3_actionAdapter(this));
this.getContentPane().add(jButton1, new XYConstraints(5, 68, 138, 56));
jButton4.setText("jButton4");
jButton3.setText("jButton3");
this.getContentPane().add(jButton2, new XYConstraints(5, 143, 98, 31));
this.getContentPane().add(jButton3, new XYConstraints(5, 192, 135, 53));
this.getContentPane().add(jButton4, new XYConstraints(5, 260, 96, 26));
jButton2.setText("jButton2");
jButton4.setVisible(false);
} XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
public void jButton1_actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("jButton1")){
jButton3.setLocation(5, 192);
jButton2.setVisible(true);
jButton4.setVisible(false);
} } public void jButton3_actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("jButton3")){
jButton3.setLocation(5, 143);
jButton2.setVisible(false);
jButton4.setVisible(true);
jButton4.setLocation(5, 192);
}
}}
class test2_jButton1_mouseAdapter extends MouseAdapter {
private test2 adaptee;
test2_jButton1_mouseAdapter(test2 adaptee) {
this.adaptee = adaptee;
}
}class test2_jButton1_actionAdapter implements ActionListener {
private test2 adaptee;
test2_jButton1_actionAdapter(test2 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class test2_jButton3_mouseAdapter extends MouseAdapter {
private test2 adaptee;
test2_jButton3_mouseAdapter(test2 adaptee) {
this.adaptee = adaptee;
}
}
class test2_jButton3_actionAdapter implements ActionListener {
private test2 adaptee;
test2_jButton3_actionAdapter(test2 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
还有..为什么在点jButton3时.响应的动作会分两步执行??
谢谢!!
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.MouseAdapter;public class test2 extends JFrame {
public test2() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
} public static void main(String[] args) {
test2 test2 = new test2();
} private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
show();
setSize(800, 600);
jButton1.setText("jButton1");
jButton1.addActionListener(new test2_jButton1_actionAdapter(this));
jButton1.addMouseListener(new test2_jButton1_mouseAdapter(this));
jButton3.addMouseListener(new test2_jButton3_mouseAdapter(this));
jButton3.addActionListener(new test2_jButton3_actionAdapter(this));
this.getContentPane().add(jButton1, new XYConstraints(5, 68, 138, 56));
jButton4.setText("jButton4");
jButton3.setText("jButton3");
this.getContentPane().add(jButton2, new XYConstraints(5, 143, 98, 31));
this.getContentPane().add(jButton3, new XYConstraints(5, 192, 135, 53));
this.getContentPane().add(jButton4, new XYConstraints(5, 260, 96, 26));
jButton2.setText("jButton2");
jButton4.setVisible(false);
} XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
public void jButton1_actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("jButton1")){
jButton3.setLocation(5, 192);
jButton2.setVisible(true);
jButton4.setVisible(false);
} } public void jButton3_actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("jButton3")){
jButton3.setLocation(5, 143);
jButton2.setVisible(false);
jButton4.setVisible(true);
jButton4.setLocation(5, 192);
}
}}
class test2_jButton1_mouseAdapter extends MouseAdapter {
private test2 adaptee;
test2_jButton1_mouseAdapter(test2 adaptee) {
this.adaptee = adaptee;
}
}class test2_jButton1_actionAdapter implements ActionListener {
private test2 adaptee;
test2_jButton1_actionAdapter(test2 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class test2_jButton3_mouseAdapter extends MouseAdapter {
private test2 adaptee;
test2_jButton3_mouseAdapter(test2 adaptee) {
this.adaptee = adaptee;
}
}
class test2_jButton3_actionAdapter implements ActionListener {
private test2 adaptee;
test2_jButton3_actionAdapter(test2 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
显示出来没大小 是因为LZ把setSize() 方法放在显示方法之后了 2个方法换下位置就好了
那个事件 没看懂什么意思 什么分2部执行?
public void jButton3_actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("jButton3")){
jButton3.setLocation(5, 143);
jButton2.setVisible(false);
jButton4.setVisible(true);
jButton4.setLocation(5, 192);
}
}
就是这事件中..按下jButton3后先执行jButton2.setVisible(false);jButton4.setVisible(true);再按一下后才执行jButton3.setLocation(5, 143);jButton4.setLocation(5, 192); 要按两下才执行完四条代码..
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.MouseAdapter;public class test2 extends JFrame {
public test2() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
} public static void main(String[] args) {
test2 test2 = new test2();
} private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
this.setTitle("*****");
this.setVisible(true);
jButton1.setText("jButton1");
jButton1.addActionListener(new test2_jButton1_actionAdapter(this));
jButton3.addActionListener(new test2_jButton3_actionAdapter(this));
this.getContentPane().add(jButton2, new XYConstraints(5, 143, 98, 31));
this.getContentPane().add(jButton3, new XYConstraints(5, 192, 135, 53));
this.getContentPane().add(jButton4, new XYConstraints(5, 260, 96, 26));
jButton4.setText("jButton4");
jButton3.setText("jButton3");
jButton2.setText("jButton2");
} XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
public void jButton1_actionPerformed(ActionEvent e) {
jButton3.setLocation(5, 192);
this.getContentPane().add(jButton2, new XYConstraints(5, 143, 98, 31));
this.remove(jButton4);
this.validate();
this.repaint();
} public void jButton3_actionPerformed(ActionEvent e) {
this.getContentPane().add(jButton1, new XYConstraints(5, 68, 138, 56));
this.remove(jButton2);
this.validate();
this.repaint();
jButton3.setLocation(5, 143);
jButton4.setLocation(5, 192);
}}class test2_jButton1_actionAdapter implements ActionListener {
private test2 adaptee;
test2_jButton1_actionAdapter(test2 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}class test2_jButton3_actionAdapter implements ActionListener {
private test2 adaptee;
test2_jButton3_actionAdapter(test2 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
这个就可以了
============================
package test2;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.MouseAdapter;public class test2 extends JFrame {
public test2() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
} public static void main(String[] args) {
test2 test2 = new test2();
} private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
this.setTitle("*****");
this.setVisible(true);
jButton1.setText("jButton1");
jButton1.addActionListener(new test2_jButton1_actionAdapter(this));
jButton3.addActionListener(new test2_jButton3_actionAdapter(this));
this.getContentPane().add(jButton1, new XYConstraints(5, 68, 138, 56));
this.getContentPane().add(jButton2, new XYConstraints(5, 143, 98, 31));
this.getContentPane().add(jButton3, new XYConstraints(5, 192, 135, 53)); jButton4.setText("jButton4");
jButton3.setText("jButton3");
jButton2.setText("jButton2"); } XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
public void jButton1_actionPerformed(ActionEvent e) {
jButton3.setLocation(5, 192);
this.getContentPane().add(jButton2, new XYConstraints(5, 143, 98, 31));
this.remove(jButton4);
this.validate();
this.repaint();
} public void jButton3_actionPerformed(ActionEvent e) {
this.getContentPane().add(jButton4, new XYConstraints(5, 260, 96, 26));
this.remove(jButton2);
this.validate();
this.repaint();
jButton3.setLocation(5, 143);
jButton4.setLocation(5, 192);
}}
class test2_jButton1_actionAdapter implements ActionListener {
private test2 adaptee;
test2_jButton1_actionAdapter(test2 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}class test2_jButton3_actionAdapter implements ActionListener {
private test2 adaptee;
test2_jButton3_actionAdapter(test2 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
但我想请问下为什么我的代码运行时..只显示背景颜色.不显示里面的组件啊?
==================================
我不知道你为什么能使用show()方法来显示窗口 好像show方法已经过期了 建议你使用setVisible(true);
因为你把setSize()放到显示窗口后面了 所以显示不出来组件 2个语句换下位置就可以了
因为你把setSize()放到显示窗口后面了 所以显示不出来组件 2个语句换下位置就可以了
=======================================
哦哦...
还有问题就是
public void jButton3_actionPerformed(ActionEvent e) {
jButton3.setLocation(5, 143);
jButton2.setVisible(false);
jButton4.setVisible(true);
jButton4.setLocation(5, 192);
}
为什么这段代码运行时会分两步执行??
问题较多..麻烦了..谢谢.