gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:44:54 24 4
gpt4 key购买 nike

我正在尝试创建 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());

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

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

But I would like to use some sort of layout to set the text filed on the top then in the middle of each tab use buttons and on the bottom of the tab have Jtextarea for stream output. Any help will be appreciated. Here is my code:

因此,也许您可​​以从主面板的 BorderLayout 开始添加到选项卡。然后您可以根据需要创建其他面板,以在 BorderLayout 的 PAGE_START、CENTER、PAGE_END 中显示该组件。阅读 Layout Manager 上的 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);

关于java - 如何在 JTabbedPane 中使用布局管理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52656518/

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