繁体   English   中英

当 JComboBox 选择更改时如何将 JComponent 添加到 Jpanel 上?

[英]How to add JComponent onto Jpanel when JComboBox selection is changed?

每当我切换daysOfTheWeek comboBox 的选择时,我都试图将 JComponent 添加到 Jpanel 上。但是,它似乎不起作用,但只有当我将它放在actionPerformed(ActionEvent e)方法之外时才起作用。 我究竟做错了什么? 这是我的简化代码:

public class App extends JFrame {

public static final int WIDTH = 1900;
public static final int HEIGHT = 1000;

JPanel scheduleArea;

private String[] days = {"Monday", "Tuesday"};


public App() {
    super("Schedule");
    scheduleArea = new JPanel();
    initializeGraphics();
}

public void initializeGraphics() {
    setMinimumSize(new Dimension(WIDTH, HEIGHT));
    getContentPane().setBackground(Color.LIGHT_GRAY);
    getContentPane().setLayout(null);

    createScheduleArea();
    setVisible(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void createScheduleArea() {

    JPanel schedulePanel = new JPanel();

    schedulePanel.setBounds(850,40,990,870);
    schedulePanel.setLayout(null);

    scheduleArea.setBounds(25,105,940,740);
    scheduleArea.setBackground(new java.awt.Color(197,218,221));

    JComboBox daysOfTheWeek = new JComboBox(days);

    daysOfTheWeek.setBounds(750,30,200,45);

    schedulePanel.add(daysOfTheWeek);

    daysOfTheWeek.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String selectedDay = (String) daysOfTheWeek.getSelectedItem();

            switch (selectedDay) {
                case "Monday":
                    scheduleArea.add(new JLabel("Monday")); // JLabel not added
                    break;
                case "Tuesday":
                    scheduleArea.add(new JLabel("Tuesday"));
                    break;
                default:
                    System.out.println("Please select");
            }

        }
    });

    schedulePanel.add(scheduleArea);
    add(schedulePanel);
}

}

Oracle 有一个有用的教程,使用 Swing 创建 GUI 跳过学习 Swing 和 NetBeans IDE 部分。 请密切注意在容器内布置组件部分。

通常,您设计一个 Swing GUI,以便一次创建所有 Swing 组件。 然后,您更新 Swing 组件的值。

通常,您从内到外创建一个 Swing GUI。 您定义 Swing 组件和JPanels ,并让JFrame自行调整大小。

这是我根据您的 GUI 想出的 GUI。

日程

我使用JFrame 您应该扩展JFrame或任何 Java class 的唯一时间是当您想要覆盖 class 方法中的一个或多个时。

我创建了两个单独的JPanels ,一个用于JComboBox ,一个用于JTextArea 我在组合框面板中添加了一个JButton ,这样您就可以在同一天多次使用 select。 我使用了JTextArea ,因此我可以定义一个 Swing 组件和 append 文本。

这是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class SchedulingApplication implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SchedulingApplication());
    }

    private JTextArea textArea;

    @Override
    public void run() {
        JFrame frame = new JFrame("Schedule");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createComboBoxPanel(), BorderLayout.NORTH);
        frame.add(createSchedulePanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createComboBoxPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 5, 5));
        panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));

        String[] days = { "Monday", "Tuesday" };

        JComboBox<String> daysOfTheWeek = new JComboBox<>(days);
        panel.add(daysOfTheWeek);

        JButton button = new JButton("Select");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedDay = (String) daysOfTheWeek.getSelectedItem();
                switch (selectedDay) {
                case "Monday":
                case "Tuesday":
                    textArea.append(selectedDay);
                    textArea.append(System.lineSeparator());
                    break;
                default:
                    System.out.println("Please select");
                }
            }
        });
        panel.add(button);

        return panel;
    }

    private JPanel createSchedulePanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));

        textArea = new JTextArea(10, 40);
        textArea.setBackground(new Color(197, 218, 221));
        JScrollPane scrollPane = new JScrollPane(textArea);
        panel.add(scrollPane);

        return panel;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM