我有这个代码。调整JFrame的大小时,需要使这个正方形灵活。正方形的大小应按百分比更改。 this.getWidth(),this.getHeight()返回(0,0); super.getWidth(),super.getHeight()返回(0,0); getWidth(),getHeight()返回(0,0); 我找不到任何范例。我无话可说了。非常感谢你!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Parker {
public static void main(String[] args) {
new Parker();
}
public Parker() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ControlPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ControlPane extends JPanel {
private JSlider slider;
private DrawPane myPanel;
public ControlPane() {
setLayout(new BorderLayout());
myPanel = new DrawPane();
myPanel.setBackground(Color.WHITE);
Dimension dim = myPanel.getSize();
slider = new JSlider(SwingConstants.HORIZONTAL,0,100,20);
slider.setMajorTickSpacing(20);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setValue(0);
slider.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Slider"),
BorderFactory.createEmptyBorder(15,10,15,10)
)
);
slider.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
myPanel.setScale(slider.getValue());
}
}
);
add(slider,BorderLayout.SOUTH);
add(myPanel,BorderLayout.CENTER);
}
}
public class DrawPane extends JPanel {
double scale = 1;
double angle = 0;
//here i tried different methods to find current size of frame, you can
//set some int values like 10 or 100 to watch that everything works
//properly
int rectWidth = ;
int rectHeight = ;
public void componentResized(ComponentEvent e) {
Dimension newSize = e.getComponent().getBounds().getSize();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int originX = getWidth() / 2;
int originY = getHeight() / 2;
int xOffset = -(rectWidth / 2);
int yOffset = -(rectHeight / 2);
g.setColor(Color.BLACK);
Graphics2D g2d = (Graphics2D) g.create();
g2d.translate(originX, originY);
g2d.scale(scale, scale);
g2d.fill(new Rectangle2D.Double(xOffset, yOffset, rectWidth, rectHeight));
g2d.dispose();
g.setColor(Color.RED);
g.drawRect(originX + xOffset, originY + yOffset, rectWidth, rectWidth);
}
public void setScale(int scale) {
this.scale = (scale / 100d);
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
}
}
答案 0 :(得分:0)
您应该将ComponentListener
注册到ControlPanel
的{{1}}上,这是您的JFrame
的主要内容窗格,用于监听调整大小事件(还监听初始调整大小),并使{{1} }调整大小,
尝试一下:
rectangle