当Jcombobox选择更改时,如何将Jcomponent添加到JPANEL?

发布于 2025-01-28 01:39:08 字数 1857 浏览 3 评论 0原文

每当我切换选择daysoftheekeek 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);
}

}

I'm trying to add a JComponent onto a Jpanel whenever I switch my selection for daysOfTheWeek comboBox. However, it doesn't seem to work, but only works when I put it ouside of theactionPerformed(ActionEvent e) method. What am I doing wrong? Here is my simplified code:

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多孤肩上扛 2025-02-04 01:39:08

Oracle有一个有用的教程,创建带有swing 的GUI。使用Netbeans IDE部分跳过学习秋千。密切注意在容器中放置组件部分。

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

通常,您会从内而外创建一个秋千GUI。您可以定义摆动组件和jpanels,然后让jframe大小本身。

根据您的GUI,这是我想到的GUI。

我使用jframe。您唯一应该扩展jframe或任何Java类的时间是要覆盖一种或多种类方法。

我创建了两个独立的jpanels,一个用于jcombobox,一个用于jtextarea。我在组合框面板中添加了jbutton,因此您可以多次选择同一天。我使用了jtextarea,因此我可以定义一个秋千组件并附加文本。

这是完整的可运行代码。

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

}

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay close attention to the Laying Out Components Within a Container section.

Generally, you design a Swing GUI so that you create all the Swing components once. Then, you update the values of the Swing Components.

Generally, you create a Swing GUI from the inside out. You define your Swing components and JPanels and let the JFrame size itself.

Here's a GUI I came up with, based on your GUI.

Schedule

I use a JFrame. The only time you should extend a JFrame, or any Java class, is when you want to override one or more of the class methods.

I created two separate JPanels, one for the JComboBox and one for the JTextArea. I added a JButton to the combo box panel so you could select the same day more than once. I used a JTextArea so I could define one Swing component and append the text.

Here's the complete runnable code.

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文