问题描述
- java 为什么点击首页按钮在旁边的面板上面不显示东西
-
import java.awt.*;
import javax.swing.*;
import static java.awt.BorderLayout.*;
import javax.swing.ImageIcon.*;
import java.awt.Color;
import static java.awt.BorderLayout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SwingDemo6
{
public void empty()
{
JFrame jf=new JFrame();
JButton jb=new JButton("首页");
JButton jb1=new JButton("搜索");
JButton jb2=new JButton("提问");
JButton jb3=new JButton("我的");
JButton jb4=new JButton("圈子");
jb.setPreferredSize(new Dimension(95,30));
jb.setBackground(Color.white);
jb.setFont(new Font("楷体",1,15));
jb1.setPreferredSize(new Dimension(95,30));
jb1.setBackground(Color.white);
jb1.setFont(new Font("楷体",1,15));
jb2.setPreferredSize(new Dimension(95,30));
jb2.setBackground(Color.white);
jb2.setFont(new Font("楷体",1,15));
jb3.setPreferredSize(new Dimension(95,30));
jb3.setBackground(Color.white);
jb3.setFont(new Font("楷体",1,15));
jb4.setPreferredSize(new Dimension(95,30));
jb4.setBackground(Color.white);
jb4.setFont(new Font("楷体",1,15));
final JPanel jp=new JPanel();
final JPanel jp1=new JPanel();
final JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
final JButton jb5=new JButton("测试");
jp.add(jb);
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
jp.setPreferredSize(new Dimension(100, 500));
jp1.setPreferredSize(new Dimension(400, 500));
jp2.setPreferredSize(new Dimension(400, 500));
jf.setLayout(new BorderLayout());
jf.add(jp,WEST);
jf.add(jp1,CENTER);
jf.add(jp2,EAST);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{jp1.setBackground(Color.white); jp1.add(jb5); } }); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.pack(); jf.setVisible(true); } public static void main(String[] args) { new SwingDemo6().empty(); }
}
解决方案
试下让frame validate()
解决方案二:
打个断点看看,,哪里的问题,,
解决方案三:
import java.awt.*;
import javax.swing.*;
import static java.awt.BorderLayout.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SwingDemo6 {
public void empty() {
JFrame jf = new JFrame();
JButton jb = new JButton("首页");
JButton jb1 = new JButton("搜索");
JButton jb2 = new JButton("提问");
JButton jb3 = new JButton("我的");
JButton jb4 = new JButton("圈子");
jb.setPreferredSize(new Dimension(95, 30));
jb.setBackground(Color.white);
jb.setFont(new Font("楷体", 1, 15));
jb1.setPreferredSize(new Dimension(95, 30));
jb1.setBackground(Color.white);
jb1.setFont(new Font("楷体", 1, 15));
jb2.setPreferredSize(new Dimension(95, 30));
jb2.setBackground(Color.white);
jb2.setFont(new Font("楷体", 1, 15));
jb3.setPreferredSize(new Dimension(95, 30));
jb3.setBackground(Color.white);
jb3.setFont(new Font("楷体", 1, 15));
jb4.setPreferredSize(new Dimension(95, 30));
jb4.setBackground(Color.white);
jb4.setFont(new Font("楷体", 1, 15));
final JPanel jp = new JPanel();
final JPanel jp1 = new JPanel();
final JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
final JButton jb5 = new JButton("测试");
jp.add(jb);
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
jp.setPreferredSize(new Dimension(100, 500));
jp1.setPreferredSize(new Dimension(400, 500));
jp2.setPreferredSize(new Dimension(400, 500));
jf.setLayout(new BorderLayout());
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jp1.setBackground(Color.white);
jb5.setVisible(true);
}
});
jb5.setVisible(false);
jp1.add(jb5);
jf.add(jp, WEST);
jf.add(jp1, CENTER);
jf.add(jp2, EAST);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
new SwingDemo6().empty();
}
}
你理解的原理有问题,JButton点击事件只会执行对应的actionPerformed方法里面的代码,不会再执行下面的添加方法了,也就是JFrame不会再改变了,也就是在第一次执行代码的时候你要把所有需要显示的组件都添加进去,但你不希望它显示的也可以先设置隐藏,之后通过点击事件可以设置显示,可以修改样式,可以获取到值,但不能修改结构(修改结构需要通过重启方法,设置是否显示实现)。