gpt4 book ai didi

java - 缩放面板但滚动条不起作用

转载 作者:行者123 更新时间:2023-12-01 11:16:22 26 4
gpt4 key购买 nike

我创建了一个具有放大和缩小功能的面板,但滚动条不起作用,我的意思是当我放大时,我无法滚动查看整个面板。
这是我的代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class PanelWithZoom extends JPanel{

private JButton btnZoomIn;
private JButton btnZoomOut;
private double scale = 1;
private JPanel innerPanel;
private JPanel outerPanel;
public PanelWithZoom() {
setLayout(new BorderLayout(0, 0));

JPanel panel = new JPanel();
add(panel, BorderLayout.NORTH);

btnZoomIn = new JButton("zoom in");
btnZoomIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
scale = scale + 0.1;
outerPanel.repaint();
outerPanel.revalidate();
}
});
panel.add(btnZoomIn);

btnZoomOut = new JButton("zoom out");
btnZoomOut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scale = scale - 0.1;
outerPanel.repaint();
outerPanel.revalidate();
}
});
panel.add(btnZoomOut);

JPanel panel_1 = new JPanel();
add(panel_1, BorderLayout.WEST);

JPanel panel_2 = new JPanel();
add(panel_2, BorderLayout.SOUTH);

JPanel panel_3 = new JPanel();
add(panel_3, BorderLayout.EAST);

outerPanel = new JPanel(){
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;
int w = (int) getWidth();
int h = (int) getHeight();
// Translate used to make sure scale is centered
g2.translate(w/2, h/2);
g2.scale(scale, scale);
g2.translate(-w/2, -h/2);
}


};
JScrollPane scroll = new JScrollPane(outerPanel);
add(scroll, BorderLayout.CENTER);

innerPanel = new JPanel(){
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.white);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(Color.black);
g2.drawString("String for test", 10, 50);
g2.drawRect(0, 0, getWidth()-1, getHeight()-1);
setPreferredSize(new Dimension(595, 842));
}

};

outerPanel.add(innerPanel);
}


public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setContentPane(new PanelWithZoom());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}

}

你可以测试它来理解我的意思。

最佳答案

重写组件的getPreferredSize方法,根据组件的scale和原始大小返回组件的大小。

因为您想要对子组件应用缩放,所以您可能需要重写 paint 而不是 paintComponent

outerPanel = new JPanel(){
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.scale(scale, scale);
super.paint(g);
g2.dispose()
}
}

但是,Swing 组件可以独立于其父容器进行绘制,这意味着在绘制子组件时可能不会调用 paint 方法...

这样做的问题是,它不会缩放鼠标事件(或一堆其他属性)。

随着问题列表不断增加,更好的解决方案是使用 JXLayer 之类的东西,如 here 所示。

旁注:

  • 使用Graphics#create复制Graphics上下文和Graphics#dispose的属性,在处理平移/转换/时更安全/渲染提示和一堆其他属性,因为对副本的任何更改都不会影响原始
  • 不要从任何绘制方法中调用任何可能直接或间接导致触发重绘事件的方法,您最终会陷入一个令人讨厌的永无休止的循环,最终会消耗您的 CPU

Graphics#create 和 #Dispose 示例...

outerPanel = new JPanel(){
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g.create();
int w = (int) getWidth();
int h = (int) getHeight();
// Translate used to make sure scale is centered
g2.translate(w/2, h/2);
g2.scale(scale, scale);
g2.translate(-w/2, -h/2);
g2.dispose();
}
}

关于java - 缩放面板但滚动条不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31786592/

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