gpt4 book ai didi

java - 使用 GridBagLayout 定位 JPanel

转载 作者:行者123 更新时间:2023-12-01 13:00:27 27 4
gpt4 key购买 nike

我尝试使用 GridBagLayout 设置由两个 JPanel 和一个 JMenuBar 组成的 GUI。现在看起来像:

image

我想像这样显示它:

Image

(不幸的是,由于声誉不够,我无法发布图片)

我尝试了不同的 GridbagConstraints 设置,但无法实现。

使用GridBagLayout,当主框架变小时,组件是否可以具有相同的大小,而另一方面,当框架变大时,组件将填充框架?

显示类

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;
}
}

右面板

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();
}
}

选项卡面板

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);
}
}

文本面板

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])));
}
}

}

最佳答案

super(new GridLayout(1, 1));

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

我可能会使用BorderLayout。将主面板添加到CENTER(这样它可以随着可用空间的变化而增大/缩小),将辅助面板添加到EAST(它将保持固定大小)。

关于java - 使用 GridBagLayout 定位 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23551078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com