JPanel pane = new JPanel();
JScrollPane sPane = new JScrollPane(pane);//主要是JScrollPane来实现滚动
frame.getContentPane().add(pane);
再把你的其它组件添加到pane中就可以了。<------ 树欲静而风不止 ------>
JScrollPane sPane = new JScrollPane(pane);//主要是JScrollPane来实现滚动
frame.getContentPane().add(pane);
再把你的其它组件添加到pane中就可以了。<------ 树欲静而风不止 ------>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;public class lianxi extends JFrame implements ActionListener
{
JPanel pane;
JFrame fram;
JButton ButtonAdd,ButtonDell;
public int count1=0;
public int count2=0;
public lianxi()
{
fram=new JFrame("在线用户");
pane=new JPanel();
JScrollPane sPane = new JScrollPane(pane);//主要是JScrollPane来实现滚动
fram.getContentPane().add(pane);
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fram.setSize(50,550);
fram.show();
ButtonAdd=new JButton("添加用户");
pane.add(ButtonAdd);
ButtonAdd.addActionListener(this);
ButtonDell=new JButton("删除用户");
pane.add(ButtonDell);
ButtonDell.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==ButtonAdd)
{
new AddObject(pane,"lbl"+count1++,"Button"+count2++);
fram.show();
}
if(evt.getSource()==ButtonDell)
{
pane.remove(count2--);
fram.show();
}
}
class AddObject
{
public AddObject(JPanel panel,String lbl,String ButtonName)
{
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbCons=new GridBagConstraints();
gbCons.anchor=GridBagConstraints.CENTER;
JButton button;
JLabel label;
JPanel jp;
jp=new JPanel();
jp.setLayout(gbl);
gbCons.gridx=0;
gbCons.gridy=0;
label=new JLabel(lbl);
gbl.setConstraints(label,gbCons);
panel.add(label, gbCons);
gbCons.gridx=0;
gbCons.gridy=1;
button=new JButton(ButtonName);
gbl.setConstraints(button,gbCons);
panel.add(button, gbCons);
jp.add(label);
jp.add(button);
panel.add(jp);
}
}
public static void main(String args[])
{
lianxi obj=new lianxi();
}
}
JFrame frame;
JPanel pane;
JScrollPane s;
JTextArea t;
public Test() {
t = new JTextArea(20,50);
frame = new JFrame("JScrollPane");
pane = new JPanel();
s = new JScrollPane(pane);
frame.getContentPane().add(s);
pane.add(t);
frame.setSize(300, 200);
frame.setVisible(true);
} public static void main(String[] args) {
Test test = new Test();
}
}<------ 树欲静而风不止 ------>