Java JTabbedPane Inset Color

…衆ロ難τιáo~ 提交于 2019-12-01 05:18:23

问题


I was wondering how you would get the color of the inset of a JTabbedPane. I cannot seem to get this color. Every attempt I make I get 236,236,236 which is the outside frame color, where the inside frame color is about 10 darker, 227,227,227 (using the built in apple color meter).

I am setting the look and feel using UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

You can see this in an image that I found on the internet. http://pagesofinterest.net/wordpress/wp-content/uploads/2009/06/Quaqua-Maven-Netbeans.jpg Where the words "Panel's Title" is the area that I am getting the lighter color that is not useful to me. Inside the round corners is the darker color I am trying to obtain. I tried getting the color of the content pane to no avail.

Thanks for all your help!

**EDIT:**Added code! As you see, I am trying to get the color of the area inside the rounded corners(if your on a mac) not the color of the frame or the tabs that say "1" "2". I have attached a photo and I am trying to get the background color of the portion that says "Here" Thanks!

import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;

public class main {
JFrame frame;
Container c1 = new Container();
Container c2 = new Container();
JTabbedPane top = new JTabbedPane();
static main GUI;
public void createGUI(){
    frame = new JFrame();
    Container c = frame.getContentPane();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    top = new JTabbedPane(JTabbedPane.TOP);
    top.setFocusTraversalKeysEnabled(false);
    top.setFocusable(false);
    top.addTab("1", c1);
    top.addTab("2", c2);
    frame.setSize(315,450);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setResizable(true);
    c.add(top);
    frame.pack();
    frame.setVisible(true);
} 
public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {}
    GUI = new main();
    GUI.createGUI();
}
}

EDIT: camickr, Here is a screenshot of the UIManager Defaults. Unfortunately none of there colors in the are the correct color that the inset is.


回答1:


You might be able to use UIMangaer Defaults to find the color.




回答2:


You can override paintComponent() to use a GradientPaint in the tab's background, as shown below. A few notes,

  • Let the content adopt the preferred size of it contents, as shown here.

  • Construct the GUI in the event dispatch thread.

  • Use conventional Java names.

Addendum: the elements are not always in the same spot, so I do not know what place to get the color.

It sounds like you want to match a color used internally by the TabbedPaneUI delegate. One approach would be as follows:

  • Render a BufferedImage of the component, as shown here.

  • Determine the coordinates of a Point in top relative to the top of c1.

    Point p = SwingUtilities.convertPoint(c1, 0, -1, top);
    
  • Obtain the color using getRGB(), as shown here; use Zoom to verify the result.

import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/** @see https://stackoverflow.com/a/16625260/230513 */
public class Main {

    JFrame frame;
    Container c1 = new GradientPanel();
    Container c2 = new GradientPanel();
    JTabbedPane top = new JTabbedPane();

    private static class GradientPanel extends JPanel {

        public GradientPanel() {
            this.add(new JLabel("Here"));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            GradientPaint p = new GradientPaint(0, 0, Color.white,
                getWidth(), getHeight(), Color.gray);
            g2d.setPaint(p);
            g2d.fillRect(0, 0, getWidth(), getHeight());
        }
    }

    public void createGUI() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        top = new JTabbedPane(JTabbedPane.TOP);
        top.addTab("1", c1);
        top.addTab("2", c2);
        frame.add(top);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().createGUI();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/16622578/java-jtabbedpane-inset-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!