JTabbedPane里添加JPanel后,不能控制JPanel的大小,而且布局似乎也有问题。
恩,如果你在JPanel上加上组件,大小就好控制了,比如你加个button或者是label什么的。。我以前写过一个例子。是QQ界面。你可以参考一下。
[CODE]/**
*
*/
package cn.edu.xmu.chatSystem.client.swing;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.ScrollPane;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.Box;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import cn.edu.xmu.chatSystem.client.ClientUserManager;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.io.DataInputStream;
import java.io.IOException;
/**
* @author 狂放不羁
*
*/
public class ClientChatWindow extends JFrame {
//private ClientUserManager userManager;
private int QQnumber;
private JTabbedPane panelNorth;
private JPanel panelCenter;
private JTextPane textDisplay;
private Box boxSouth;
private JTextPane textInput;
private ToolsPane toolsPane;
private JPanel panelEast;
private StyledDocument doc;
//private DataInputStream dis;
public ClientChatWindow(){
super("聊天窗口");
try { // 使用Windows的界面风格
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
setLayout(new BorderLayout());
panelNorth = new TabbedPanel();
panelNorth.setBackground(new Color(225,254,255));
panelCenter = new JPanel();
panelCenter.setLayout(new BorderLayout());
JPanel panelMsg = new JPanel();
panelMsg.setPreferredSize(new Dimension(350,25));
panelCenter.setBackground(Color.red);
panelCenter.add(panelMsg,BorderLayout.NORTH);
textDisplay = new JTextPane();
doc = textDisplay.getStyledDocument();
JScrollPane displayMsg = new JScrollPane(textDisplay);
//displayMsg.add(new JScrollBar(JScrollBar.VERTICAL),BorderLayout.EAST);
displayMsg.setMinimumSize(new Dimension(panelCenter.getWidth(),panelCenter.getHeight()-30));
textDisplay.setBackground(Color.white);
textDisplay.setEnabled(false);
panelCenter.add(displayMsg,BorderLayout.CENTER);
boxSouth = new Box(BoxLayout.Y_AXIS);
//boxSouth.setPreferredSize(new Dimension(10,150));
//boxSouth.setMaximumSize(new Dimension(10,180));
toolsPane = new ToolsPane();
boxSouth.add(toolsPane);
textInput = new JTextPane();
//textInput.addActionListener(new ActionListener())
JScrollPane textScroll= new JScrollPane(textInput);
boxSouth.add(textScroll);
boxSouth.setBackground(Color.green);
textScroll.setPreferredSize(new Dimension(10,80));
boxSouth.add(new ToolsbarDown());
panelCenter.add(boxSouth,BorderLayout.SOUTH);
panelEast = new JPanel();
panelEast.setPreferredSize(new Dimension(150,400));
panelEast.setBackground(Color.white);
panelEast. setBorder(BorderFactory.createEtchedBorder());
//getContentPane().setBackground(new Color(225,254,255));
getContentPane().add(panelNorth,BorderLayout.NORTH);
getContentPane().add(panelCenter,BorderLayout.CENTER);
getContentPane().add(panelEast,BorderLayout.EAST);
}
public void launchWindow(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
setMinimumSize(new Dimension(400,350));
setLocation(450,450);
setVisible(true);
}
private class ToolsPane extends JPanel{
private ToolsBarUp downBar = new ToolsBarUp();
private FontBar upBar= new FontBar();
ToolsPane(){
setLayout(new BorderLayout());
setBackground(Color.green);
add(upBar,BorderLayout.CENTER);
add(downBar,BorderLayout.SOUTH);
}
public String getFontName(){
return upBar.getFontName();
}
public int getFontSize(){
return upBar.getFontSize();
}
public String getStyleName(){
return upBar.getStyleName();
}
/* public Color getFontColor(){
return upBar.getFontColor();
}*/
private class ToolsBarUp extends JToolBar{
private JButton buttonChar = new JButton();
private JButton buttonFace = new JButton();
private JButton buttonSend = new JButton();
ToolsBarUp(){
setOrientation( HORIZONTAL );
setFloatable(false);
setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
//setBackground(new Color(225,254,255));
setPreferredSize(new Dimension(350,25));
buttonChar.setIcon(new ImageIcon("image\\chatWindow\\toolsbarUp\\IMSmallToolbarFont.png" ));
buttonFace.setIcon(new ImageIcon("image\\chatWindow\\toolsbarUp\\face.jpg"));
buttonSend.setIcon(new ImageIcon("image\\chatWindow\\toolsbarUp\\IMBigToolbarSendFile.jpg" ));
add(buttonChar);
add(buttonFace);
add(buttonSend);
add(Box.createHorizontalGlue());
buttonChar.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==1){
upBar.setVisible(true);
}else{
upBar.setVisible(false);
}
}
});
}
}
private class FontBar extends JToolBar{
private String [] fontNames= {"宋体","黑体","楷体","Dialog", "Gulim" };
private String [] fontStyle ={"常规", "斜体", "粗体", "粗斜体" };
private String [] fontSize ={"12","13","14","15","18","20","22" };
private JComboBox nameComboBox ;
private JComboBox sizeComboBox;
private JComboBox styleComboBox;
private JButton fontColor ;
FontBar(){
nameComboBox = new JComboBox(fontNames);
sizeComboBox = new JComboBox(fontSize);
styleComboBox= new JComboBox(fontStyle);
fontColor = new JButton(new ImageIcon(""));
//nameComboBox.setSize(0,0);
//sizeComboBox.setSize(0,0);
nameComboBox.setPreferredSize(new Dimension(65,20));
sizeComboBox.setPreferredSize(new Dimension(30,20));
styleComboBox.setPreferredSize(new Dimension(45,20));
/*nameComboBox.setMaximumSize(new Dimension(50,20));
sizeComboBox.setMaximumSize(new Dimension(20,20));
styleComboBox.setMaximumSize(new Dimension(50,20));*/
setFloatable(false);
setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
//setPreferredSize(new Dimension(0,0));
//setBackground(Color.white);
add(nameComboBox);
add(sizeComboBox);
add(styleComboBox);
add(fontColor);
add(Box.createHorizontalGlue());
nameComboBox.setMaximumRowCount(5);
sizeComboBox.setMaximumRowCount(5);
styleComboBox.setMaximumRowCount(5);
setVisible(false);
}
public String getFontName(){
return (String) nameComboBox.getSelectedItem();
}
public int getFontSize(){
return Integer.parseInt((String) sizeComboBox.getSelectedItem());
}
public String getStyleName(){
return (String) styleComboBox.getSelectedItem();
}
}
}
private class ToolsbarDown extends JPanel{
private JButton chatRecord = new JButton("聊天记录");
private JButton chatModel = new JButton("聊天模式");
private JButton shut = new JButton("关闭");
private JButton send = new JButton("发送");
private Insets insets = new Insets(2,5,2,5);
ToolsbarDown(){
setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
//setBackground(new Color(16,200,239));
//setFloatable(false);
setBorder(BorderFactory.createRaisedBevelBorder());
//chatRecord.setIcon(new ImageIcon("image\\chatWindow\\toolsbarDown\\chatRecord.jpg"));
//chatModel.setIcon(new ImageIcon("image\\chatWindow\\toolsbarDown\\chatModel.jpg"));
//shut.setIcon(new ImageIcon("image\\chatWindow\\toolsbarDown\\shut.jpg"));
//send.setIcon(new ImageIcon("image\\chatWindow\\toolsbarDown\\send.jpg"));
setPreferredSize(new Dimension(60,30));
//chatRecord.setFont(new Font(Font.DIALOG,Font.PLAIN,12 ));
chatRecord.setMargin(insets);
chatModel.setMargin(insets);
shut.setMargin(insets);
shut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
ClientChatWindow.this.dispose();
}
});
send.setMargin(insets);
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(textInput.getText().matches("[ ]*")){
JOptionPane.showMessageDialog(textInput,"请不要发送空消息");
}else{
display();
//userManager.sendMessager(QQnumber+textInput.getText());
textInput.setText("");
}
}
});
add(chatRecord);
add(chatModel);
add(Box.createHorizontalGlue());
add(shut);
add(send);
}
}
//你主要看这里就可以了。
private class TabbedPanel extends JTabbedPane {
private JToolBar chatBar;
private JToolBar pastimeBar;
private JToolBar applyBar;
private JToolBar aboutBar;
TabbedPanel(){
chatBar = new JToolBar();
chatBar.setLayout(new BoxLayout(chatBar,BoxLayout.X_AXIS));
//chatBar.setBackground(new Color(16,200,239));
chatBar.setFloatable(false);
JButton message = new JButton("短信");
JButton video = new JButton("视频");
JButton voice = new JButton("语音");
JButton sendFile = new JButton("传文件");
message.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\IMBigToolbarMQQ.png" ));
video.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\IMBigToolbarVideo.PNG" ));
voice.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\phone.jpg" ));
chatBar.add(message);
chatBar.add(video);
chatBar.add(voice);
chatBar.add(sendFile);
chatBar.add(Box.createHorizontalGlue());
addTab("聊天",null,chatBar,null);
pastimeBar = new JToolBar();
pastimeBar.setLayout(new BoxLayout(pastimeBar,BoxLayout.X_AXIS));
pastimeBar.setFloatable(false);
//pastimeBar.setBackground(new Color(16,200,239));
JButton movie = new JButton("看电影");
JButton game = new JButton("游戏");
JButton televison = new JButton("网络电视");
JButton song = new JButton("点歌");
song.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\phone.jpg" ));
game.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\phone.jpg" ));
movie.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\phone.jpg" ));
televison.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\phone.jpg"));
pastimeBar.add(song);
pastimeBar.add(game);
pastimeBar.add(movie);
pastimeBar.add(televison);
pastimeBar.add(Box.createHorizontalGlue());
addTab("娱乐",null,pastimeBar,null);
applyBar = new JToolBar();
applyBar.setLayout(new BoxLayout(applyBar,BoxLayout.X_AXIS));
//applyBar.setBackground(new Color(16,200,239));
applyBar.setFloatable(false);
JButton share = new JButton("浏览共享");
JButton help = new JButton("远程协助");
share.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\phone.jpg" ));
help.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\phone.jpg" ));
applyBar.add(share);
applyBar.add(help);
applyBar.add(Box.createHorizontalGlue());
addTab("应用",null,applyBar,null);
aboutBar = new JToolBar();
aboutBar.setLayout(new BoxLayout(aboutBar,BoxLayout.X_AXIS));
//aboutBar.setBackground(new Color(16,200,239));
aboutBar.setFloatable(false);
JButton about = new JButton("关于");
about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
new AboutDialog(ClientChatWindow.this,"关于本软件",true);
}
});
JButton contact = new JButton("联系作者");
contact.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
new AboutAuthor(ClientChatWindow.this,"作者",true);
}
});
about.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\phone.jpg" ));
contact.setIcon(new ImageIcon("image\\chatWindow\\tabbedPanel\\phone.jpg" ));
aboutBar.add(about);
aboutBar.add(contact);
aboutBar.add(Box.createHorizontalGlue());
addTab("关于",null,aboutBar,null);
}
private class AboutDialog extends JDialog {
private JLabel label;
private JButton button;
AboutDialog(JFrame frame,String title,boolean bool){
super(frame,title, bool);
label = new JLabel("<html><body>本程序用JAVA语言编写。哈哈。<br>请兄弟们帮偶发现BUG..<html><body>");
button = new JButton("确定^_^");
JPanel labelPanel = new JPanel();
labelPanel.add(label);
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
dispose();
}
});
getRootPane().setDefaultButton(button);
getContentPane().add(labelPanel,BorderLayout.CENTER);
getContentPane().add(buttonPanel,BorderLayout.SOUTH);
setSize(200,150);
setLocation(frame.getX()+50,frame.getY()+50);
setResizable(false);
setVisible(true);
}
}
private class AboutAuthor extends JDialog{
private JLabel label;
private JButton button;
AboutAuthor(JFrame frame,String title,boolean bool){
super(frame,title,bool);
label = new JLabel(new ImageIcon("image\\chatWindow\\tabbedPanel\\author.gif"));
label.setText("<html><body>网名:狂放不羁o(∩_∩)o<br>QQ:474590646^_^<br> MSN:yuquan_hg@hotmail.com<html><body>");
button = new JButton("点我");
JPanel labelPanel = new JPanel();
labelPanel.add(label);
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
dispose();
}
});
getContentPane().add(labelPanel,BorderLayout.CENTER);
getContentPane().add(buttonPanel,BorderLayout.SOUTH);
setSize(300,250);
setLocation(frame.getX()+50,frame.getY()+50);
setResizable(false);
setVisible(true);
}
}
}
private ChatWindowFont getChatWindowFont(){
ChatWindowFont font = new ChatWindowFont();
System.out.println(textInput.getText());
font.setContent(textInput.getText());
font.setName(toolsPane.getFontName());
System.out.println(toolsPane.getFontName()+toolsPane.getFontSize());
font.setSize(toolsPane.getFontSize());
String temp_style = (String) toolsPane.getStyleName();
if (temp_style.equals("常规")) {
font.setStyle(ChatWindowFont.GENERAL);
} else if (temp_style.equals("粗体")) {
font.setStyle(ChatWindowFont.BOLD);
} else if (temp_style.equals("斜体")) {
font.setStyle(ChatWindowFont.ITALIC);
} else if (temp_style.equals("粗斜体")) {
font.setStyle(ChatWindowFont.BOLD_ITALIC);
}
return font ;
}
void display(){
try {
ChatWindowFont font = getChatWindowFont();
System.out.println(font.getContent());
System.out.println(font.getAttrSet());
doc.insertString(doc.getLength(), font.getContent()+"\n", font.getAttrSet());
} catch (BadLocationException e) {
e.printStackTrace();
}
}
//private void MessageReceiver
public int getUserID(){
return QQnumber;
}
/**
* @param args
*/
public static void main(String[] args) {
new ClientChatWindow().launchWindow();
}
}
[/CODE]
[此贴子已经被作者于2007-8-21 11:45:52编辑过]