首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态切换GridbagLayout

动态切换GridbagLayout
EN

Stack Overflow用户
提问于 2013-07-04 22:23:27
回答 1查看 99关注 0票数 0

嘿,guys.How,当我按下一个按钮的时候,我是不是要切换或者改变GridBagLayout的所有JButton?它似乎卡住了。它不会取代旧的one.....Please帮助我。我尝试了repaint(),revalidate(),但是“叹息”它就是不能工作。请纠正我。

代码语言:javascript
复制
package gridlayoutjumper;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class GridLayoutJumper extends JFrame implements ActionListener{
    JFrame timeTableFrame = new JFrame("Helo");  
    JPanel timeTablePnl = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    JButton jb1 = new JButton("Jbutton1");
    JButton jb4 = new JButton("Change");
    JButton jb3 = new JButton("Jbutton3");
    JButton jb2 = new JButton("Jbutton2");

    public GridLayoutJumper(){

    }

    public void newLayout1(){

        System.out.println("newLayout1 executed");
        JButton jb5 = new JButton("Jbutton5");
        JButton jb6 = new JButton("Jbutton6");
        JButton jb7 = new JButton("Jbutton7");

        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        c.insets = new Insets(0, 15, 0, 15); 
        timeTablePnl.add(jb5, c);

        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        c.insets = new Insets(0, 15, 0, 15);  
        timeTablePnl.add(jb6, c);

        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.insets = new Insets(0, 15, 0, 15);  
        timeTablePnl.add(jb7, c);

        timeTablePnl.validate();
        timeTablePnl.repaint();
    }

    public void createComponent(){
        timeTableFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        timeTableFrame.setPreferredSize(new Dimension(500, 500));
        timeTableFrame.setMinimumSize(new Dimension(500, 500));
        timeTableFrame.add(timeTablePnl);

        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        c.insets = new Insets(0, 15, 0, 15);
        timeTablePnl.add(jb1, c);

        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        c.insets = new Insets(0, 15, 0, 15); 
        timeTablePnl.add(jb2, c);

        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.insets = new Insets(0, 15, 0, 15);  
        timeTablePnl.add(jb3, c);

        c.gridx = 1;
        c.gridy = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.insets = new Insets(0, 15, 0, 15);  
        timeTablePnl.add(jb4, c);

        jb4.addActionListener(this); 
        timeTableFrame.setVisible(true);
    }


    @Override
    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        if(source == jb4){
            System.out.println("Gotcha");
            newLayout1();
        }
    }

    public static void main(String[] args) {
        GridLayoutJumper x = new GridLayoutJumper();
        x.createComponent();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-04 22:35:34

尝试先从容器中删除旧组件:

代码语言:javascript
复制
public void newLayout1() {

    timeTablePnl.remove(jb1);
    timeTablePnl.remove(jb2);
    timeTablePnl.remove(jb3);

    .
    .
    .
}

我认为GridBagLayout只是忽略了在相同网格坐标下插入的组件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17472720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档