有时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))
    );
.

任何人都可以帮助我,所以它在构造函数中也有效吗?

有帮助吗?

解决方案

正如我所说,你拥有的问题是你尝试移动/大小的组件的容器有一个布局管理器。

布局管理器负责确定其中内部所有组件的位置和大小。

你有两种选择。写下自己的布局经理,可以做你想要的事。这是合理复杂的过程,但通常是更好的选择或没有布局经理。

亲自,当做这样的事情时,我将结果直接涂给面板,但这是我...

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);
        }

    }

}
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top