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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends JFrame implements ActionListener {
public static final int WIDTH = 300;
public static final int L_HEIGHT = 100;
public static final int B_HEIGHT = 200;
JSplitPane jsp;
JPanel jp1;
JPanel jp2;
JTextField jtf;
JButton jb;
JTextPane jtp;
CardLayout cl;
boolean showTP = true;
public Test () {
super("Test");
jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
jsp.setDividerSize(5);
jtf = new JTextField(10);
jb = new JButton("Show TextPane");
jb.addActionListener(this);
jp1 = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.CENTER;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
jp1.add(jtf,c);
c.gridx = GridBagConstraints.RELATIVE;
c.gridwidth = GridBagConstraints.REMAINDER;
jp1.add(jb,c);
jp1.setSize(WIDTH,L_HEIGHT);
jsp.setTopComponent(jp1);
jtp = new JTextPane();
jtp.setSize(WIDTH,L_HEIGHT);
jsp.setBottomComponent(jtp);
jsp.setDividerLocation(L_HEIGHT);
cl = new CardLayout();
this.setSize(WIDTH,L_HEIGHT);
this.getContentPane().setLayout(cl);
this.getContentPane().add("HidePanel",jp1);
this.getContentPane().add("ShowPanel",jsp);
/*this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(jsp,BorderLayout.CENTER);*/
}
public void actionPerformed(ActionEvent e) {
if (showTP){
this.setSize(WIDTH,B_HEIGHT);
cl.next(this.getContentPane());
jb.setText("Hide TextPane");
} else {
this.setSize(WIDTH,L_HEIGHT);
cl.previous(this.getContentPane());
jb.setText("Show TextPane");
}
showTP = !showTP;
}
public static void main (String arg[]) {
Test t = new Test();
t.setVisible(true);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} |
Partager