JPanel设置为隐藏,除默认设置以外的组合框选项将其设置为可见,但组件丢失

时间:2011-02-13 15:12:05

标签: java swing jpanel visibility jcombobox

我正在建立一个欧姆法律应用程序。请记住这是我的第一个程序(没有教程)所以请善待=)以下是它运行时的样子:

http://imgur.com/QYe4V

组合框有计算伏特,计算欧姆等的选项......

中心面板设置为隐藏,直到您从组合框中进行选择。以下是您做出选择时的样子:

http://imgur.com/ZlWJX

我面临的问题是,第一次时间我做出选择,只显示一个JLabel / JTextArea组合(每对组合在一个垂直框内的自己的面板上):

http://imgur.com/vNYX1

如果我单击组合框并再次进行相同选择,则会正确显示。我无法弄清楚为什么它在第一次点击时行为不正确。也许我在构建GUI时采取了错误的方法。这是迄今为止的代码:

private JFrame frame;
private String[] choiceList = {"", "Calculate Volts", "Calculate Amps", "Calculate Ohms", "Calculate Watts"};
private JTextField textField_2;
private JPanel centerPanel;
private String volts = "Volts";
private String amps = "Amps";
private String ohms = "Ohms";
private String watts = "Watts";
private JLabel var1Label;
private JLabel var2Label;
private JLabel var3Label;
private JFormattedTextField var1TextField;
private JFormattedTextField var2TextField;
private JFormattedTextField var3TextField;



/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GUI window = new GUI();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public GUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setTitle("BotsOne ElectriCalc");
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel northPanel = new JPanel();
    northPanel.setIgnoreRepaint(true);
    northPanel.setBorder(new LineBorder(Color.GRAY));
    frame.getContentPane().add(northPanel, BorderLayout.NORTH);

    JLabel choiceLabel = new JLabel("Please make a selection:");
    northPanel.add(choiceLabel);

    JComboBox choiceCombo = new JComboBox(choiceList);
    northPanel.add(choiceCombo);
    choiceCombo.addActionListener(new ChoiceComboListener());

    JPanel southPanel = new JPanel();
    southPanel.setIgnoreRepaint(true);
    southPanel.setBorder(new LineBorder(Color.GRAY));
    frame.getContentPane().add(southPanel, BorderLayout.SOUTH);

    JLabel label = new JLabel("Answer:");
    southPanel.add(label);

    textField_2 = new JTextField();
    textField_2.setColumns(10);
    southPanel.add(textField_2);

    centerPanel = new JPanel();
    centerPanel.setVisible(false);
    centerPanel.setBorder(new LineBorder(Color.GRAY));
    frame.getContentPane().add(centerPanel, BorderLayout.CENTER);

    Box centerPanelVertBox = Box.createVerticalBox();
    centerPanelVertBox.setAlignmentX(Component.CENTER_ALIGNMENT);
    centerPanel.add(centerPanelVertBox);
    centerPanelVertBox.setVisible(true);
    centerPanelVertBox.setBorder(null);

    JLabel pleaseEnterLabel = new JLabel("Please enter 2 of 3 values:");
    pleaseEnterLabel.setBorder(new EmptyBorder(15, 0, 10, 0));
    pleaseEnterLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
    centerPanelVertBox.add(pleaseEnterLabel);

    JPanel var1Panel = new JPanel();
    centerPanelVertBox.add(var1Panel);

    var1Label = new JLabel("xxx");
    var1Label.setAlignmentX(Component.CENTER_ALIGNMENT);
    var1Panel.add(var1Label);

    var1TextField = new JFormattedTextField(NumberFormat.getInstance());
    var1TextField.setColumns(10);
    var1Panel.add(var1TextField);

    Panel var2Panel = new Panel();
    centerPanelVertBox.add(var2Panel);

    var2Label = new JLabel("xxx");
    var2Label.setAlignmentX(Component.CENTER_ALIGNMENT);
    var2Panel.add(var2Label);

    var2TextField = new JFormattedTextField(NumberFormat.getInstance());
    var2TextField.setColumns(10);
    var2Panel.add(var2TextField);

    Panel var3Panel = new Panel();
    centerPanelVertBox.add(var3Panel);

    var3Label = new JLabel("xxx");
    var3Label.setAlignmentX(Component.CENTER_ALIGNMENT);
    var3Panel.add(var3Label);

    var3TextField = new JFormattedTextField(NumberFormat.getInstance());
    var3TextField.setColumns(10);
    var3Panel.add(var3TextField);

    Panel calculatePanel = new Panel();
    centerPanelVertBox.add(calculatePanel);

    JButton calculateButton = new JButton("Calculate");
    calculatePanel.add(calculateButton);
}

public class ChoiceComboListener implements ActionListener {    //combobox actionlistener

    public void actionPerformed(ActionEvent ev) {
        JComboBox cb = (JComboBox)ev.getSource();
        String currentComboSelection = (String)cb.getSelectedItem();
        if (currentComboSelection.equals(choiceList[1])) {                  //choice 1 (Calculate Volts)
            centerPanel.setVisible(true);
            var1Label.setText(amps);
            var2Label.setText(ohms);
            var3Label.setText(watts);               
            //centerPanel.repaint();                
        }
        if (currentComboSelection.equals(choiceList[2])) {                //choice 2 (Calculate Amps)
            centerPanel.setVisible(true);
            var1Label.setText(volts);
            var2Label.setText(ohms);
            var3Label.setText(watts);
            centerPanel.repaint();              
        }
        if (currentComboSelection.equals(choiceList[3])) {
            centerPanel.setVisible(true);
            var1Label.setText(volts);
            var2Label.setText(amps);
            var3Label.setText(watts);
        }
        if (currentComboSelection.equals(choiceList[4])) {
            centerPanel.setVisible(true);
            var1Label.setText(volts);
            var2Label.setText(amps);
            var3Label.setText(ohms);
        }
        if (currentComboSelection.equals(choiceList[0])) {
            centerPanel.setVisible(false);
            //centerPanel.repaint();
        }
    }

}

如果你看一下底部,有一个名为ChoiceComboListener的内部类,这是comboBox监听器,我尝试过玩弄repaint()和其他各种东西,但我无法想出任何东西。任何帮助,指导或批评都表示赞赏。

3 个答案:

答案 0 :(得分:3)

  

感谢任何帮助,指导或批评

另一个opton可能是使用Card Layout。本教程有一个可以完全满足您需求的工作示例。

答案 1 :(得分:3)

“请记住,这是我的第一个程序(没有教程)” - 干得好,干得好。一个建议是,你可能希望考虑将你的电气单元变成一个枚举,因为它们似乎是练习这个重要概念的完美场所。此外,您可以使用GridBagLayout更好地排列JTextFields,但可以非常使用它。

例如,在枚举:

enum ElectricalUnits {
   AMPS("Amps"), OHMS("Ohms"), WATTS("Watts"), VOLTS("Volts");

   private String text;
   private ElectricalUnits(String text) {
      this.text = text;
   }
   public String getText() {
      return text;
   }
   @Override
   public String toString() {
      return text;
   }
}

然后用户选择可以是枚举,这可以简化gui逻辑部分中的一些代码。

答案 2 :(得分:0)

我想通了,我把第一个面板(显示正确的面板)声明为JPanel,然后我宣布其他2个为常规面板。啧!无论如何,如果有人想提供一些建议或批评,它仍然受到欢迎! - 机器人