JAVA动态显示两个JPanel
我有三个JPanel,jpanelLogin,jpanelSetting,jpanelMain;,jpanelMain用来存放前两个,在jpanelLogin上有个“设置...
我有三个JPanel, jpanelLogin,jpanelSetting,jpanelMain;,jpanelMain用来存放前两个,在jpanelLogin上有个“设置”按钮,如下图,当我单击按钮时,我想把jpanelMain里的jpanelSetting设置为可见的,我该怎么弄呢?
我这样做的话,又会出现异常。
jbtSetting.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jpanelSetting.setVisible(true);
}
}); 展开
我这样做的话,又会出现异常。
jbtSetting.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jpanelSetting.setVisible(true);
}
}); 展开
2个回答
展开全部
应该是没什么问题的,注意你声明的jpanelSetting的位置。
具体参考这个:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class test1 extends JFrame{
JPanel jp1 = new JPanel();
JPanel jp2= new JPanel();
public void init(){
JPanel jp = new JPanel();
this.setLocation(300,300);
this.add(jp);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(-1);
}
});
JButton jb1 = new JButton("Ok1");
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
jp1.setVisible(false);
jp2.setVisible(true);
}
});
jp1.add(jb1);
JButton jb2 = new JButton("OK2");
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
jp2.setVisible(false);
jp1.setVisible(true);
}
});
jp2.add(jb2);
jp.add(jp1);
jp.add(jp2);
jp2.setVisible(false);
pack();
this.setVisible(true);
}
public static void main(String[] args) {
test1 t = new test1();
t.init();
}
}
具体参考这个:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class test1 extends JFrame{
JPanel jp1 = new JPanel();
JPanel jp2= new JPanel();
public void init(){
JPanel jp = new JPanel();
this.setLocation(300,300);
this.add(jp);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(-1);
}
});
JButton jb1 = new JButton("Ok1");
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
jp1.setVisible(false);
jp2.setVisible(true);
}
});
jp1.add(jb1);
JButton jb2 = new JButton("OK2");
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
jp2.setVisible(false);
jp1.setVisible(true);
}
});
jp2.add(jb2);
jp.add(jp1);
jp.add(jp2);
jp2.setVisible(false);
pack();
this.setVisible(true);
}
public static void main(String[] args) {
test1 t = new test1();
t.init();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询