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

时间:2015-02-24 16:22:40

标签: java swing actionlistener border-layout

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

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

Swing Layout

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

}

1 个答案:

答案 0 :(得分:1)

您需要为中心的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的屏幕截图。

Border Layout Demo

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

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