【问题标题】:Java Swing JTabbedPane layoutJava Swing JTabbedPane 布局
【发布时间】:2019-04-04 07:26:24
【问题描述】:

我是 Swing 新手,找不到可以帮助我理解 JTabbedPane 的页面。我找不到控制选项卡式面板组件布局的方法。我可以将每个面板正确地布局为单独的 GUI,但不能像我需要的那样在选项卡式窗格中。我想使用 BorderLayout 而不是 FlowLayout。

另外,您可以看到我正在尝试使用颜色来跟踪我的面板及其组件。我无法设置 JTabbedPane 的背景。它仍然是默认的灰色。谁能告诉我这是为什么?

感谢您提供的任何建议。

What I have so far appears to follow a 'flow layout' despite any changes I've tried

(方法已被删除或几乎被删除以保持代码更短)

公共类 GUIFrame 扩展 JFrame {

public GUIFrame(String title) {
    JFrame frame = new JFrame(title);
    Container c = frame.getContentPane();
    buildGUI(c);
    setFrameAttributes(frame);
}
private void buildGUI(Container c) {
    c.setLayout(new BorderLayout());
    c.setBackground(Color.BLACK);
    JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
    tabs.setBackground(Color.YELLOW);
    c.add("Center", tabs);
    tabs.addTab("Specialty", new SpecialtyPanel());
    tabs.addTab("Treatment", new TreatmentPanel());
    tabs.addTab("Doctor", new DoctorPanel());
    tabs.addTab("Patient", new PatientPanel());
}
private void setFrameAttributes(JFrame f) {
    f.setSize(500, 500);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
    MedicalSystemIO test = new MedicalSystemIO();
    new GUIFrame("Tabbed Title");
}

公共类 SpecialtyPanel 扩展 JPanel 实现 ActionListener {

JTextField jteInput = null; 
DefaultListModel<String> model = new DefaultListModel<String>();
JList<String> list = new JList(model);
JScrollPane pane = new JScrollPane(list);

public SpecialtyPanel() {
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.black));
    buildGUI(panel);    
}
private void buildGUI(JPanel panel) {       
    JPanel jpaInput = createInputPanel();
    JPanel jpaProcess = createProcessPanel();
    JPanel jpaOutput = createOutputPanel();
    //panel.setLayout(new BorderLayout());
    add("North", jpaInput);
    add("Center", jpaProcess);
    add("South", jpaOutput);
}
private JPanel createInputPanel() {
    JPanel jpaInput = new JPanel();
    jpaInput.setBackground(Color.RED);
    return jpaInput;
}
private JPanel createProcessPanel() {
    JPanel jpaProcess = new JPanel();
    jpaProcess.setBackground(Color.BLUE);
    return jpaProcess;
}
private JPanel createOutputPanel() {
    JPanel jpaOutput = new JPanel();
    jpaOutput.add(pane);
    return jpaOutput;
}

【问题讨论】:

    标签: java swing user-interface


    【解决方案1】:

    SpecialtyPanel 以这种方式显示(流布局),因为您以错误的方式将组件放置在其上:

    1. 无需将新面板传递给buildGUI 方法,因为您想将它们直接放在已经是JPanelSpecialtyPanel 上,
    2. 您注释掉了BorderLayout 的设置并且
    3. 您在add 方法中使用了错误的传递布局约束的表示法。

    您的构造函数和构建方法应如下所示:

    public SpecialtyPanel() {
        buildGUI();
    }
    
    private void buildGUI() {
        setBorder(BorderFactory.createLineBorder(Color.black));
        JPanel jpaInput = createInputPanel();
        JPanel jpaProcess = createProcessPanel();
        JPanel jpaOutput = createOutputPanel();
        setLayout(new BorderLayout());
        add(jpaInput, BorderLayout.NORTH);
        add(jpaProcess, BorderLayout.CENTER);
        add(jpaOutput, BorderLayout.SOUTH);
    }
    

    要使面板具有其他颜色而不是灰色,您必须为放置在选项卡式窗格上的组件着色,因为它覆盖了整个空间。在buildGUI 方法中添加想要的颜色,例如:

    private void buildGUI(JPanel panel) {
        // ...
        setBackground(Color.YELLOW);
    }
    

    由于默认情况下 JPanel 是不透明的(这意味着不透明),因此您需要将顶部的面板(除非您明确着色的面板)设置为透明。如果是SpecialtyPanel

    private JPanel createOutputPanel() {
        JPanel jpaOutput = new JPanel();
        jpaOutput.add(pane);
        jpaOutput.setOpaque(false); // panel transparent
        return jpaOutput;
    }
    

    【讨论】:

    • 谢谢。你帮我理清了一些事情。 BorderLayout 被注释掉了,因为它是我尝试更改布局的其中一件事,但对我的操作没有任何影响。
    猜你喜欢
    • 2012-01-29
    • 2018-05-03
    • 2014-12-20
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多