java JPanel问题
我想在panel1中有个button,点击了button后JFRame不变,然后只是panel1被panel2替换,请问怎么做,谢谢大虾了...
我想在panel1中有个button,点击了button后JFRame不变,然后只是panel1被panel2替换,请问怎么做,谢谢大虾了
展开
2个回答
展开全部
按照你的要求,在panel1中有个button,点击了button后JFRame不变,只是panel1被panel2替换.
import java.awt.BorderLayout;
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 H extends JFrame implements ActionListener{
JLabel jl1=new JLabel("第一页");
JButton jb1=new JButton("下一页");
JPanel jp1=new JPanel();
H(){
jp1.add(jl1);
jp1.add(jb1);
add(jp1);
jb1.addActionListener(this);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new H();
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb1){
remove(jp1);
MyPanel myPanel=new MyPanel();
add(myPanel);
setVisible(true);
}
}
}
class MyPanel extends JPanel{
JLabel jl2=new JLabel("第二页");
MyPanel(){
add(jl2);
setVisible(true);
}
}
import java.awt.BorderLayout;
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 H extends JFrame implements ActionListener{
JLabel jl1=new JLabel("第一页");
JButton jb1=new JButton("下一页");
JPanel jp1=new JPanel();
H(){
jp1.add(jl1);
jp1.add(jb1);
add(jp1);
jb1.addActionListener(this);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new H();
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb1){
remove(jp1);
MyPanel myPanel=new MyPanel();
add(myPanel);
setVisible(true);
}
}
}
class MyPanel extends JPanel{
JLabel jl2=new JLabel("第二页");
MyPanel(){
add(jl2);
setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询