gpt4 book ai didi

java - JPanel&JScrollPane及错误渲染

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:52:44 24 4
gpt4 key购买 nike

我的英语不好,但我会尽力解释。我有个问题。在 JPanel,我画了一条线....它们应该超出对象 JPanel 和 JScrollPane 的区域,理论上应该显示整个面板,但它没有更新,也不显示为面板范围发布的所有行.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Console;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestFrame extends JFrame {

static int speed = 10;
static int floor = 22;
public static void createGUI() {
JFrame frame = new JFrame("Test frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final Font font = new Font("Verdana", Font.PLAIN, 25);

JPanel butPanel = new JPanel();

JButton biginButton = new JButton("start");
biginButton.setFont(font);
biginButton.setFocusable(false);
butPanel.add(biginButton);

JButton remButton = new JButton("repaint");
remButton.setFont(font);
remButton.setFocusable(false);
butPanel.add(remButton);

final JPanel labPanel = new JPanel();
final JScrollPane scrollPane = new JScrollPane(labPanel);
labPanel.setLayout(new BoxLayout(labPanel, BoxLayout.Y_AXIS));

biginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//labPanel.getGraphics().drawString("Ololo", 50, 50);
int floH = 100;
for(int i = 0; i < floor; i++){
labPanel.getGraphics().drawLine(0, i*floH, 300, i*floH);
labPanel.getGraphics().setColor(Color.RED);
scrollPane.revalidate();
}
}
});

remButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
labPanel.repaint();
scrollPane.revalidate();
}
});

frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(butPanel, BorderLayout.NORTH);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setPreferredSize(new Dimension(screenSize.width-10, screenSize.height-10));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
createGUI();
}
});
}
}

最佳答案

我猜你正在尝试绘制到 JPanel,然后期望 JPanel 应该变大以容纳你的绘图,这不仅对你不起作用,而且如果你调用你会丢失绘图JPanel 上的 repaint()。

如果是,那么,

  • 仅仅因为您绘制超出了 JPanel 或其他 JComponent 的大小不会自动使该组件变大,因为该组件本身没有“知识”您正在绘制超出其边界。如果你想调整组件的大小,你必须自己做。
  • 最好通过覆盖其 getPreferredSize() 方法让 JPanel 设置自己的大小。这将防止其他代码将其大小重置为其他值。
  • 您不想通过调用 getGraphics() 来获取组件的图形。如此获得的 Graphics 对象持续时间很短,一旦下次调用 paint(...) 或 paintComponent() 时,绘制的任何内容都会消失。可以在 BufferedImage 上调用 getGraphics(),但不能在 Component 或 JComponent 上调用(除非您有充分的理由这样做并且知道如何防止 Graphics 对象变为 null)。
  • 覆盖 JPanel 或其他一些 JComponent 并在该组件的 paintComponent(...) 覆盖方法中绘制。如果您使用的是 BufferedImage,则可以在此 paintComponent 方法中绘制 BufferedImage。

例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.*;

@SuppressWarnings("serial")
public class TestDrawing extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 600;
private DrawingPanel drawingPanel = new DrawingPanel();

public TestDrawing() {
JPanel northPanel = new JPanel();
northPanel.add(new JButton(new AbstractAction("Draw Lines") {

@Override
public void actionPerformed(ActionEvent arg0) {
drawingPanel.drawLines();
drawingPanel.repaint();
}
}));

setLayout(new BorderLayout());
add(new JScrollPane(drawingPanel));
add(northPanel, BorderLayout.PAGE_START);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

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

JFrame frame = new JFrame("TestDrawing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class DrawingPanel extends JPanel {
private static final int BI_W = 500;
private static final int FLOOR = 22;
private static final int FLO_H = 100;
private BufferedImage img = new BufferedImage(BI_W, FLO_H * FLOOR, BufferedImage.TYPE_INT_ARGB);

public DrawingPanel() {

}

public void drawLines() {

Graphics g = img.getGraphics();
g.setColor(Color.black);
for (int i = 0; i < FLOOR; i++) {
g.drawLine(0, i * FLO_H, 300, i * FLO_H);
g.setColor(Color.RED);
}
g.dispose();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(BI_W, FLO_H * FLOOR);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
}
}

关于java - JPanel&JScrollPane及错误渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9762171/

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