2016-11-25 108 views
0

这是它的外观,当我开始我的窗口:如何设置一个SWING窗口的尺寸在JAVA

https://i.stack.imgur.com/LwakG.jpg

而这正是我想要的:

https://i.stack.imgur.com/TykkA.jpg

这是我用过的代码:

Main:

public void main() 
{ 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      createAndShowGui(); 
     } 
    }); 

} 

创建和显示GUI

public static void createAndShowGui() 
{ 
    Frame chatPanel = new Frame(); 
    JFrame frame = new JFrame("Chat"); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().add(chatPanel); 
    //Ensure the frame is the minimum size it needs to display properly 
    frame.pack(); 
    frame.setLocationByPlatform(true); 
    frame.setVisible(true); 
} 

框架

public class Frame extends JPanel implements ActionListener 
{ 

public static JTextArea textArea_send = new JTextArea(5, 14); 
public static JTextArea textArea_receive = new JTextArea(15, 20); 

private JButton send_button = new JButton("Send"); 

private JLabel receiver = new JLabel("Salon"); 


public Frame() 
{ 
    //Set the frame icon to an image loaded from a file. 
    //this.setIconImage(new ImageIcon(url).getImage()); 

    JPanel AreaGrid = new JPanel(); 
    AreaGrid.setLayout(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 

    gbc.gridx = gbc.gridy = 0; = 

    gbc.gridwidth = GridBagConstraints.REMAINDER; = 
    gbc.gridheight = 1; 
    gbc.anchor = GridBagConstraints.LINE_START; 
    gbc.insets = new Insets(10, 15, 0, 0); 
    this.add(receiver, gbc); 

    /* Next component*/ 
    gbc.gridx = 0; 
    gbc.gridy = 1; 

    gbc.weightx = 1.; 
    gbc.weighty = 1.; 

    gbc.fill = GridBagConstraints.BOTH; 
    gbc.anchor = GridBagConstraints.LINE_START; // pas WEST. 

    gbc.insets = new Insets(30, 15, 0, 10); 
    this.add(new JScrollPane(textArea_receive, 
      JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), gbc); 


    /* next component */ 
    gbc.gridx = 0; 
    gbc.gridy = 1; 

    gbc.gridwidth = GridBagConstraints.WEST; 

    gbc.fill = GridBagConstraints.HORIZONTAL; 

    gbc.anchor = GridBagConstraints.BASELINE; 

    gbc.insets = new Insets(15, 15, 15, 10); 
    this.add(new JScrollPane(textArea_send, 
      JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), gbc); 

    /* Button */ 
    gbc.gridx = 1; 
    this.add(send_button); 


    textArea_receive.setEditable(false); 
    textArea_send.setEditable(true); 
    send_button.addActionListener(this); 

    DefaultCaret caret = (DefaultCaret)textArea_receive.getCaret(); 
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); 

} 

我想我的窗口井井有条这样,如果我们扩大或缩小它,只有两个文本区域会改变大小。目前,textAreas不会调整大小。

欢迎任何建议!

由于提前,

维克多

+0

提示:不要试图通过反复试验来学习的Swing UI和“布点”和错误。从一个完整的教程开始(从一个可靠的源代码,例如https://docs.oracle.com/javase/tutorial/uiswing/index.html ...),然后解决这个问题。 **有太多的事情**,为了让UI程序正确,人们必须知道**;所以试验和错误只是意味着:大量的试验,很多错误和几乎没有奖励的进步使经验。但是:很好的第一个问题。我希望更多的新手能够在他们的问题上花费如此多的精力! – GhostCat

回答

2

你的类扩展JPanel。 JPanel的默认布局管理器是FlowLayout,这就是组件按照原样显示的原因。

JPanel AreaGrid = new JPanel(); 
AreaGrid.setLayout(new GridBagLayout()); 

上面的代码什么都不做,因为您从不向“areaGrid”面板添加任何组件。摆脱这些陈述。

你需要做的是设置类本身的布局,因为你直接添加所有的组件添加到您的自定义类:

//JPanel AreaGrid = new JPanel(); 
//AreaGrid.setLayout(new GridBagLayout()); 
this.setLayout(new GridBagLayout());