我试图制作3个标签,每个标签中都有一个文本字段,多个按钮和复选框以及一个大文本区域。
问题是当我尝试同时将GridBagLayout
或GridLayout
与JTabbedPane
一起使用时,它什么都没有显示或者将所有三个标签都带进中间的一个小盒子。我不知道我们是否可以同时使用它们?
但是我想使用某种布局将文本设置在顶部,然后在每个选项卡的中间使用按钮,在选项卡的底部使用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);
}
}
答案 0 :(得分:2)
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进行流输出。任何帮助将不胜感激。这是我的代码:
因此,也许您从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);
答案 1 :(得分:-1)
您不需要在JFrame中设置布局管理器,因为您将只添加一个组件:jTabPane。默认情况下,所有组件都是容器,但仅接受1个组件,如果要添加两个或更多组件,则需要设置布局管理器。在这种情况下,您不需要在Jframe中设置布局管理器,但是需要在添加为jTabPane中的选项卡的每个jpanel上设置布局管理器。因为您没有在每个jpanel上都设置一个布局管理器,所以它将仅显示1个组件,最后一个组件。
我建议您使用MigLayout,它简单而强大。