Java 动态切换网格布局
嘿,伙计们。当我按下按钮时,我如何切换或更改GridBagLayout的所有按钮?它似乎卡住了。它不能代替旧的……请帮帮我。。。我尝试了重新绘制(),重新验证(),但“叹气”它就是不起作用。请纠正我Java 动态切换网格布局,java,swing,runtime,gridbaglayout,Java,Swing,Runtime,Gridbaglayout,嘿,伙计们。当我按下按钮时,我如何切换或更改GridBagLayout的所有按钮?它似乎卡住了。它不能代替旧的……请帮帮我。。。我尝试了重新绘制(),重新验证(),但“叹气”它就是不起作用。请纠正我 package gridlayoutjumper; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; impor
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();
}
}
首先尝试从容器中删除旧组件:
public void newLayout1() {
timeTablePnl.remove(jb1);
timeTablePnl.remove(jb2);
timeTablePnl.remove(jb3);
.
.
.
}
我认为GridBagLayout只会忽略在同一网格坐标处插入的组件。我可以再问一个问题吗?如果我动态创建jLabel而不是jButton呢。也就是说,我循环创建jLabel并将其插入gridbaglayout,而不具体命名每个jLabel。所有jLabel在gridbaglayout的不同位置具有相同的名称。顺便说一下,我需要这个来制作时间表。请帮忙,泰!