Java JXBrowser:是否可以覆盖JXBrowser组件?

时间:2019-05-23 07:08:35

标签: java graphics paint paintcomponent jxbrowser

我只是想知道是否可以在JXBrowser组件上绘制?我在互联网上进行搜索,发现所使用的BrowserView继承了paintcomponents等。但是我似乎无法使其正常工作。

代码如下:

        public test() {
    browser = new Browser();
    view = new BrowserView(browser);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(view, BorderLayout.CENTER);

    test = new JButton("Open FOE");
    test.addActionListener(this);
    test1 = new JButton("Helpen");
    test1.addActionListener(this);
    test1.setMaximumSize(new Dimension(90, 20));
    JPanel panel1 = new JPanel();
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
    panel1.add(test);
    panel1.add(Box.createRigidArea(new Dimension(0, 15)));
    panel1.add(test1);

    JPanel panel2 = new JPanel();
    panel2.setLayout(new BorderLayout());
    panel2.add(panel, BorderLayout.CENTER);
    panel2.add(panel1, BorderLayout.EAST);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel2);
    frame.setSize(1500, 1000);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

现在的样子:

First

我想要实现的目标: Second

1 个答案:

答案 0 :(得分:0)

使用玻璃窗格, 示例代码片段:

JPanel glassPane = new JPanel()
    {
      @Override
      protected void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        g.drawString("Test",100,100);
        g.setColor(Color.BLUE);
        g.drawRect(300,300,300,300);
      }
    };

    glassPane.setOpaque(false);   
    frame.getGlassPane().setVisible(true);

    frame.setGlassPane(glassPane);
    frame.setVisible(true);