JAVA 如何给鼠标按钮设置事件? 我是初学者,想法很简单,通过点击按钮进入下一个界面。 求帮助,在线等!
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassguessextendsMo...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class guess extends MouseAdapter{
JPanel pv,ph;
JFrame f1,f2;
CardLayout mc;
public static void main (String args[]){
guess t=new guess();
t.go();
}
public void go(){
f1=new JFrame("我不说");
pv=new JPanel();
Container c=f1.getContentPane();//是不是关于布局的?果然
pv.setLayout(new BoxLayout(pv,BoxLayout.Y_AXIS));
pv.add(new JButton("Kiss Me!"));
c.add(pv,BorderLayout.CENTER);
pv.addMouseListener(this);
f1.setSize(300,300);
f1.setVisible(true);
}
public void mouseClicked(MouseEvent e){
f2=new JFrame("恭喜我,成功了");
ph=new JPanel();
Container c=f1.getContentPane();
ph.add(new JButton("OVER"));
c.add(ph,BorderLayout.CENTER);
f2.setSize(300,300);
f2.setVisible(true);
mc.next(c);
}
} 展开
import javax.swing.*;
import java.awt.event.*;
public class guess extends MouseAdapter{
JPanel pv,ph;
JFrame f1,f2;
CardLayout mc;
public static void main (String args[]){
guess t=new guess();
t.go();
}
public void go(){
f1=new JFrame("我不说");
pv=new JPanel();
Container c=f1.getContentPane();//是不是关于布局的?果然
pv.setLayout(new BoxLayout(pv,BoxLayout.Y_AXIS));
pv.add(new JButton("Kiss Me!"));
c.add(pv,BorderLayout.CENTER);
pv.addMouseListener(this);
f1.setSize(300,300);
f1.setVisible(true);
}
public void mouseClicked(MouseEvent e){
f2=new JFrame("恭喜我,成功了");
ph=new JPanel();
Container c=f1.getContentPane();
ph.add(new JButton("OVER"));
c.add(ph,BorderLayout.CENTER);
f2.setSize(300,300);
f2.setVisible(true);
mc.next(c);
}
} 展开
展开全部
你的程序我帮你改了一下,修改处见注释。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class guess extends MouseAdapter{
JPanel pv,ph;
JFrame f1,f2;
CardLayout mc;
public static void main (String args[]){
guess t=new guess();
t.go();
}
public void go(){
f1=new JFrame("我不说");
pv=new JPanel();
Container c=f1.getContentPane();//是不是关于布局的?果然
pv.setLayout(new BoxLayout(pv,BoxLayout.Y_AXIS));
JButton jb=new JButton("Kiss Me!"); //这里改动了一下
pv.add(jb); //这里改动了一下
c.add(pv,BorderLayout.CENTER);
jb.addMouseListener(this); //这里改动了一下 pv改成jb
f1.setSize(300,300);
f1.setVisible(true);
}
public void mouseClicked(MouseEvent e){
f2=new JFrame("恭喜我,成功了");
ph=new JPanel();
Container c=f2.getContentPane(); //这里改动了一下 f1改成f2
ph.add(new JButton("OVER"));
c.add(ph,BorderLayout.CENTER);
f2.setSize(300,300);
f2.setVisible(true);
//mc.next(c); //这里注释掉了
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class guess extends MouseAdapter{
JPanel pv,ph;
JFrame f1,f2;
CardLayout mc;
public static void main (String args[]){
guess t=new guess();
t.go();
}
public void go(){
f1=new JFrame("我不说");
pv=new JPanel();
Container c=f1.getContentPane();//是不是关于布局的?果然
pv.setLayout(new BoxLayout(pv,BoxLayout.Y_AXIS));
JButton jb=new JButton("Kiss Me!"); //这里改动了一下
pv.add(jb); //这里改动了一下
c.add(pv,BorderLayout.CENTER);
jb.addMouseListener(this); //这里改动了一下 pv改成jb
f1.setSize(300,300);
f1.setVisible(true);
}
public void mouseClicked(MouseEvent e){
f2=new JFrame("恭喜我,成功了");
ph=new JPanel();
Container c=f2.getContentPane(); //这里改动了一下 f1改成f2
ph.add(new JButton("OVER"));
c.add(ph,BorderLayout.CENTER);
f2.setSize(300,300);
f2.setVisible(true);
//mc.next(c); //这里注释掉了
}
}
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Guess {
private JFrame frame = new JFrame("不说?!");
private Container container = frame.getContentPane();
private JPanel panel = new JPanel();
private JPanel pane1 = new JPanel();
private JPanel pane2 = new JPanel();
private JButton button1 = new JButton("Kiss Me");
private JButton button2 = new JButton("Back,and...");
private JLabel label = new JLabel("Yes, you are sussesed!");
private CardLayout cardl = new CardLayout();
Guess() {
container.setLayout(new BorderLayout());
panel.setLayout(cardl);
container.add(panel,BorderLayout.CENTER);
pane1.setLayout(new FlowLayout(FlowLayout.LEFT));
pane2.setLayout(new FlowLayout(FlowLayout.LEFT));
panel.add(pane1,"a");
panel.add(pane2,"b");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cardl.show(panel,"b");
frame.setTitle("oh! Yes.");
}
});
pane1.add(button1);
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
cardl.show(panel,"a");
frame.setTitle("还是不说?");
}
});
pane2.add(label);
pane2.add(button2);
frame.setSize(300,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
Guess g = new Guess();
}
}
import java.awt.event.*;
import javax.swing.*;
public class Guess {
private JFrame frame = new JFrame("不说?!");
private Container container = frame.getContentPane();
private JPanel panel = new JPanel();
private JPanel pane1 = new JPanel();
private JPanel pane2 = new JPanel();
private JButton button1 = new JButton("Kiss Me");
private JButton button2 = new JButton("Back,and...");
private JLabel label = new JLabel("Yes, you are sussesed!");
private CardLayout cardl = new CardLayout();
Guess() {
container.setLayout(new BorderLayout());
panel.setLayout(cardl);
container.add(panel,BorderLayout.CENTER);
pane1.setLayout(new FlowLayout(FlowLayout.LEFT));
pane2.setLayout(new FlowLayout(FlowLayout.LEFT));
panel.add(pane1,"a");
panel.add(pane2,"b");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cardl.show(panel,"b");
frame.setTitle("oh! Yes.");
}
});
pane1.add(button1);
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
cardl.show(panel,"a");
frame.setTitle("还是不说?");
}
});
pane2.add(label);
pane2.add(button2);
frame.setSize(300,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
Guess g = new Guess();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |