为了调节列表项的显示空间
关键代码如下
if(rdbtnNewRadioButton_2.isSelected())
{
list.setLayoutOrientation(JList.VERTICAL_WRAP);//设置排版方式
scrollPane.revalidate();//更新界面
}
完整代码如下
public class ListShowWay extends JFrame {
private JPanel contentPane;
private JList list;
private JRadioButton rdbtnNewRadioButton;
private JRadioButton rdbtnNewRadioButton_1;
private JRadioButton rdbtnNewRadioButton_2;
private JScrollPane scrollPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ListShowWay frame = new ListShowWay();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ListShowWay() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
do_windows_activity(e);
//super.windowOpened(e);
}
});
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
list = new JList();
list.setFont(new Font("Times New Roman",Font.PLAIN,20));
scrollPane.setViewportView(list);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
rdbtnNewRadioButton = new JRadioButton("HORIZONTAL_WRAP");
panel.add(rdbtnNewRadioButton);
rdbtnNewRadioButton_1= new JRadioButton("VERTICAL");
panel.add(rdbtnNewRadioButton_1);
rdbtnNewRadioButton_2 = new JRadioButton("VERTICAL_WRAP");
panel.add(rdbtnNewRadioButton_2);
ButtonGroup bg=new ButtonGroup();
bg.add(rdbtnNewRadioButton);
bg.add(rdbtnNewRadioButton_1);
bg.add(rdbtnNewRadioButton_2);
rdbtnNewRadioButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setRadioButtonListenerH();
}
});
rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setRadioButtonListenerV();
}
});
rdbtnNewRadioButton_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setRadioButtonListenerVW();
}
});
}
public void do_windows_activity(WindowEvent e)
{
String data[]=new String[12];
for(int i=0;i<12;i++)
{
data[i]="Item"+i;
}
list.setListData(data);
}
public void setRadioButtonListenerH()
{
if(rdbtnNewRadioButton.isSelected())
{
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);//设置排版方式
scrollPane.revalidate();//更新界面
}
}
public void setRadioButtonListenerV()
{
if(rdbtnNewRadioButton_1.isSelected())
{
//JList 的默认布局
list.setLayoutOrientation(JList.VERTICAL);//设置排版方式
scrollPane.revalidate();//更新界面
}
}
public void setRadioButtonListenerVW()
{
if(rdbtnNewRadioButton_2.isSelected())
{
list.setLayoutOrientation(JList.VERTICAL_WRAP);//设置排版方式
scrollPane.revalidate();//更新界面
}
}
}