JAVA如何通过按钮来实现界面的切换?
展开全部
完整代码完全是按你要求写的。
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TESTswing extends JPanel implements ActionListener{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JButton btn1 = new JButton("Button1");
JButton btn2 = new JButton("Button2");
public TESTswing()
{
p1.add(btn1);
p1.add(btn2);
p2.add(new JLabel("I’m is p2"));
p3.add(new JLabel("I’m is p3"));
p2.setVisible(false);
p3.setVisible(false);
this.add(p1);
this.add(p2);
this.add(p3);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(200, 200);
Container contentPane = frame.getContentPane();
contentPane.add(new TESTswing());
frame.show();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String cmd = e.getActionCommand();
if (cmd.equals("Button1"))
{
p2.setVisible(true);
p3.setVisible(false);
}
else
{
p2.setVisible(false);
p3.setVisible(true);
}
}
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TESTswing extends JPanel implements ActionListener{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JButton btn1 = new JButton("Button1");
JButton btn2 = new JButton("Button2");
public TESTswing()
{
p1.add(btn1);
p1.add(btn2);
p2.add(new JLabel("I’m is p2"));
p3.add(new JLabel("I’m is p3"));
p2.setVisible(false);
p3.setVisible(false);
this.add(p1);
this.add(p2);
this.add(p3);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(200, 200);
Container contentPane = frame.getContentPane();
contentPane.add(new TESTswing());
frame.show();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String cmd = e.getActionCommand();
if (cmd.equals("Button1"))
{
p2.setVisible(true);
p3.setVisible(false);
}
else
{
p2.setVisible(false);
p3.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询