package demo;import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class dynamicTest extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel contentPanel = (JPanel) this.getContentPane();
JButton jb = new JButton("添加");
JPanel jp1 = new JPanel(new FlowLayout());
JPanel jp2 = new JPanel(new FlowLayout()); public dynamicTest() {
super("动态生成组件");
this.setSize(400, 300);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPanel.setLayout(new BorderLayout());
contentPanel.add("North", jp1);
contentPanel.add("Center", jp2);
jp1.add(jb);
jb.addActionListener(this);
this.setVisible(true);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb) {
JButton jb2 = new JButton("按钮" + (jp2.getComponentCount() + 1));
jb2.addActionListener(this);
jp2.add(jb2);
this.validate();
} else {
jp2.remove((JButton) e.getSource());
this.validate();
this.repaint();
}
} public static void main(String args[]) {
new dynamicTest();
}
}这段代码可以动态生成一系列Button,但是Button所在的面板是固定不变的,我想实现在没有添加Button的时候,面板就只恰好够容纳一个“添加”按钮,而在我动态添加了Button后,面板能自动扩大,在删除Button后,面板自动缩小,以适当的大小展现出来,请问各位高手应该怎么做,感激不尽。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class dynamicTest extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel contentPanel = (JPanel) this.getContentPane();
JButton jb = new JButton("添加");
JPanel jp1 = new JPanel(new FlowLayout());
JPanel jp2 = new JPanel(new FlowLayout()); public dynamicTest() {
super("动态生成组件");
this.setSize(400, 300);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPanel.setLayout(new BorderLayout());
contentPanel.add("North", jp1);
contentPanel.add("Center", jp2);
jp1.add(jb);
jb.addActionListener(this);
this.setVisible(true);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb) {
JButton jb2 = new JButton("按钮" + (jp2.getComponentCount() + 1));
jb2.addActionListener(this);
jp2.add(jb2);
this.validate();
} else {
jp2.remove((JButton) e.getSource());
this.validate();
this.repaint();
}
} public static void main(String args[]) {
new dynamicTest();
}
}这段代码可以动态生成一系列Button,但是Button所在的面板是固定不变的,我想实现在没有添加Button的时候,面板就只恰好够容纳一个“添加”按钮,而在我动态添加了Button后,面板能自动扩大,在删除Button后,面板自动缩小,以适当的大小展现出来,请问各位高手应该怎么做,感激不尽。
/**
* 我的动态面板
*/
private static final long serialVersionUID = 4134594001618222599L;
private static Vector<Component> button = new Vector<Component>();
static {
JButton b1 = new JButton("我的好友"), b2 = new JButton("我的通讯录"), b3 = new JButton(
"我的群组");
JList list1, list2, list3;
list1 = new JList(new String[] { "one", "two" });
list2 = new JList(new String[] { "one", "two" });
list3 = new JList(new String[] { "one", "two" });
list1.setVisible(true);
list2.setVisible(false);
list3.setVisible(false);
button.add(b1);
button.add(list1);
button.add(b2);
button.add(list2);
button.add(b3);
button.add(list3);
} public MyPane() {
for (int i = 0; i < button.size(); i++) {
if (i % 2 == 0) {
button.get(i).addMouseListener(this);
}
}
setLayout(new GridBagLayout());
PaintButton();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(180, 350);
setVisible(true);
} public void PaintButton() {
GridBagConstraints g = new GridBagConstraints();
g.fill = GridBagConstraints.BOTH;
g.gridx = 1;
g.weightx = 1;
int line = 1;
for (int i = 0; i < button.size(); i++) {
if (i % 2 == 0) {
g.weighty = 0.005;
g.gridy = line;
this.add(button.get(i), g);
line++;
}
else {
g.weighty = 1;
g.gridy = line;
this.add(button.get(i), g);
line++;
}
}
} public static void main(String[] args) {
new MyPane();
} public void mouseClicked(MouseEvent e) {
for (int i = 0; i < button.size(); i++) {
if (i % 2 != 0) {
button.get(i).setVisible(false);
}
}
button.get(button.indexOf(e.getSource()) + 1).setVisible(true);
PaintButton();
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
}
}