我有应该显示一些信息的简单GUI。我创建了多个JTabbedPanes的菜单,并且选项卡按预期相互堆叠。我正在尝试删除每个选项卡的左侧,右侧和底部的边框,以使其看起来不会堆叠。我还没有找到能解决我问题的方法,实际上有办法吗?

谢谢您提前回答。

这是屏幕截图:

java - JAVA JTabbedPane-去除堆叠标签的边框-LMLPHP

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import javax.swing.*;

public class Convertor implements ItemListener {

public void addComponentToPane(Container pane) throws FileNotFoundException {
    //main panel containing all below
    JPanel mainPanel = new JPanel(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();

    //Menu panel
    JPanel menuPanel = new JPanel();
    menuPanel.setMinimumSize(new Dimension(1200, 30));
    menuPanel.setMaximumSize(new Dimension(1200, 30));
    menuPanel.setPreferredSize(new Dimension(1200, 30));
    menuPanel.setBackground(Color.BLUE);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0;
    gbc.weighty = 0;
    gbc.ipadx = 0;
    gbc.ipady = 0;
    gbc.insets = new Insets(0, 0, 0, 0);
    gbc.gridx = 0;
    gbc.gridy = 0;
    mainPanel.add(menuPanel, gbc);

    //Menu panel buttons
    JButton importButton = new JButton("Import");
    importButton.setPreferredSize(new Dimension(75, 20));
    menuPanel.add(importButton, BorderLayout.EAST);
    JButton button2 = new JButton("XXX");
    button2.setPreferredSize(new Dimension(75, 20));
    menuPanel.add(button2, BorderLayout.EAST);

    //PAYER PANEL CONTAINER - containing payer panel
    JPanel payerPanelContainer = new JPanel(new BorderLayout());
    //payerPanelContainer.setBackground(Color.RED);
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0;
    gbc.weighty = 1;
    gbc.ipadx = 0;
    gbc.ipady = 0;
    gbc.insets = new Insets(0, 0, 0, 0);
    gbc.gridx = 0;
    gbc.gridy = 1;
    mainPanel.add(payerPanelContainer, gbc);

    //PAYER PANEL
    JPanel payerPanel = new JPanel(new GridBagLayout());
    payerPanel.setBackground(Color.YELLOW);
    payerPanelContainer.add(payerPanel);

    //PAYER PANEL TABBED PANE
    JTabbedPane payerTabbedPane = new JTabbedPane();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.ipadx = 0;
    gbc.ipady = 0;
    gbc.insets = new Insets(0, 0, 0, 0);
    gbc.gridx = 0;
    gbc.gridy = 0;
    payerPanel.add(payerTabbedPane, gbc);

    //Create payer tabs
    JPanel payerTab1 = new JPanel(new BorderLayout());
    payerTabbedPane.addTab("Payer 1", payerTab1);

    JPanel payerTab2 = new JPanel(new BorderLayout());
    payerTabbedPane.addTab("Payer 2", payerTab2);

    //create MSISDN tabbed pane in each payer tab
    JTabbedPane msisdnTabbedPane1 = new JTabbedPane();
    payerTab1.add(msisdnTabbedPane1);

    //create MSISDN tabs
    JPanel generalTabPayer1 = new JPanel(new BorderLayout());
    msisdnTabbedPane1.addTab("GENERAL", generalTabPayer1);

    JPanel msisdnTab1 = new JPanel(new BorderLayout());
    msisdnTabbedPane1.addTab("MSISDN 1", msisdnTab1);

    JPanel msisdnTab2 = new JPanel(new BorderLayout());
    msisdnTabbedPane1.addTab("MSISDN 2", msisdnTab2);

    JPanel msisdnTab3 = new JPanel(new BorderLayout());
    msisdnTabbedPane1.addTab("MSISDN 2", msisdnTab3);

    //create MSISDN options tabbed pane
    JTabbedPane msisdnOptionsTabbedPane1 = new JTabbedPane();
    msisdnTab1.add(msisdnOptionsTabbedPane1);

    //create MSISDN options tabs
    JPanel msisdnOptionsTab1 = new JPanel(new BorderLayout());
    msisdnOptionsTabbedPane1.addTab("Option 1", msisdnOptionsTab1);

    JPanel msisdnOptionsTab2 = new JPanel(new BorderLayout());
    msisdnOptionsTabbedPane1.addTab("Option 2", msisdnOptionsTab2);

    //add main panel to window
    pane.add(mainPanel);

}


private static void createAndShowGUI() throws FileNotFoundException {
    //Create and set up the window.
    JFrame frame = new JFrame("CSV Reader");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    Convertor demo = new Convertor();
    demo.addComponentToPane(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setSize(1206, 800);
    frame.setMinimumSize(new Dimension(1206, 800));
    frame.setMaximumSize(new Dimension(1206, 800));
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {

            try {
                createAndShowGUI();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}

@Override
public void itemStateChanged(ItemEvent arg0) {
    // TODO Auto-generated method stub

}


}

最佳答案

可能,但是很难。唯一的方法是为选项卡式窗格提供自己的UI。这是一个示例,它不能完全满足您的要求,但是可以显示您必须走的路(对不起,但是我不能提供完整的解决方案,因为它可能会花费很多时间)。

import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalTabbedPaneUI;


public class BorderlessTabsExample implements Runnable {

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(MetalLookAndFeel.class.getName());
        } catch (Exception e) {
            // Nothing
        }
        SwingUtilities.invokeLater(new BorderlessTabsExample());
    }

    @Override
    public void run() {
        JTabbedPane tabber = new JTabbedPane();
        tabber.addTab("First", new JLabel("First"));
        tabber.addTab("Second", new JLabel("Second"));
        tabber.addTab("Third", new JLabel("Third"));
        // set the UI which will paint your tabs
        tabber.setUI(new MetalBorderlessTabbedPaneUI());
        JFrame frm = new JFrame("Tabber test");
        frm.add(tabber);
        frm.setSize(500, 400);
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }

    private static class MetalBorderlessTabbedPaneUI extends MetalTabbedPaneUI {

        @Override
        protected void installDefaults() {
            super.installDefaults();
            if (contentBorderInsets != null) {
                contentBorderInsets = new Insets(contentBorderInsets.top, 0, 0, 0);
            }
        }

        @Override
        protected void paintContentBorderRightEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
            // Do nothing
        }

        @Override
        protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
            // Do nothing
        }

        @Override
        protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
            // Do nothing
        }

    }
}

关于java - JAVA JTabbedPane-去除堆叠标签的边框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51944426/

10-15 21:59