如何在 JTabbedPane 中使用布局管理器?



我正在尝试制作 3 个选项卡,在每个选项卡中,我有一个文本字段和多个按钮和复选框以及一个大文本区域。

问题是当我尝试同时将GridBagLayoutGridLayoutJTabbedPane一起使用时,要么它不显示任何内容,要么将所有三个选项卡放在中心的一个小框中。不知道能不能把两者用在一边?

但是我想使用某种布局来设置顶部的文本,然后在每个选项卡的中间使用按钮,并在选项卡的底部JTextArea用于流输出。

这是我的代码:

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.io.PrintStream;
import javax.swing.JTabbedPane;
public class FrameFinder extends JFrame  {
private static final int FRAME_HEIGHT = 600;
private static final int FRAME_WIDTH = 500;
private final Font LARGE_FONT = new Font(Font.SERIF, Font.BOLD, 34);
public static JTextField text;
private JPanel panel;
private JButton button;
private JLabel label;
private JMenuBar menubar;
private KeyListener newkey;
private JMenuItem exit;
private JMenuItem about;
private JTabbedPane jtap;
private JTextArea textArea;
static private final String newline = "n";
// constructor
public FrameFinder(){
}
//creating constructor
public FrameFinder(String str){
createFrame(str);
CreateTabbedPane();
}
// click listener
private void addMenuListeners(){
exit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
exitActionPerformed();
}
});
about.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//  aboutActionPerformed();
}
});
}
private void exitActionPerformed(){
dispose();
}

//creating frame
private void createFrame(String title) {
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
setResizable(false);
}
// here we create the tab bar
public void CreateTabbedPane() {
//  super(String.valueOf(new BorderLayout()));
setTitle("Tabbed Pane");
//setLayout(new GridLayout());
jtap = new JTabbedPane();
getContentPane().add(jtap);
textArea = new JTextArea(50, 10);
textArea.setEditable(false);
// PrintStream printStream = new PrintStream(new CustomOutputStream(textArea));
//JPanel controls = new JPanel();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();

setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 10, 10);
constraints.anchor = GridBagConstraints.WEST;
jp1.add(new JTextField("Find:", 20));
jp1.add(new JButton("Search"));
jp2.add(new JTextField("Directory path :", 20));
jp2.add(new JButton("Browser "));
jp1.add(new JCheckBox("match whole word."));
jp1.add(new JCheckBox("Match case."));
jp3.add(new JTextArea(50, 10));
jp2.add(new JTextField("Find in folders:", 20));
jp2.add(new JButton("Search"));
jp2.add(new JTextField("Directory path :", 20));
jp2.add(new JButton("Browser "));
jp2.add(new JCheckBox("match whole word."));
jp2.add(new JCheckBox("Match case."));
jp3.add(new JTextArea(50, 10));
jp3.add(new JTextField("Find in folders:", 20));
jp3.add(new JButton("Search"));
jp2.add(new JTextField("Directory path :", 20));
jp2.add(new JButton("Browser "));
jp3.add(new JCheckBox("match whole word."));
jp3.add(new JCheckBox("Match case."));
jp3.add(new JTextArea(50, 10));
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel labe13 = new JLabel();
// label2.setText("You are in area of Tab2");
createFrame("Finder");
jp1.add(label1);
jp2.add(label2);
jp3.add(labe13);

jtap.addTab("Find ", jp1);
jtap.addTab("Replace All in one file", jp2);
jtap.addTab("Replace All  in Multi files",jp3);
}
}
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();

setLayout(new GridBagLayout());

上面的代码正在设置框架的布局管理器,这不是您想要做的。

面板的默认布局管理器是 FlowLayout(因为您没有指定布局管理器(。

您希望为每个面板设置布局管理器:

JPanel jp1 = new JPanel( pick a layout manager);
JPanel jp2 = new JPanel( pick a layout manager);
JPanel jp3 = new JPanel( pick a layout manager);
//setLayout(new GridBagLayout());

因此,添加到选项卡的每个面板都可以具有不同的布局管理器,以实现所需的布局。您还可以使用不同的布局管理器嵌套面板,为您提供更大的灵活性。

每个选项卡的布局完全独立于其他选项卡。唯一的关系是所有选项卡共享相同的空间,因此选项卡式窗格的首选大小将是最大选项卡的首选大小。

但是我想使用某种布局来设置顶部的文本,然后在每个选项卡的中间使用按钮,并在选项卡底部有用于流输出的 Jtextarea。任何帮助将不胜感激。这是我的代码:

因此,也许您从主面板的边框布局开始,以添加到选项卡中。然后,您可以根据需要创建其他面板,以便在边框布局的PAGE_START、中心PAGE_END中显示组件。阅读布局管理器上的 Swing 教程,了解一些入门示例。

编辑:

根据我上面的建议,您可以执行以下操作:

JPanel top = new JPanel( new GridLayout(0, 3) );
top.add( new JLabel("Label1") );
top.add( new JLabel("Label2") );
top.add( new JLabel("Label3") );
JTextArea textArea = new JTextArea(5, 30);
JPanel bottom = new JPanel( new GridLayout(0, 2) );
bottom.add( new JButton("Button1") );
bottom.add( new JButton("Button2") );
JPanel tab1 = new JPanel( new BorderLayout() );
tab1.add(top, BorderLayout.PAGE_START);
tab1.add(new JScrollPane(textArea), BorderLayout.CENTER);
tab1.add(bottom, BorderLayout.PAGE_END);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Tab1", tab1);
frame.add(tabbedPane);

你不需要在JFrame中设置布局管理器,因为你只需要添加一个组件:jTabPane。默认情况下,所有组件都是容器,但只接受 1 个组件,如果要添加两个或更多组件,则需要设置布局管理器。在这种情况下,您不需要在 Jframe 中设置布局管理器,但您需要在 jTabPane 中作为选项卡添加的每个 jpanel 上设置布局管理器。因为您没有在每个 jpanel 上设置布局管理器,所以它只会显示 1 个组件,即最后添加的组件。

我建议您使用MigLayout,它简单且功能强大。

最新更新