保存布局管理器
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LayoutTest extends JFrame {
public LayoutTest() {
initComponents();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton = new JButton();
jPanel1.setBackground(new java.awt.Color(204, 204, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LayoutManager tempGroupLayout = jPanel1.getLayout();
jPanel1.setLayout(layoutManager2);
layoutManager = layoutManager2;
layoutManager2 = tempGroupLayout;
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().
addContainerGap(286, Short.MAX_VALUE).
addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
layoutManager = jPanel1Layout;
javax.swing.GroupLayout jPanel1Layout2 = new javax.swing.GroupLayout(jPanel1);
jPanel1Layout2.setHorizontalGroup(
jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout2.createSequentialGroup().
addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
jPanel1Layout2.setVerticalGroup(
jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
layoutManager2 = jPanel1Layout2;
getContentPane().add(jPanel1);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LayoutTest().setVisible(true);
}
});
}
private JButton jButton;
private JPanel jPanel1;
private LayoutManager layoutManager;
private LayoutManager layoutManager2;
}
这段代码不起作用,为什么? 但是如果我每次在actionListener中创建一个布局,它就会起作用。
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LayoutTest extends JFrame {
public LayoutTest() {
initComponents();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton = new JButton();
jPanel1.setBackground(new java.awt.Color(204, 204, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LayoutManager tempGroupLayout = jPanel1.getLayout();
jPanel1.setLayout(layoutManager2);
layoutManager = layoutManager2;
layoutManager2 = tempGroupLayout;
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().
addContainerGap(286, Short.MAX_VALUE).
addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
layoutManager = jPanel1Layout;
javax.swing.GroupLayout jPanel1Layout2 = new javax.swing.GroupLayout(jPanel1);
jPanel1Layout2.setHorizontalGroup(
jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout2.createSequentialGroup().
addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
jPanel1Layout2.setVerticalGroup(
jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
layoutManager2 = jPanel1Layout2;
getContentPane().add(jPanel1);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LayoutTest().setVisible(true);
}
});
}
private JButton jButton;
private JPanel jPanel1;
private LayoutManager layoutManager;
private LayoutManager layoutManager2;
}
This code does not work, why?
But if I each time create a layout in actionListener, it will work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需在切换布局管理器后调用
validate()
即可。注意:在发布的代码中缺少一些行(例如jButton 的初始化)。
You just have to include a call to
validate()
after switching the layoutmanager.Note: In the code posted there are missing some lines (e.g. initialization of jButton).