gpt4 book ai didi

java - 在默认 JFrame 面板上使用 paintComponent()

转载 作者:行者123 更新时间:2023-11-29 04:48:07 24 4
gpt4 key购买 nike

我想向 JFrame 添加渐变背景。在 JFrame 中,我没有创建 JPanel,而是使用了在创建 JFrame 时自动添加的 JPanel。我在 JPanel 实例上找到的所有在线调用 paintComponent() 的示例,但如前所述,在我的例子中我没有专门实例化一个。我知道 paintComponent() 需要在组件上调用,因此不能在 JFrame 上调用,那么我是否需要创建并添加 JPanel 才能实现此目的?我过去曾成功地这样做过,但我正在努力使我的代码尽可能精简,所以如果不需要,我不想创建第二个 JPanel。这是我的代码:

public class Main extends JFrame {

private static final long serialVersionUID = 1L;

JPanel pane;

public Main() {

int WIDTH = 700, HEIGHT = 400;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.GRAY);
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);

pane = (JPanel) getContentPane();
pane.setOpaque(false);
pane.setLayout(new BorderLayout());
pane.setBorder(new EmptyBorder(20, 20, 20, 20));

// SOME OTHER STUFF
}
protected void paintComponent(Graphics grphcs) {
Graphics2D g2d = (Graphics2D) grphcs;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0, new Color(255, 195, 225), 0, getHeight(), new Color(139, 207, 236));
g2d.setPaint(gp);
paintComponent(grphcs);
}
}

我将不胜感激。

最佳答案

so do I need to create and add a JPanel in order to achieve this?

是的。

but I'm trying to keep my code as streamlines as possible, so I don't want to create a second JPanel if I don't need to.

这里不会浪费,因为简化上述代码的方法是去掉 extends JFrame。没有必要这样做,并且有很多理由不这样做。让类扩展 JFrame 是在逼迫自己创建和显示 JFrame,而这通常需要更大的灵 active 。事实上,我敢打赌,我创建的和看到的大多数 Swing GUI 代码都扩展 JFrame,而且实际上您很少想这样做这。更常见的是,您的 GUI 类将用于创建 JPanel,然后可以将其放入 JFrames 或 JDialogs,或 JTabbedPanes,或通过 CardLayouts 在任何需要的地方进行交换。这将大大增加您的 GUI 编码的灵 active 。

例如,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class MainPanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final int PREF_W = 700;
private static final int PREF_H = 400;
private static final Color GRAD_COLOR_1 = new Color(255, 195, 225);
private static final Color GRAD_COLOR_2 = new Color(139, 207, 236);

public MainPanel() {
setBackground(Color.GRAY);
setSize(WIDTH, HEIGHT);
setLayout(new BorderLayout());
setBorder(new EmptyBorder(20, 20, 20, 20));

// SOME OTHER STUFF
}

// to set the preferred size of the JPanel/GUI
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs); // ** don't forget this.
Graphics2D g2d = (Graphics2D) grphcs;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0, GRAD_COLOR_1, 0, getHeight(), GRAD_COLOR_2);
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
}

private static void createAndShowGui() {
MainPanel mainPanel = new MainPanel();

// create the JFrame when/if needed
JFrame frame = new JFrame("Main");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}

But doesn't it still create unneeded JPanels?

那是在担心错误的事情。创建了一个 JPanel,即 contentPane,这不会对您的程序或您的资源使用产生真正的不利影响。

My actual class extends JPanel, so when I instantiate that there is one created, plus the default panel which was created when we created the JFrame. Isn't that kind of the same as what I had before, I mean I could have just created a second JPanel and added it to my main class (JFrame) and called paintComponent() on it.

如上所述——不要小题大做,也不要过早优化。如果您担心程序中的资源滥用,请分析它,然后解决具有实际影响的问题。

What are the advantages of extending JPanel instead of JFrame

我已经提到了很多 - 我上面的回答特别令人困惑吗?

关于java - 在默认 JFrame 面板上使用 paintComponent(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36389928/

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