繁体   English   中英

如何在特定的 swing 选项卡式窗格中添加其他 swing 组件?

[英]How do I add other swing components in a specific swing tabbed pane?

我刚刚开始学习 Java Swing 并且我正在制作一个应用程序表单类型的项目,我想在特定选项卡中添加更多组件,如按钮、文本区域和其他组件,但我无法做到。 代码如下:

 import javax.swing.*;
public class TabbedPaneExample {
    JFrame f;
    TabbedPaneExample(){
        f=new JFrame("Hotel Appication Form");
        JTextArea ta=new JTextArea(400,400);
        JPanel p1=new JPanel();
        p1.add(ta);
        JPanel p2=new JPanel();
        JPanel p3=new JPanel();
        JTabbedPane tp=new JTabbedPane();
        tp.setBounds(39,20,500,500);
        tp.add("form",p1);
        tp.add("preferences",p2);
        tp.add("FaQ's",p3);
        f.add(tp);
        f.setSize(600,600);
        f.setVisible(true);

        //JButton
        JButton b = new JButton("Submit");
        b.setBounds(50,50,30,20);
        f.add(b);
        //JLabel

    }
    public static void main(String[] args) {
        new TabbedPaneExample();
    }
  }

output的截图附在这里

在此代码示例中,带有选项卡式面板的简单框架和每个选项卡中的简单组件。 您的问题是向 JPanel 添加组件不正确。 希望对你有帮助!

输出1 输出2

import java.awt.GridLayout;
import javax.swing.*;

public class test {

    private static void createAndShowGUI() {

        // Create and set up the window.
        final JFrame frame = new JFrame("Split Pane Example");

        // Display the window.
        frame.setSize(500, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // set grid layout for the frame
        frame.getContentPane().setLayout(new GridLayout(1, 1));

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        JPanel panel = new JPanel();
        JPanel panel2 = new JPanel();
        JButton button = new JButton("Button");
        JLabel label = new JLabel("Label");
        JTextField textField = new JTextField("TextField");

        panel.add(button);
        panel.add(label);
        panel2.add(textField);
        tabbedPane.addTab("Tab1", panel);
        tabbedPane.addTab("Tab2", panel2);

        frame.getContentPane().add(tabbedPane);

    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM