簡體   English   中英

使用GridBagLayout定位JPanels

[英]Positioning JPanels with GridBagLayout

我試圖使用GridBagLayout設置由兩個JPanel和一個JMenuBar組成的GUI。 現在看起來像:

圖片

我想顯示如下:

圖片

(不幸的是,由於信譽不足,我無法發布圖片)

我嘗試了不同的GridbagConstraints設置,但我無法實現。

GridBagLayout是否有可能在主框架變小時組件具有相同的大小,而另一方面,當主框架變大時組件將填充框架?

DisplayClass

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class DisplayClass extends JFrame {

JPanel tabPanel;
JPanel rightPanel;
JMenuBar menu;
JPanel textPanel; 

public DisplayClass()
{
    textPanel =  TextPanel.getTextPanel();
    rightPanel = RightPanel.getRightPanel();;
    tabPanel = TabbedPane.getTabbedPane();

    this.getContentPane().add(setContent());
    this.setLocation(0, 0);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(1000, 700);
    this.setVisible(true);  
}

public static void main(String args[]) 
{         
    DisplayClass frame = new DisplayClass();
    MainMenu menu = MainMenu.getMainMenu();
    frame.setJMenuBar(menu.createJMenuBar());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private  Container setContent()
{
         JPanel content = new JPanel();
      content.setLayout(new GridBagLayout());
      GridBagConstraints g = new GridBagConstraints();        

      g.gridx = 0;
      g.gridy = 0;
      g.gridwidth = 1;
      g.gridheight = 1;
      g.ipadx = 10;
      g.ipady = 3;
      g.fill = GridBagConstraints.VERTICAL;
      g.anchor = GridBagConstraints.NORTHWEST;
      g.weightx = 1;
      g.weighty = 1;
      content.add(tabPanel, g);

      g.fill = GridBagConstraints.VERTICAL;
      g.gridx = 1;
      g.gridy = 0;
      g.gridwidth = 1;
      g.gridheight = 1;
      g.ipadx = 10;
      g.ipady = 3;
      content.add(rightPanel, g);

      return content;
}
}

RightPanel

import javax.swing.*;
import javax.swing.border.*;   
import java.awt.*;
import java.awt.event.*;   

public class RightPanel extends JPanel
{
private JLabel charLabel;
private JTextField letterCount;
private JTextField charField;
private GridBagConstraints g;
private JButton replaceButton;


private RightPanel()
{
    this.setSize(800, 700);
    this.setKomponents();
}


private void setKomponents(){

  this.setLayout(new GridBagLayout());
  g = new GridBagConstraints();


  char letter = 'A';

  for (int i = 0; i < 26; i++) 
  {   
          charLabel = new JLabel(String.valueOf(letter)); 
          g.fill = GridBagConstraints.VERTICAL;
          g.gridx = 0;
          g.gridy = i;
          g.ipadx = 10;
          g.ipady = 3;
         // g.anchor = g.FIRST_LINE_END;
          this.add(charLabel, g);

          charField = new JTextField(String.valueOf(letter)); 
          g.fill = GridBagConstraints.VERTICAL;
          g.gridx = 1;
          g.gridy = i;
          g.ipadx = 10;
          g.ipady = 3;
          charField.setHorizontalAlignment(SwingConstants.CENTER);
          this.add(charField, g);

          letterCount = new JTextField(String.valueOf(letter)); 
          letterCount.setEditable(false);
          letterCount.setHorizontalAlignment(SwingConstants.CENTER);
          g.fill = GridBagConstraints.VERTICAL;
          g.gridx = 2;
          g.gridy = i;
          g.ipadx = 10;
          g.ipady = 3;
          this.add(letterCount, g);

          letter++;          
}
  replaceButton = new JButton("Replace"); 
  g.fill = GridBagConstraints.VERTICAL;
  g.gridx = 0;
  g.gridy = 27;
  g.gridheight = 1;
  g.gridwidth = 3;
  g.ipadx = 3;
  g.ipady = 3;
   this.add(replaceButton, g);   
}

public static JPanel getRightPanel(){

 return new RightPanel();
}  
}

TabbedPanel

import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.BorderLayout; 
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

public class TabbedPane extends JPanel 
{
private JTabbedPane tabbedPane;
private TabbedPane() 
{    
    super(new GridLayout(1, 1));

     this.tabbedPane = new JTabbedPane();

     this.createPage1();
     this.createPage2();
     this.createPage3();
     this.createPage4();

    add(tabbedPane);

    //The following line enables to use scrolling tabs.
    //tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}

public static JPanel getTabbedPane()
{
    return new TabbedPane();
}


void createPage1()
{
    JPanel panel1 = TextPanel.getTextPanel();
     this.tabbedPane.addTab("Main", panel1);
     this.tabbedPane.setMnemonicAt(0, KeyEvent.VK_2);  
}


void createPage2()
{
    double values [] = new double [3];
    values [0] = 3;
    values [1] = 6;
    values [2] = 15;
    String names [] =  {"a","b","c"};


     JPanel panel2 = UnigramPanel.getUnigramPanel(values, names, "Unigram graph");
     this.tabbedPane.addTab("tab1", panel2);
     this.tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
}

void createPage3()
{
    double values [] = new double [3];
    values [0] = 3;
    values [1] = 6;
    values [2] = 15;
    String names [] =  {"a","b","c"};


     JPanel panel3 = UnigramPanel.getUnigramPanel(values, names, "Bigram graph");
     this.tabbedPane.addTab("tab2", panel3);
     this.tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
}


void createPage4()
{
    double values [] = new double [3];
    values [0] = 3;
    values [1] = 6;
    values [2] = 15;
    String names [] =  {"a","b","c"};


     JPanel panel4 = UnigramPanel.getUnigramPanel(values, names, "Trigram graph");
     this.tabbedPane.addTab("tab3", panel4);
     this.tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
}

private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TabbedPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(getTabbedPane(), BorderLayout.CENTER);    

    //Display the window.
    frame.pack();  
    frame.setVisible(true);
} 
}

TextPanel

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 

public class TextPanel extends JPanel
{
private BufferedReader input;
private String line;
private JFileChooser fileChoser;
private final JTextArea textArea;
private JButton openFileButton;
private JButton saveFileButton;
private JButton countButton;

private TextPanel()
{
  line = new String();
  fileChoser = new JFileChooser(); 
  this.textArea = new JTextArea(30, 60);   
  this.displayGUI();
}


public static JPanel getTextPanel(){

     return new TextPanel();
  }

public void displayGUI()
{
  textArea.setLineWrap(true);  
  textArea.setWrapStyleWord(true);  

  this.openFileButton = new JButton("OPEN FILE");
  this.openFileButton.addActionListener(new ALOpenFileButton());
  this.saveFileButton = new JButton("SAVE FILE");
  this.saveFileButton.addActionListener(new ALSaveFileButton());
  this.countButton = new JButton("button");
  this.countButton.addActionListener(new ALCountButton());

  this.setLayout(new GridBagLayout());     
  GridBagConstraints g = new GridBagConstraints();

  g.fill = GridBagConstraints.VERTICAL;
  g.gridx = 0;
  g.gridy = 0;
  g.gridwidth = 2;
  g.ipadx = 10;
  g.ipady = 3; 
  this.add(textArea, g);

  JScrollPane scrollPane = new JScrollPane( this.textArea );
  g.fill = GridBagConstraints.VERTICAL;
  g.gridx = 1;
  g.gridy = 0;
  g.gridwidth = 0;
  this.add(scrollPane,g);


  g.gridx = 0;
  g.gridy = 1;
  g.ipadx = 10;
  g.ipady = 3;
  g.fill = GridBagConstraints.VERTICAL;
  g.anchor = GridBagConstraints.SOUTHWEST;
  this.add(openFileButton, g);


  g.gridx = 1;
  g.gridy = 2;
  g.ipadx = 10;
  g.ipady = 3;
  g.fill = GridBagConstraints.VERTICAL;
  g.anchor = GridBagConstraints.SOUTHWEST;
  this.add(saveFileButton, g);   

  g.fill = GridBagConstraints.VERTICAL;
  g.anchor = GridBagConstraints.SOUTHEAST;
  g.gridx = 2;
  g.gridy = 1;
  g.ipadx = 10;
  g.ipady = 3;
  this.add(countButton, g);   
}

private class ALOpenFileButton implements ActionListener
{

 public void actionPerformed(ActionEvent ae)
   {         
     FileOperations file = FileOperations.getFileOperations();
     file.openFile(textArea, fileChoser);
   }
}

private class ALSaveFileButton implements ActionListener
{

 public void actionPerformed(ActionEvent ae) 
 {
     FileOperations file = FileOperations.getFileOperations();
     file.saveFile(textArea, fileChoser);


 }
}

private class ALCountButton implements ActionListener
{

 public void actionPerformed(ActionEvent ae) 
 {
      FrequentAnalysis fa = FrequentAnalysis.getFrequentAnalysis();
      //String unigramCount = fa.countUnigrams(textArea);
      int[] unigramCount = fa.countUnigrams(textArea);
      int[][] bigramCount = fa.countBigrams(textArea);
      int[][][] trigramCount = fa.countTrigrams(textArea);

      textArea.append((Integer.toString(trigramCount[0][0][0])));
 }
}

}

超級(新GridLayout(1,1));

不要使用GridLayout,它會使所有組件的大小相等。

我可能會使用BorderLayout 將主面板添加到CENTER (以便可以隨着可用空間的變化而增加/縮小),將輔助面板添加到EAST (其將保持固定大小)。

GridBagLAyout確實很難正確解決。

通過使用BorderLayout以及其他一些基本布局將面板相互嵌套在一起,幾乎總是容易實現所需的功能。

BorderLayout將始終嘗試盡可能擴大中心元素,沿邊緣(北,南,東,西)縮小內容。

底部的按鈕(“打開文件”等)應添加到具有基本版式管理器(例如FlowLayout)的jpanel中。 然后,應將該面板添加到主面板的SOUTH位置(假設您使用borderLayout)。 類似地,所有字母的面板應類似地添加到EAST位置。 最后,可以在CENTER中添加主文本面板。 然后,Swing將嘗試盡可能擴展CENTER部分,並盡可能合理地壓縮SOUTH和EAST部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM