周末偷闲,作了个模仿QQ界面效果的玩意。
太简单了点,不好意思拿出手,呵呵。
用jbuilder 7写的,用到了jbcl,不过加以改造的话,可以脱离jbcl。写得随意,也没加注释,大家凑和看吧。
**---- Application1.java ----**
package untitled4;
import javax.swing.UIManager;
import java.awt.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author Apocalypse
* @version 1.0
*/
public class Application1 {
private boolean packFrame = false;
//Construct the application
public Application1() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application1();
}
}
**---- Frame1.java ----**
package untitled4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author Apocalypse
* @version 1.0
*/
public class Frame1 extends JFrame implements ItemPanelContainer{
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel_content = new JPanel();
private VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void resetContentPanel(ItemPanel itemPanel) {
Component itemPanelSet[] = jPanel_content.getComponents();
for (int i = 0, n = itemPanelSet.length; i < n; i++) {
ItemPanel iPanel = (ItemPanel)itemPanelSet[i];
iPanel.expand(iPanel == itemPanel);
}
jPanel_content.revalidate();
}
private void initContent() {
for (int i = 0; i < 5; i++) {
ItemPanel itemPanel = new ItemPanel("Group " + Integer.toString(i), this);
for (int j = 0; j < 4; j++) {
itemPanel.addItem(new JLabel("group " + Integer.toString(i) + " item " + Integer.toString(j)));
}
jPanel_content.add(itemPanel, null);
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jPanel_content.setLayout(verticalFlowLayout1);
contentPane.add(jPanel_content, BorderLayout.CENTER);
initContent();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
**---- ItemPanel.java ----**
package untitled4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import com.borland.jbcl.layout.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author Apocalypse
* @version 1.0
*/
public class ItemPanel extends JPanel{
private BorderLayout borderLayout1 = new BorderLayout();
private JButton jButton_id = new JButton();
private String name = null;
private ItemPanelContainer container = null;
private JPanel jPanel_content = new JPanel();
private VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
public ItemPanel(String name, ItemPanelContainer container) {
try {
this.name = name;
this.container = container;
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void addItem(JComponent item) {
jPanel_content.add(item, null);
}
public void expand(boolean flag) {
if (flag)
this.add(jPanel_content, BorderLayout.CENTER);
else
this.remove(jPanel_content);
}
private void jbInit() throws Exception {
jButton_id.setText(name);
jButton_id.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton_id_actionPerformed(e);
}
});
this.setLayout(borderLayout1);
jPanel_content.setLayout(verticalFlowLayout1);
this.add(jButton_id, BorderLayout.NORTH);
}
void jButton_id_actionPerformed(ActionEvent e) {
container.resetContentPanel(this);
}
}
**---- ItemPanelContainer.java ----**
package untitled4;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author Apocalypse
* @version 1.0
*/
public interface ItemPanelContainer {
public void resetContentPanel(ItemPanel itemPanel);
}
自己想到的改进,ItemPanel里jPanel_content现在简单的用了VerticalFlowLayout,可以考虑改成JViewport,再加两个滚动按钮,以便在含有组件较多时实现滚动。