62,621
社区成员
发帖
与我相关
我的任务
分享import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author Skjalg Bjorndal
*/
public class MigFrame extends JFrame {
final JButton button1 = new JButton("Hello World");
final JButton button2 = new JButton("Hello There");
final JButton button3 = new JButton("Hello Again");
final JButton button4 = new JButton("Hello 4 U");
public MigFrame(boolean hideButton4) {
setPreferredSize(new Dimension(600, 400));
initGUI(hideButton4);
}
private void initGUI(boolean hideButton4) {
JPanel panel = new JPanel(new MigLayout("fill, hidemode 3, nocache, debug"));
getContentPane().add(panel);
panel.add(createButtonPanel(), "dock south");
panel.add(button1, "dock west, growx");
panel.add(button2, "dock west");
JPanel panelClient = new JPanel(new MigLayout("flowy, fill"));
panelClient.add(button3, "dock north, growy");
if (!hideButton4) {
panelClient.add(button4, "growy, dock south");
setLocation(620, 200);
} else {
setLocation(10, 200);
}
panel.add(panelClient, "dock west, growx");
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
JPanel createButtonPanel() {
JPanel panel = new JPanel(new MigLayout());
JToggleButton hideButton = new JToggleButton("Hide");
hideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button1.setVisible(!button1.isVisible());
button4.setVisible(!button4.isVisible());
getContentPane().validate();
}
});
panel.add(hideButton);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final MigFrame fm = new MigFrame(true);
fm.setVisible(true);
final MigFrame fm2 = new MigFrame(false);
fm2.setVisible(true);
}
});
}
}http://www.open-open.com/open92161.htm
http://www.open-open.com/open92161.htm
看一下这个吧!
package test;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestDrawer extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public JButton btn1 = new JButton("TestBTN1");
public JButton btn2 = new JButton("TestBTN2");
public JButton btn3 = new JButton("TestBTN3");
public JLabel lab1 = new JLabel("TestLab1");
public JLabel lab2 = new JLabel("TestLab2");
public JLabel lab3 = new JLabel("TestLab3");
public JPanel panel = new JPanel();
public int i = 1;
public TestDrawer() {
init();
}
private void init() {
this.setTitle("TestDrawer");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(getCenterPanel(), BorderLayout.CENTER);
this.pack();
this.setVisible(true);
}
private JPanel getCenterPanel() {
panel.setLayout(new GridLayout(3, 1));
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
return panel;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btn1)) {
i++;
if (i % 2 == 0) {
panel.setLayout(new GridLayout(4, 1));
panel.removeAll();
panel.add(btn1);
panel.add(lab1);
panel.add(btn2);
panel.add(btn3);
} else {
panel.setLayout(new GridLayout(3, 1));
panel.removeAll();
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
}
panel.validate();
panel.repaint();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new TestDrawer();
}
}