在GUI中使用JComboBox和ActionListener

时间:2014-06-09 00:17:52

标签: user-interface actionlistener jcombobox

我刚刚开始用Java编码,并想知道如何实现这一目标。我想要的是让用户能够选择他们想要学习的子主题,然后在点击子主题时打开带有文本的新面板。我一直在尝试我所知道的一切,但似乎没有任何效果。任何帮助,将不胜感激。谢谢。

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

public class FinalPanel extends JPanel {
private JLabel jcomp1;
private JComboBox jcomp2;
private JComboBox jcomp3;
private JComboBox jcomp4;
private JComboBox jcomp5;
private JLabel jcomp6;

public FinalPanel() {
    //construct preComponents
    String[] jcomp2Items = {"Introduction", "Summary ", "Terms"};
    String[] jcomp3Items = {"Review Test"};
    String[] jcomp4Items = {"Conversion Factors", "Calculations", "Problems a"};
    String[] jcomp5Items = {"Limiting ", "Problems 1", "Percent Yield", "Problems 2",                   
    "Energy Changes", "Problems 3", "Heat of Reaction", "Problems 4"};

    //construct components
    jcomp1 = new JLabel ("Welcome to the LifeSaving Chemistry Study guide!");
    jcomp2 = new JComboBox (jcomp2Items);
    jcomp3 = new JComboBox (jcomp3Items);
    jcomp4 = new JComboBox (jcomp4Items);
    jcomp5 = new JComboBox (jcomp5Items);
    jcomp6 = new JLabel ("Stoichiometry");

    //adjust size and set layout
    setPreferredSize (new Dimension (644, 332));
    setLayout (null);

    //add components
    add (jcomp1);
    add (jcomp2);
    add (jcomp3);
    add (jcomp4);
    add (jcomp5);
    add (jcomp6);

    //set component bounds (only needed by Absolute Positioning)
    jcomp1.setBounds (155, 20, 370, 20);
    jcomp2.setBounds (90, 140, 175, 25);
    jcomp3.setBounds (370, 205, 175, 30);
    jcomp4.setBounds (90, 210, 175, 25);
    jcomp5.setBounds (370, 140, 175, 25);
    jcomp6.setBounds (265, 55, 100, 25);
 }


public static void main (String[] args) {
    JFrame frame = new JFrame ("MyPanel");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add (new FinalPanel());
    frame.pack();
    frame.setVisible (true);

}    
public void actionPerformed (ActionEvent e){
   JFrame frame2 = new JFrame ("Clicked");
   frame2.setVisible(true);
   frame2.setSize(500,600);
   JLabel label= new JLabel("Summary");
   JLabel panel= new JLabel();
   frame2.add(panel);
   panel.add(label);
}

}

0 个答案:

没有答案