【问题标题】:Java JButton shows in one panel but not the otherJava JButton 显示在一个面板中,但不在另一个面板中
【发布时间】:2016-03-25 09:35:36
【问题描述】:

我正在为一个人造销售系统构建一个非常基本的 GUI,我试图显示两个屏幕(“销售”和“主页”),两个屏幕都带有转到另一个 jpanel 的按钮,还有一个“退出”按钮关闭应用程序。

我正在使用以下代码,我想我可以对其他按钮使用与“退出”按钮相同的代码,但是退出仅显示在销售页面上,而不是主页上。

import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;

public class PHPSRSFrame implements ActionListener
{
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final int BUTTON_WIDTH = 115;
    public static final int BUTTON_HEIGHT = 30;
    public static final int HEADING_WIDTH = 350;
    public static final int HEADING_HEIGHT = 35;

    private JFrame frame;
    private JPanel homePanel, salesPanel;
    private JButton salesButton, homeButton, quitButton;

    public PHPSRSFrame()
    {
        // Create the frame for the program
        frame = new JFrame("PHP Sales Reporting System");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create each panel (page), use null layout for absolute locations
        homePanel = new JPanel();
        homePanel.setLayout(null);
        salesPanel = new JPanel();
        salesPanel.setLayout(null);

        // Create Homepage heading
        JLabel lblPhpSalesReporting = new JLabel("Welcome");
        lblPhpSalesReporting.setFont(new Font("Arial Black", Font.BOLD, 24));
        lblPhpSalesReporting.setBounds(WIDTH/2-HEADING_WIDTH/2, 50, HEADING_WIDTH, HEADING_HEIGHT);

        // Create Sales heading
        JLabel lblSales = new JLabel("Sales");
        lblSales.setFont(new Font("Arial Black", Font.BOLD, 24));
        lblSales.setBounds(WIDTH/2-100/2, 50, 100, HEADING_HEIGHT);

        // Create button to go to sales screen
        salesButton = new JButton("Sales");
        salesButton.setBounds(WIDTH/2-BUTTON_WIDTH/2, 150, BUTTON_WIDTH, BUTTON_HEIGHT);
        salesButton.addActionListener(this);

        // Create button to return to home screen
        homeButton = new JButton("Home");
        homeButton.setBounds(20, 20, BUTTON_WIDTH, BUTTON_HEIGHT);
        homeButton.addActionListener(this);

        // Create button to quit the whole program
        quitButton = new JButton("Quit");
        quitButton.setBounds(650, 500, BUTTON_WIDTH, BUTTON_HEIGHT);
        quitButton.addActionListener(this);

        // Add components to Homepage
        homePanel.add(lblPhpSalesReporting);
        homePanel.add(salesButton);
        homePanel.add(quitButton);

        // Add components to Sales page
        salesPanel.add(homeButton);
        salesPanel.add(lblSales);
        salesPanel.add(quitButton);

        frame.setContentPane(homePanel);
        frame.setSize(WIDTH, HEIGHT);
        frame.setVisible(true);
    }


    public void actionPerformed(ActionEvent ae)
    {
        JButton button = (JButton) ae.getSource();
        // Go to sales page
        if (button == salesButton)
        {   
            frame.remove(homePanel);
            frame.setContentPane(salesPanel);
        }
        // Go to homepage
        else if (button  == homeButton)
        {
            frame.remove(salesPanel);
            frame.setContentPane(homePanel);
        }
    // Quit the application
    else if (button == quitButton) {
        System.exit(0);
    }
    // Redo the frame
    frame.revalidate();
    frame.repaint();
}   

    public static void main(String[] args)
    {   
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new PHPSRSFrame();
            }
        });
    }
}

谁能告诉我我错过了什么/做错了什么导致主页按钮只出现在销售页面上?

谢谢。

【问题讨论】:

    标签: java swing jframe jpanel jbutton


    【解决方案1】:

    您根本无法将相同的JButton 添加到两个不同的父容器中。这将导致只有最后一个 add 调用有效。 您需要创建两个可以共享同一个 ActionListener 的不同 JButton。

    或者在切换JPanels时,您可以将按钮从消失中移除并将其添加到出现的框架中。

    homePanel.add(quitButton);  // add to home panel
    [...]
    //salesPanel.add(quitButton); // don't add to sales panel
    [...]
    public void actionPerformed(ActionEvent ae){
        [...]
        if (button == salesButton)
        {   
            homePanel.remove(quitButton);
            salesPanel.add(quitButton);
            [...]
        }
        else if (button  == homeButton)
        {
            salesPanel.remove(quitButton);
            homePanel.add(quitButton);
            [...]
        }
        [...]
    }
    

    【讨论】:

    • 非常感谢。我为每个页面创建了两个不同的退出按钮,现在它可以按预期工作了。
    猜你喜欢
    • 2010-10-25
    • 1970-01-01
    • 2012-03-17
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多