1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import java.awt.BorderLayout;
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;
@SuppressWarnings("serial")
public class Test extends JFrame {
private JPanel topPanel = new JPanel();
private JButton btn = new JButton("toogle");
private JPanel bottomPanel = new JPanel();
private boolean visible = false;
public static void main(String[] args) {
new Test();
}
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createLayout();
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void createLayout() {
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
visible = !visible;
bottomPanel.setVisible(visible);
pack();
}
});
topPanel.add(new JLabel("label 1 ..."));
bottomPanel.add(new JLabel("label 2 ..."));
bottomPanel.setVisible(visible);
add(topPanel, BorderLayout.NORTH);
add(btn, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
}
} |
Partager