将中心 JButton 更改为按下其他按钮时更改的面板

Changing center JButton to a panel that changes when other buttons are pressed

我是新手所以这个问题可能看起来非常明显...

我正在尝试更改 Java 中的边框布局,以便中心按钮成为 panel/Jtextarea。一个面板在按下其他面板时做出反应,说 "Going *" * 是方向。然后,当我按下一个新按钮时,它会擦除​​旧按钮并更改为 "Going **" ** 作为新方向。我已经包含了当前代码和我正在寻找的图片:)

/*
 * BorderLayoutDemo.java
 *
 */
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;


public class BorderLayoutDemo {
public static boolean RIGHT_TO_LEFT = false;

public static void addComponentsToPane(Container pane) {

    if (!(pane.getLayout() instanceof BorderLayout)) {
        pane.add(new JLabel("Container doesn't use BorderLayout!"));
        return;
    }

    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(
                java.awt.ComponentOrientation.RIGHT_TO_LEFT);
    }


    JButton button = new JButton("Up");
    pane.add(button, BorderLayout.PAGE_START);

    //Make the center component 400x400
    //typical usage of BorderLayout.

    button = new JButton("Going...");
    pane.setPreferredSize(new Dimension(400, 400));
    pane.add(button, BorderLayout.CENTER);

    button = new JButton("Left");
    pane.add(button, BorderLayout.LINE_START);

    button = new JButton("Down");
    pane.add(button, BorderLayout.PAGE_END);

    button = new JButton("Right");
    pane.add(button, BorderLayout.LINE_END);
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
private static void createAndShowGUI() {

    //Create and set up the window.
    JFrame frame = new JFrame("BorderLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());
    //Use the content pane's default BorderLayout. No need for
    //setLayout(new BorderLayout());
    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    /* Use an appropriate Look and Feel */
    try {
        //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    /* Turn off metal's use bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);

    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}

你需要给中间的JButton一个唯一的字段名。

goingButton = new JButton("Going...");
pane.add(goingButton, BorderLayout.CENTER);

然后您需要为其他更改goingButton 文本的按钮编写一个动作侦听器。下面介绍如何设置 JButton 的文本。

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            goingButton.setText("Going up");
        }       
    });

编辑后只为您提供修改后的代码。如果其他人为您工作,您将学不到任何东西。

这是 BorderLayoutDemo 的屏幕截图。

这是为您格式化和修改的代码:

package com.ggl.testing;

/*
 * BorderLayoutDemo.java
 *
 */
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BorderLayoutDemo {
    private static final boolean RIGHT_TO_LEFT = false;

    private JButton goingButton;

    public void addComponentsToPane(Container pane) {

        if (!(pane.getLayout() instanceof BorderLayout)) {
            pane.add(new JLabel("Container doesn't use BorderLayout!"));
            return;
        }

        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
        }

        pane.setPreferredSize(new Dimension(400, 400));

        JButton button = new JButton("Up");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                goingButton.setText("Going up");
            }
        });
        pane.add(button, BorderLayout.PAGE_START);

        // Make the center component 400x400
        // typical usage of BorderLayout.

        goingButton = new JButton("Going...");
        goingButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                goingButton.setText("Going crazy");
            }
        });
        pane.add(goingButton, BorderLayout.CENTER);

        button = new JButton("Left");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                goingButton.setText("Going left");
            }
        });
        pane.add(button, BorderLayout.LINE_START);

        button = new JButton("Down");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                goingButton.setText("Going down");
            }
        });
        pane.add(button, BorderLayout.PAGE_END);

        button = new JButton("Right");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                goingButton.setText("Going right");
            }
        });
        pane.add(button, BorderLayout.LINE_END);
    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event dispatch thread.
     */
    private void createAndShowGUI() {

        // Create and set up the window.
        JFrame frame = new JFrame("BorderLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Set up the content pane.
        addComponentsToPane(frame.getContentPane());
        // Use the content pane's default BorderLayout. No need for
        // setLayout(new BorderLayout());
        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);

        // Schedule a job for the event dispatch thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new BorderLayoutDemo().createAndShowGUI();
            }
        });
    }
}