[英]Java Swing Frame is a tad off?
我有一個簡單的動畫代碼,可以創建一個儀表類型的矩形效果。 我想創建它,使其完全填充面板。 我很近,但它延伸到底部的框架之外。 我想念什么?
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1,2));
frame.setSize(500, 500);
//MouseTest test = new MouseTest();
Test test2 = new Test(frame.getWidth(), frame.getHeight(), frame.getHeight(), 50);
//frame.add(test);
frame.add(test2);
frame.setVisible(true);
}
}
public class Test extends JPanel implements ActionListener, MouseListener{
int y = 0, width, height, dy=0, maxHeight;
int BOTTOM;
Timer timer;
public Test(int width, int height, int BOTTOM, int SPEED){
setBackground(Color.BLUE);
this.width = width;
maxHeight = height;
this.BOTTOM = BOTTOM;
addMouseListener(this);
timer = new Timer(1000/SPEED, this);
timer.start();
}
public void move(){
if(y>=maxHeight){
dy = -1;
}
if(y<=0)
{
dy = 1;
}
y+=dy;
height+=dy;
//System.out.println(y);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(y<=maxHeight/2) {
g.setColor(Color.green);
g.fillRect(0, BOTTOM - y, width, height);
}
if(y>=maxHeight/2 && y<(maxHeight/10)*9){
g.setColor(Color.green);
g.fillRect(0, BOTTOM-(maxHeight/2), width, maxHeight/2);
g.setColor(Color.green);
g.fillRect(0, BOTTOM - y, width, height-(maxHeight/2));
}
if(y>=(maxHeight/10)*9){
g.setColor(Color.green);
g.fillRect(0, BOTTOM-(maxHeight/2), width, maxHeight/2);
g.setColor(Color.green);
g.fillRect(0, BOTTOM-(9*(maxHeight/10)), width, (4*maxHeight)/10);
g.setColor(Color.green);
g.fillRect(0, BOTTOM - y, width, height-(9*(maxHeight)/10));
}
for(int i = 1; i<6; i++)
g.fillRect(0, BOTTOM - (i*(maxHeight/5)), width, 10);
System.out.println(y);
}
JPanel
getPreferredSize
,這將幫助布局管理器確定布局組件的最佳方法 JFrame#pack
將框架邊框包裹在視圖周圍 getWidth
和getHeight
獲取面板的實際大小。 每當您需要了解這些值時都應該這樣做,不要長時間存儲它們,因為這些值可能會發生變化 這是基於您的代碼的基本示例...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test1 {
public static void main(String[] args) {
new Test1();
}
public Test1() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private int yPos;
private int dy = 1;
private int bottom = 50;
public TestPane() {
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
move();
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
public void move() {
if (yPos >= getHeight()) {
dy = -1;
}
if (yPos <= 0) {
dy = 1;
}
yPos += dy;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int maxHeight = getHeight();
int width = getWidth();
int height = maxHeight;
if (yPos <= maxHeight / 2) {
g.setColor(Color.green);
g.fillRect(0, bottom - yPos, width, height);
}
if (yPos >= maxHeight / 2 && yPos < (maxHeight / 10) * 9) {
g.setColor(Color.green);
g.fillRect(0, bottom - (maxHeight / 2), width, maxHeight / 2);
g.setColor(Color.green);
g.fillRect(0, bottom - yPos, width, height - (maxHeight / 2));
}
if (yPos >= (maxHeight / 10) * 9) {
g.setColor(Color.green);
g.fillRect(0, bottom - (maxHeight / 2), width, maxHeight / 2);
g.setColor(Color.green);
g.fillRect(0, bottom - (9 * (maxHeight / 10)), width, (4 * maxHeight) / 10);
g.setColor(Color.green);
g.fillRect(0, bottom - yPos, width, height - (9 * (maxHeight) / 10));
}
for (int i = 1; i < 6; i++) {
g.fillRect(0, bottom - (i * (maxHeight / 5)), width, 10);
}
}
}
}
另一種方法是使用pack()。 它將框架的大小設置為框架內容的大小。 在您的代碼中,它看起來像這樣:
frame.add(test2);
frame.pack();
如果以這種方式進行操作,則不需要frame.setSize()調用。 您可以添加.add()或添加以下內容來添加面板:
frame.setContentPane(test2);
因此,我結合了您的評論並使其正常工作。 這是有效的代碼。
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1,2));
Insets inset = frame.getInsets();
Test test2 = new Test(200-(inset.right + inset.left), 200 -(inset.top + inset.bottom), 200, 50);
frame.setContentPane(test2);
frame.pack();
frame.setVisible(true);
}
}
public class Test extends JPanel implements ActionListener, MouseListener{
int y = 0, width, height, dy=0, maxHeight;
int BOTTOM;
Timer timer;
Dimension size;
Insets inset;
public Test(int width, int height, int BOTTOM, int SPEED){
size = new Dimension(width, height);
setBackground(Color.BLUE);
setPreferredSize(size);
inset = this.getInsets();
this.width = width-(inset.right+inset.left);
maxHeight = height;
this.BOTTOM = BOTTOM - (inset.bottom+inset.top);
addMouseListener(this);
timer = new Timer(1000/SPEED, this);
timer.start();
}
public void move(){
if(y>=maxHeight){
dy = -1;
}
if(y<=0)
{
dy = 1;
}
y+=dy;
height+=dy;
//System.out.println(y);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(y<=maxHeight/2) {
g.setColor(Color.green);
g.fillRect(0, BOTTOM - y, width, height);
}
if(y>=maxHeight/2 && y<(maxHeight/10)*9){
g.setColor(Color.green);
g.fillRect(0, BOTTOM - (maxHeight / 2), width, maxHeight / 2);
g.setColor(Color.green);
g.fillRect(0, BOTTOM - y, width, height-(maxHeight/2));
}
if(y>=(maxHeight/10)*9){
g.setColor(Color.green);
g.fillRect(0, BOTTOM-(maxHeight/2), width, maxHeight/2);
g.setColor(Color.green);
g.fillRect(0, BOTTOM-(9*(maxHeight/10)), width, (4*maxHeight)/10);
g.setColor(Color.green);
g.fillRect(0, BOTTOM - y, width, height-(9*(maxHeight)/10));
}
g.setColor(Color.white);
for(int i = 1; i<6; i++)
g.fillRect(0, BOTTOM - (i*(maxHeight/5)), width, 5);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.