Iam尝试使用GridBagLayout设置包含两个JPanel和一个JMenuBar的GUI。 现在它看起来像:
image http://oi62.tinypic.com/f441sk.jpg
我想像这样显示:
Image http://i62.tinypic.com/2z4elxc.png
(很遗憾,由于声誉不足,我无法发布图片)
我尝试过不同的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])));
}
}
}
答案 0 :(得分:1)
super(new GridLayout(1,1));
不要使用GridLayout,它会使所有组件的大小相等。
我可能会使用BorderLayout
。将主面板添加到CENTER
(因此可以随着可用空间的变化而增大/缩小),将辅助面板添加到EAST
(它将保持固定大小)。
答案 1 :(得分:0)
GridBagLAyout真的很难做对。
通过使用BorderLayout以及可能的一些其他基本布局将面板嵌套在彼此内部,几乎总是更容易实现您想要的效果。
BorderLayout将始终尝试尽可能地扩展中心元素,沿边缘(北,南,东,西)缩小内容。
底部的按钮(&#34;打开文件&#34;等)应添加到带有基本布局管理器(如FlowLayout)的jpanel中。然后应该将该面板添加到位于SOUTH的主面板中(假设您使用borderLayout)。具有所有字母的面板应该类似地添加在EAST的位置。最后,可以在CENTER中添加主文本面板。然后Swing将尝试尽可能地扩展CENTER部件,并尽可能合理地压缩SOUTH和EAST部件。