- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我通过将背景 alpha 设置为 0 使 JFrame 背景透明,但是如果我在 linux 中运行该程序,JFrame 背景是白色的,这是为什么?
好的,我发现它与将图形渲染到框架有关,这种方法已经过时了,我不再需要在程序中使用它,但我仍然想知道为什么会发生这种情况(它仅在 Linux 中可以找到)窗)
这是一个可运行的例子
import java.awt.*;
import java.awt.Color;
import javax.swing.*;
class Main extends JFrame{
private void init() {
this.setPreferredSize(new Dimension(1420, 820));
this.setUndecorated(true);
this.setResizable(false);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBackground(new Color(0, 255, 0, 0));
this.requestFocus();
this.setVisible(true);
this.validate();
this.pack();
Color color = UIManager.getColor("activeCaptionBorder");
this.getRootPane().setBorder(BorderFactory.createLineBorder(color, 1));
paintInfo();
}
private void paintInfo() {
Graphics g = this.getGraphics();
g.setColor(new Color(222, 222, 222, 4));
g.setFont(new Font("Arial Black", Font.BOLD, 15));
g.setColor(Color.BLACK);
g.drawString("test String ",this.getWidth()/2, this.getHeight()/2);
g.dispose();
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
new Main().init();
}
}
最佳答案
让我们从...开始
private void paintInfo() {
Graphics g = this.getGraphics();
g.setColor(new Color(222, 222, 222, 4));
g.setFont(new Font("Arial Black", Font.BOLD, 15));
g.setColor(Color.BLACK);
g.drawString("test String ",this.getWidth()/2, this.getHeight()/2);
g.dispose();
}
在 Swing
中绘制和处理不是您创建的 Graphics
上下文的方式不是会导致奇怪的事情吗...
相反,创建你的框架,设置它的透明度,然后向它添加另一个组件,它会完成你想要的实际绘画,例如......
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
*
* @author swhitehead
*/
public class JavaApplication233 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GraphicsEnvironment ge
= GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
//If translucent windows aren't supported, exit.
if (!gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
System.err.println("Per-pixel translucency is not supported");
System.exit(0);
} else {
new JavaApplication233();
}
}
public JavaApplication233() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setOpaque(false);
setLayout(new GridBagLayout());
add(new JLabel("I'm not wearing anything"));
// Color color = UIManager.getColor("activeCaptionBorder");
setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
LinearGradientPaint lgp = new LinearGradientPaint(
new Point(0, 0),
new Point(0, getHeight()),
new float[]{0f, 1f},
new Color[]{applyAlpha(Color.RED), applyAlpha(Color.YELLOW)}
);
g2d.setPaint(lgp);
g2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
g2d.dispose();
}
protected Color applyAlpha(Color color) {
return new Color(color.getRed(), color.getGreen(), color.getBlue(), 64);
}
}
}
关于java - 使用 frame.setBackground(0, 255, 0, 0) 不适用于 linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29026419/
我不认为这是重复的,因为其他问题与 JButtons 和 JPanels 有关。 我想知道为什么 java 中的以下内容不能像人们想象的那样工作: import javax.swing.JApplet
我正在用 Java 制作游戏,目前设置的背景不起作用,但设置的前景有效。这是我的主类代码: import java.awt.*; import javax.swing.*; public class
我是 Java swing 库的新手,目前在设置 JFrame 的背景时遇到了一些问题。 我已阅读 jframe-setbackground-not-working-why和其中的链接,但它似乎不适合
import javax.swing.JApplet; import java.awt.*; public class Snowman extends JApplet { //------------
希望对你来说是一个简单的... 我一直在尝试设置 QtListWidgetItem 的背景颜色,但运气不佳——这可能是因为我没有正确使用 QListWidgetItem...在我的测试代码中,我可以设
我想知道为什么 setBackground() 方法实际上没有使背景变黑。我感觉这与实现 JApplet 的类有关,而不是 Applet,但我无法弄清楚具体细节。这真的很困扰我。如有任何帮助,我们将不
当您创建JFrame实例时,您可以从此实例使用setBackground方法。但是,无论您尝试在那里放置什么颜色,您都会收到灰色背景颜色。 发生这种情况(据我所知),因为默认的 JPanel 实例是在
我一直在到处寻找解决我的问题的方法,但没有找到任何有效的方法:要求:在 jButton“颜色!”的红色和绿色背景颜色之间切换 状态:当我第一次单击该按钮时,它会变为红色,并且在下次单击时不会变为绿色。
我有一个非常简单的程序,要求用户单击其背景颜色与较大面板(又名显示面板)的背景颜色相匹配的四个面板之一,该面板随机将其背景颜色设置为四个面板之一。如果面板错误点击Joptionpane就出来了;显示面
我有一个图像按钮: 这是从我的主要 Activity 开始的单独 Activity 的一部分: Intent intent = new Intent(this, BookRead.class); s
我正在尝试为我的 swing UI 的某些区域着色,但感觉受限于使用 .setBackground(Color.//color here 有什么方法可以在其中使用更具体和独特的颜色(甚至没有棕色)的十
我正在编写一个带有测验元素的程序,当用户得到错误的答案时,会给出反馈。问题 JFrame 由具有实际问题的 JLabel 和 4 个具有不同选项(名为 rad1、rad2、rad3、rad4)的 JR
我有一个使我成为图像按钮的类(class)。这个图像按钮包含一个 ImageView 、两个 TextView 和一个按钮。 我在 ImageView 中放置了一些宽度,如果我将一个图像放入 xml
我在一本书中看到过这个例子,工作正常,但出现的唯一问题是背景在第一次调用 Paint 时没有变黑,当 clearCounter 变为 == 5 时,然后屏幕被清除并再次绘画开始时,背景变为黑色。 pu
我已经花了 6 个小时试图解决这个问题。我终于可以根据需要更改颜色,但我只想保留组合框箭头的默认颜色。 这是代码... 注意:编辑后的代码编译时没有图片显示的背景... import org.apac
我尝试设置 jbutton 的背景颜色,但不起作用?看图片。为什么不起作用?如何修复? 我想要红色背景:-) save_button.setForeground(Color.red);
我是 Android 新手,遇到以下问题: 我想将 TextView 的背景设置为 #333。我用过: TextView title = new TextView(this); ti
JPanel.setBackground方法不执行任何操作(尽管 opaque 属性为 true )如果 super.paintComponent父方法未被调用。 我在这里阅读了很多关于此问题的类似问
我有一个名为 MainUI 的类,它扩展了 JFrame,它具有以下代码: //Constructor public MainUI(){ // components/panels are alread
我最近了解到我可以为 JTable 创建自定义 DefaultTableCellRenderer 类。 但是,我的代码只为整行着色,而不是我想要根据条件着色的特定列/单元格。 如何在我创建的 Defa
我是一名优秀的程序员,十分优秀!