java编程JPanel上加JPanel,为什么设了null布局之后什么都不显示
在下面这个类中,的监控事件里添加更新了panelpublicclassWelcomeextendsJFrameimplementsActionListener,Mouse...
在下面这个类中,的监控事件里添加更新了panel
public class Welcome extends JFrame implements ActionListener ,MouseListener{
JLabel label1,label2;
JButton sure;
JTextField unm;
JPasswordField pwd;
Connection conn;
JLabel viplog,vipzc,jc,tz;
Image cur = new ImageIcon("./img/首页/arrow.png").getImage();
morenpanel mpanel=new morenpanel();
viploginPanel vi=new viploginPanel();
final JPanel rPanel = new JPanel();
public Welcome()
{
init();
add();
setVisible(true);
//点关闭按钮时退出
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void add()
{
ImageIcon v = new ImageIcon("./img/首页/会员登录3.jpg");
viplog=new JLabel(v);
viplog.setBounds(50,120,170,70);
viplog.addMouseListener(this);
//viplog.setEnabled(false);
this.add(viplog);
//this.add(mpanel);
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getSource()==viplog)
{
viplog.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(cur, new Point(), null));
rPanel.removeAll();
rPanel.setLocation(250, 80);
//rPanel.setPreferredSize()
rPanel.add(new viploginPanel(),BorderLayout.CENTER);
//SwingUtilities.updateComponentTreeUI(rPanel);
rPanel.validate();
}
if(e.getSource()==viplog)
{
vipzc.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(cur, new Point(), null));
}
if(e.getSource()==jc)
{
jc.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(cur, new Point(), null));
}
if(e.getSource()==tz)
{
tz.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(cur, new Point(), null));
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
更新的panel源码:
public class viploginPanel extends JPanel{
viploginPanel()
{
init();
}
void init()
{
this.setLayout(null);//这句话一加这个界面就不显示了,求修改代码,因为我还想用null布局
JButton b=new JButton("123");
b.setBounds(50,50,200,200);
this.add(b);
this.add(new JButton("123456"));
//this.setVisible(true);
// this.setOpaque(true);
this.setBackground(Color.white);
this.setVisible(true);
}
} 展开
public class Welcome extends JFrame implements ActionListener ,MouseListener{
JLabel label1,label2;
JButton sure;
JTextField unm;
JPasswordField pwd;
Connection conn;
JLabel viplog,vipzc,jc,tz;
Image cur = new ImageIcon("./img/首页/arrow.png").getImage();
morenpanel mpanel=new morenpanel();
viploginPanel vi=new viploginPanel();
final JPanel rPanel = new JPanel();
public Welcome()
{
init();
add();
setVisible(true);
//点关闭按钮时退出
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void add()
{
ImageIcon v = new ImageIcon("./img/首页/会员登录3.jpg");
viplog=new JLabel(v);
viplog.setBounds(50,120,170,70);
viplog.addMouseListener(this);
//viplog.setEnabled(false);
this.add(viplog);
//this.add(mpanel);
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getSource()==viplog)
{
viplog.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(cur, new Point(), null));
rPanel.removeAll();
rPanel.setLocation(250, 80);
//rPanel.setPreferredSize()
rPanel.add(new viploginPanel(),BorderLayout.CENTER);
//SwingUtilities.updateComponentTreeUI(rPanel);
rPanel.validate();
}
if(e.getSource()==viplog)
{
vipzc.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(cur, new Point(), null));
}
if(e.getSource()==jc)
{
jc.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(cur, new Point(), null));
}
if(e.getSource()==tz)
{
tz.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(cur, new Point(), null));
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
更新的panel源码:
public class viploginPanel extends JPanel{
viploginPanel()
{
init();
}
void init()
{
this.setLayout(null);//这句话一加这个界面就不显示了,求修改代码,因为我还想用null布局
JButton b=new JButton("123");
b.setBounds(50,50,200,200);
this.add(b);
this.add(new JButton("123456"));
//this.setVisible(true);
// this.setOpaque(true);
this.setBackground(Color.white);
this.setVisible(true);
}
} 展开
4个回答
展开全部
JPanel默认属性 下 你是什么都看不到的。
null布局下控件都必须用绝对定位,不然就没有。
最常用的定位方法是setBounds(x, y, width, height);控件都有
null布局下控件都必须用绝对定位,不然就没有。
最常用的定位方法是setBounds(x, y, width, height);控件都有
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-12-31
展开全部
if(e.getSource()==tz){
先看到一点,,对象的比较,不要使用==
JLabel 更新一下
viplog.setBounds(50,120,170,70);
viplog.addMouseListener(this);
//viplog.setEnabled(false);
this.add(viplog);
//this.add(mpanel);
viplog.updateUI(); ////////////////////////////////////////////////这
先看到一点,,对象的比较,不要使用==
JLabel 更新一下
viplog.setBounds(50,120,170,70);
viplog.addMouseListener(this);
//viplog.setEnabled(false);
this.add(viplog);
//this.add(mpanel);
viplog.updateUI(); ////////////////////////////////////////////////这
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写了个简单例子没有发现问题,给你参考下:
public class Welcome extends JFrame {
static class viploginPanel extends JPanel {
viploginPanel() {
this.setLayout(null);// 没有发现问题
JButton b = new JButton("123");
b.setBounds(50, 50, 200, 200);
this.add(b);
b = new JButton("123456");
b.setBounds(250, 300, 100, 20);// 这个按钮也要显示设置bound
this.add(b);
this.setBackground(Color.white);
}
}
public static void main(String[] args) {
Welcome f = new Welcome();
f.add(new viploginPanel());
f.setSize(400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询