gpt4 book ai didi

java - 带菜单栏的 swing JFrame 上的投影

转载 作者:行者123 更新时间:2023-11-30 05:26:58 25 4
gpt4 key购买 nike

我正在尝试使用不昂贵的绘图矩形在具有菜单栏的 JFrame 周围模拟投影,但阴影显示在不包括菜单栏的下部周围。

知道如何实现整个窗口的效果吗?

enter image description here

package com.dropshadow;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;

import org.nuiton.jaxx.runtime.swing.ComponentMover;
import org.nuiton.jaxx.runtime.swing.ComponentResizer;

public class DropShadowMenu {

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

private static void initFrame() {
final DropShadowTool shadow = new DropShadowTool();
JFrame frame = new JFrame() {
private static final long serialVersionUID = -512601712971605848L;

@Override
public void paint(Graphics g) {
super.paint(g);
shadow.applyShadow(this, (Graphics2D) g);
}
};

frame.setUndecorated(true);
frame.setBackground(new Color(255, 255, 255, 0));
frame.setPreferredSize(new Dimension(400, 600));

JPanel contentPane = new JPanel() {
private static final long serialVersionUID = -4799881378955761842L;
@Override
public void paint(Graphics g) {
super.paint(g);
shadow.applyShadow(this, (Graphics2D) g);
}

};

contentPane.setBorder(new EmptyBorder(20, 20, 20, 20));
contentPane.setLayout(new BorderLayout());
frame.setContentPane(contentPane);
contentPane.setBackground(new Color(0,0,0,0));
JPanel content = new JPanel();
contentPane.add(content);
JMenuBar menuBar = new JMenuBar();
menuBar.setBorder(new EmptyBorder(0,20,0,20));
menuBar.setBackground(new Color(255, 0, 255, 255));
frame.setJMenuBar(menuBar);

JPanel menuPanel = new JPanel();

menuPanel.setBorder(BorderFactory.createLineBorder(Color.red));
menuPanel.setLayout(new BorderLayout());
JMenu file = new JMenu("File");
file.setMnemonic('f');
JMenuItem exit = new JMenuItem("Exit");
exit.setMnemonic('x');
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.ALT_DOWN_MASK));
menuBar.add(file);
file.add(exit);
exit.addActionListener(e ->
{
System.out.println("Exiting - Bye!");
System.exit(0);
} );
JMenu menu = new JMenu("Menu 1");
menuBar.add(menu);
menu.add(new JMenuItem("Item 1"));
menu.add(new JMenuItem("Item 2"));
JMenu subMenu = new JMenu("SubMenu 1");
subMenu.add(new JMenuItem("Item 3"));
subMenu.add(new JMenuItem("Item 4"));
menuBar.add(Box.createHorizontalGlue());
JButton x = new JButton("X");
x.setBorder(null);
x.setBackground(new Color(0,0,0,0));
menuBar.add(x);
x.addActionListener(e -> System.exit(0));
menu.add(subMenu);
content.setLayout(new BorderLayout());
JButton button = new JButton("bla blah");
button.addActionListener(e->System.out.println("bla bla button"));
content.add(button, BorderLayout.SOUTH);
content.setBackground(new Color(255,255,255));
JButton jx = new JButton(". X .");
jx.setBackground(new Color(255,255,255));
menuPanel.add(jx, BorderLayout.EAST);
jx.addActionListener(e -> System.exit(0));
ComponentMover cm = new ComponentMover();
cm.registerComponent(frame);
ComponentResizer cr = new ComponentResizer();
cr.registerComponent(frame);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

private static List<Color> colors = new ArrayList<Color>();
static {

for (int i=0;i<20;i++) {
colors.add(new Color(0, 0, 0, 5*i));
}
}

private static class DropShadowTool {
private void applyShadow(Container c, Graphics2D g) {
if(c.getName().equals("Charbel"))
System.out.println(c);
Insets insets = c.getInsets();
System.out.println(insets);

Dimension size = c.getSize();
int x0 = 0;
int xi = insets.left;
int y0 = 0;
int yi = insets.top;
int width0 = size.width;
int widthi = size.width - xi - insets.right;
int height0 = size.height;
int heighti = size.height - yi - insets.bottom;

int last = 0;
for (int i = 0; i < insets.left; i++) {
last = i;
drawLines(g, x0, y0, width0, height0, i);
}
drawLines(g, x0, y0, width0, height0, last);
}

private void drawLines(Graphics2D g, int x0, int y0, int width0, int height0, int i) {
int x = x0 + i;
int y = y0 + i;
int width = width0 - i - i;
int height = height0 - i - i;
g.setColor(colors.get(i % colors.size()));
g.drawRect(x, y, width, height);
}
}
}

最佳答案

我建议您的投影只是一个自定义边框,应该这样实现。

然后您应该能够将 DropShadowBorder 添加到任何 Swing 组件,而无需在组件上进行任何自定义绘制。

那么我建议您可以设置JRootPaneBorder。根 Pane 在更高级别上管理菜单栏和内容 Pane ,因此边框应围绕这两个组件。

阅读 Swing 教程中关于 The Root Pane 的部分。

关于java - 带菜单栏的 swing JFrame 上的投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58359767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com