单击Jbutton时,按钮移动或标签文本更改。我只想表演两者

时间:2013-01-06 03:44:21

标签: java swing layout jlabel grid-layout

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

2 个答案:

答案 0 :(得分:2)

您正在使用GridLayout,因此您不能随意移动组件。我建议你阅读Laying Out Components Within a Container上的Swing教程,了解布局管理的工作原理。

答案 1 :(得分:2)

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

这种没有GUI效果的行为也可能是invokeLater中遗失actionPerformed造成的。

您为使用过的对象创建了类以设置特定属性。这是一种古怪的浪费,因此我重写了一点。

如果您调整窗口大小,您将看到GridLayout管理器再次布局,以便JButton出现在其原始位置。您可以使用setLayout(null)setLocationpublic 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); } }); } } 一起使用绝对位置。

{{1}}

P.S。我见过相当糟糕的代码。这实际上是整齐的代码,只有多余的类。