有时setBounds()可以工作,但只能在actionlistener中工作。
我的代码:
- 不工作
public panel() {
initComponents();
setBounds(100,100,105,105);
setMaximumSize(new Dimension(100,100));
}
- 工作
private void btnBestellingItemToevActionPerformed(java.awt.event.ActionEvent evt) {
setBounds(100,100,105,105);
setMaximumSize(new Dimension(100,100));
}
-layout manager:
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnBestellingItemToev, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(txtWat, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtTafelNr, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)))
.addGap(0, 131, Short.MAX_VALUE))
);
任何人都可以帮助我,所以它也可以在构造函数中工作吗?
答案 0 :(得分:0)
正如我所说,你遇到的问题是你试图移动/调整大小的容器有一个布局管理器。
布局管理器负责确定其中所有组件的位置和大小。
你有两个选择。编写自己的布局管理器,执行您希望它执行的操作。这是一个相当复杂的过程,但通常是更好的选择,或者没有布局管理器。
就个人而言,在做这样的事情时,我会将结果直接绘制到面板上,但那就是我......
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class BouncyBall {
public static void main(String[] args) {
new BouncyBall();
}
public BouncyBall() {
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 BallCourt());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BallCourt extends JPanel {
private JLabel beachBall;
private int delta = 4;
public BallCourt() {
setLayout(null);
beachBall = new JLabel();
try {
beachBall.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/BeachBallOfDoom.png"))));
} catch (IOException ex) {
ex.printStackTrace();
}
beachBall.setBounds(0, 100 - 16, 32, 32);
add(beachBall);
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int x = beachBall.getX() + delta;
if (x + beachBall.getWidth() > getWidth()) {
delta *= -1;
x = (getWidth() - beachBall.getWidth()) + delta;
} else if (x < 0) {
delta *= -1;
x = delta;
}
beachBall.setLocation(x, beachBall.getY());
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}