我有以下代码。
类似的代码有效。朋友也说它应该工作。但它没有。
任何人都有线索?
谢谢!
而不是绘制所需的布局,一切都很小......(
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class ChatGUI extends JPanel {
private static final long serialVersionUID = -3069354983197916315L;
private Image imgBackground;
private GridBagLayout gblChat;
private GridBagConstraints constraintsChat;
private JPanel jPanelChat;
private JList clientList;
private JColorChooser font_chooser;
private JButton privateMessage_button;
private JButton sendMessage_button;
private JScrollPane OutputOfChatMessages;
private JScrollPane ShowConnectedClients;
private JScrollPane InputOfChatMessage;
public static void main(String[] args) {
JFrame jf = new JFrame();
ChatGUI cg1 = new ChatGUI(500, 500);
jf.add(cg1);
jf.setExtendedState(Frame.MAXIMIZED_BOTH);
jf.setVisible(true);
}
/**
* Layout
*/
public ChatGUI(int width, int height) {
// used for scaling
this.setMinimumSize(new Dimension(width / 2,
height / 4));
this.setPreferredSize(new Dimension(width / 2,
height / 4));
jPanelChat = new JPanel();
gblChat = new GridBagLayout();
constraintsChat = new GridBagConstraints();
this.setLayout(gblChat);
this.setBackground(Color.LIGHT_GRAY);
constraintsChat.fill = GridBagConstraints.BOTH;
/**
* Panel 1 = OutputOfChatMessages
*/
OutputOfChatMessages = new JScrollPane();
constraintsChat.gridx = 0;
constraintsChat.gridy = 0;
constraintsChat.gridwidth = 1;
constraintsChat.gridheight = 1;
gblChat.setConstraints(OutputOfChatMessages, constraintsChat);
this.add(OutputOfChatMessages);
/**
* Panel 2 = ShowConnectedClients
*/
ShowConnectedClients = new JScrollPane();
constraintsChat.gridx = 1;
constraintsChat.gridy = 0;
constraintsChat.gridwidth = 4;
constraintsChat.gridheight = 4;
gblChat.setConstraints(ShowConnectedClients, constraintsChat);
this.add(ShowConnectedClients);
/**
* Panel 3 = InputOfChatMessage
*/
InputOfChatMessage = new JScrollPane();
constraintsChat.gridx = 0;
constraintsChat.gridy = 5;
constraintsChat.gridwidth = 2;
constraintsChat.gridheight = 2;
gblChat.setConstraints(InputOfChatMessage, constraintsChat);
this.add(ShowConnectedClients);
/**
* Panel 4 = privateMessage_button
*/
privateMessage_button = new JButton();
constraintsChat.gridx = 1;
constraintsChat.gridy = 6;
constraintsChat.gridwidth = 1;
constraintsChat.gridheight = 1;
gblChat.setConstraints(privateMessage_button, constraintsChat);
this.add(privateMessage_button);
/**
* Panel 5 = sendMessage_button
*/
sendMessage_button = new JButton();
constraintsChat.gridx = 1;
constraintsChat.gridy = 7;
constraintsChat.gridwidth = 1;
constraintsChat.gridheight = 1;
gblChat.setConstraints(sendMessage_button, constraintsChat);
this.add(sendMessage_button);
this.validate();
//this.revalidate();
}
}
答案 0 :(得分:2)
setXxxSize
,您的ChatGUI
应该没有可能需要占用的可见尺寸的概念。