在 一个 JFrame里添加多个JPanel, 用JButton可指定某个 JPenel切换。。怎么做?
packageabc;importjava.awt.BorderLayout;importjava.awt.CardLayout;importjava.awt.Color...
package abc;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;
public class asdf extends JFrame {
private JPanel con;
private CardLayout cl;
private JPanel qq,tt;
private JButton aa,bb;
private JFrame zz;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
asdf frame = new asdf();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public asdf() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(null);
con = new JPanel();
con.setBorder(new EmptyBorder(50, 50, 50, 50));
setContentPane(con);
con.setLayout(null);
qq = new JPanel();
qq.setBounds(101, 60, 329, 290);
qq.setBackground(Color.BLACK);
con.add(qq);
tt = new JPanel();
FlowLayout flowLayout = (FlowLayout) tt.getLayout();
tt.setBounds(0, 60, 363, 290);
tt.setBackground(Color.RED);
con.add(tt);
cl = new CardLayout();
cl.preferredLayoutSize(con);
cl.addLayoutComponent(qq,"qq");
cl.addLayoutComponent(tt,"tt");
//cl.show(con, "qq");
aa = new JButton("");
aa.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//cl.next(con);
}
});
aa.setBounds(10,10,50,30);
con.add(aa);
}
}
新手求教啊。。
好心人帮我改下咯。。 展开
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;
public class asdf extends JFrame {
private JPanel con;
private CardLayout cl;
private JPanel qq,tt;
private JButton aa,bb;
private JFrame zz;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
asdf frame = new asdf();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public asdf() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(null);
con = new JPanel();
con.setBorder(new EmptyBorder(50, 50, 50, 50));
setContentPane(con);
con.setLayout(null);
qq = new JPanel();
qq.setBounds(101, 60, 329, 290);
qq.setBackground(Color.BLACK);
con.add(qq);
tt = new JPanel();
FlowLayout flowLayout = (FlowLayout) tt.getLayout();
tt.setBounds(0, 60, 363, 290);
tt.setBackground(Color.RED);
con.add(tt);
cl = new CardLayout();
cl.preferredLayoutSize(con);
cl.addLayoutComponent(qq,"qq");
cl.addLayoutComponent(tt,"tt");
//cl.show(con, "qq");
aa = new JButton("");
aa.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//cl.next(con);
}
});
aa.setBounds(10,10,50,30);
con.add(aa);
}
}
新手求教啊。。
好心人帮我改下咯。。 展开
1个回答
2013-11-02
展开全部
不显示的,setVisible(false)、
要显示的setVisible(true)
要显示的setVisible(true)
追问
如果JPanel很多的话 控制是不是很麻烦。。有没有别的办法。。
追答
写成数组呀,一下循环好了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询