【问题标题】:swing java multiple buttons guiswing java多按钮gui
【发布时间】:2016-04-12 00:32:54
【问题描述】:

我正在尝试添加一些按钮,但它只显示 button3 ,我不知道为什么 我没有发现任何与其他问题重复的问题

如何使这些按钮中的每一个在标签上显示(当有人单击它们时)变量的值? 如果有人可以帮忙,请帮忙

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class gui extends JFrame {

    public gui() {

        initUI();
    }

    private void initUI() {

        JButton quitButton = new JButton("Quit");
        JButton button1 = new JButton("button1");
        JButton button2 = new JButton("button2");
        JButton button3 = new JButton("button3");

        quitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        createLayout(quitButton);
        createLayout(button1);
        createLayout(button2);
        createLayout(button3);

        setTitle("example");
        setSize(500, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                gui ex = new gui();
                ex.setVisible(true);
            }
        });
    }

【问题讨论】:

  • 当您只访问第一个元素时,为什么要使用createLayout(JComponent... arg)
  • @JonnyHenly 我不知道;这是我在网上看到的
  • 每次调用 createLayout 时,都会创建一个新的 GroupLayout 并向其中添加一个组件,因此实际上只有最后一个组件得到了管理。恕我直言 GroupLayout 不是手动管理的好选择,它更适合表单设计人员

标签: java swing jbutton layout-manager grouplayout


【解决方案1】:

如果您不知道并且只想查看所有按钮,您可以这样尝试:

  • 删除你的 createLayout()
  • 将您的 initUI() 方法替换为:

    private void initUI() {
    
        JButton quitButton = new JButton("Quit");
        JButton button1 = new JButton("button1");
        JButton button2 = new JButton("button2");
        JButton button3 = new JButton("button3");
        JPanel panel = new JPanel();
    
        quitButton.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
    
        button1.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
    
        getContentPane().add(panel);
    
        panel.add(quitButton);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
    
        setTitle("example");
        setSize(500, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    

我强烈建议您首先阅读有关摇摆布局的文档: A Visual Guide to Layout Managers

【讨论】:

  • 您好,感谢您的回答。你能告诉我如何制作那个 GUI,所以当用户单击 button1 时,显示一个字符串(可能在标签中)..当它单击 button2 时显示一些其他字符串等...
  • @javAndr,声明JLabel label;全局,然后在button1的actionPerformed-方法的ActionListener中使用label.setText("Button1 clicked");而不是System.exit(0);并且不要忘记将标签添加到面板
猜你喜欢
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
相关资源
最近更新 更多