0
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JavaGUI extends JPanel {

    private Control control = new Control();
    private Keys keys = new Keys("Original starting value.");

    public JavaGUI() {
        this.setLayout(new GridLayout(0, 1));
        this.add(keys);
        this.add(control);

        private class Control extends JPanel {

            public Control() {
                this.add(new JButton(new AbstractAction("Update") {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Command: " + e.getActionCommand());
                        keys.string = String.valueOf(System.nanoTime());
                        //keys.label.setText(keys.string);  //remove comments cause button move
                        JButton j = (JButton) e.getSource();
                        j.setLocation(j.getX() + 10, j.getY() + 10);
                    }
                }));
            }
        }


        private class Keys extends JPanel {

            private String string;
            private JLabel label = new JLabel();

            public Keys(String s) {
                this.string = s;
                label.setText(s);
                this.add(label);
            }
        }


        private void display() {
            JFrame f = new JFrame("JavaGUI");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }

        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    new JavaGUI().display();
                }
            });
        }
    }
4

2 回答 2

2

您正在使用 GridLayout,因此您不能随意移动组件。我建议你阅读关于在容器内布局组件的 Swing 教程,以了解布局管理的工作原理。

于 2013-01-06T05:37:10.960 回答
2

我会全力以赴。错误是,您设置了 String string,而不是 JLabel label

这种没有 GUI 效果的行为也可能是invokeLater由于actionPerformed.

您为使用的对象创建了类以设置特定属性。那是官僚主义的浪费,因此我将其重写了一些。

如果调整窗口大小,您将看到 GridLayout 管理器再次布局,因此 JButton 出现在其原始位置。您可以使用绝对定位翼和setLayout(null)使用单个setLocations。

public class JavaGUI extends JPanel {

    private JPanel control;
    private JLabel keys;

    public JavaGUI() {
        setLayout(new GridLayout(0, 1));

        keys = new JLabel("Original starting value.");
        keys.setHorizontalAlignment(SwingConstants.CENTER);
        add(keys);

        control = new JPanel();
        control.setPreferredSize(new Dimension(200, 100));
        control.setMinimumSize(new Dimension(200, 100));
        control.setLayout(null);
        JButton button = new JButton(new AbstractAction("Update") {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Command: " + e.getActionCommand());
                keys.setText(String.valueOf(System.nanoTime()));
                //keys.label.setText(keys.string);  //remove comments cause button move
                JButton j = (JButton) e.getSource();
                j.setLocation(j.getX() + 10, j.getY() + 10);
            }
        });
        button.setBounds(10, 10, 90, 24);
        control.add(button);
        add(control);
    }

    public static void main(String[] args) {
        final JFrame f = new JFrame("JavaGUI");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JavaGUI());
        f.pack();
        f.setLocationRelativeTo(null);

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                f.setVisible(true);
            }
        });
    }
}

PS我见过相当糟糕的代码。这实际上是简洁的代码,只有多余的类。

于 2013-01-06T06:22:01.700 回答