java中如何让JPanel面版进行缩放大小;
图图凌乱给谁看
2020-02-21 08:58:59
我现在一个个面版,我想让它缩放,且上面的子组件着变换大小,我做了一个例子,不过只能 将面版以及它的子组件大的UI缩放,而无法将组件真正的缩放,下面是我做的例子, package print; import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.awt.geom.Line2D; import javax.swing.*; public class PrintTest2 extends JFrame { AffineTransform trans;//用来控制面版缩放的对象; public PrintTest2(){ setSize(800, 600); PanelZzg panelzzg=new PanelZzg(); panelzzg.setPreferredSize(new Dimension(900,700)); JScrollPane jscrollpane=new JScrollPane(panelzzg); add(jscrollpane,BorderLayout.CENTER); setVisible(true); } public static void main(String[] args){ PrintTest2 a=new PrintTest2(); a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class PanelZzg extends JPanel{ JButton btn; JLabel lab; JToggleButton a; AbstractButton b; public PanelZzg(){ setLayout(null); a=new JToggleButton("kdkfj"); setBackground(Color.BLUE); btn=new JButton("可是可靠 可是扩大 "); btn.setBounds(120,60,120,60); lab=new JLabel("skdfjklsdjflsksdfs"); lab.setOpaque(true); lab.setBounds(200,200,100,100); a.setBounds(200,400,100,60); add(a); add(btn); add(lab); setSize(800,600); trans=new AffineTransform(); trans.scale(0.5, 0.5);//设置缩放的陪数; setVisible(true); } public void paint(Graphics g){ Graphics2D g2=(Graphics2D)g; g2.setTransform(trans);//设置画笔坐标系的缩放 super.paintComponent(g2); super.paintChildren(g2); } } }
分享到