JPanel is set to invisible, combobox selection other than the default sets it to visible, but components are missing

I am creating an om law application. Please keep in mind that this is my first program (without tutorials), so please be kind =) Here is what it looks like at startup:

http://imgur.com/QYe4V

The combo box has options for calculating volts, calculating Ohms, etc.

The central panel is set to invisible until you make a choice from the drop-down list. Here's what it should look like when you make a choice:

http://imgur.com/ZlWJX

The problem I am facing is that the first time that I do is only one of the JLabel / JTextArea combines (each pair is on its own panel inside a vertical window):

http://imgur.com/vNYX1

If I click on comboBox and make the same selection again, it will display correctly. I cannot understand why it does not work correctly on the first click. Maybe I'm using the wrong approach when building a GUI. Here's the code for now:

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

If you look at the bottom, there is an inner class ChoiceComboListener, it is a comboBox listener, I tried to play with repaint () and other things, but I can not think of anything. Any help, guidance or criticism is appreciated.

+4
source share
3 answers

Any help, guidance or criticism is appreciated.

Another option might be to use map planning . The tutorial has a working example that does exactly what you want.

+3
source

"Please keep in mind that this is my first program (without tutorials)" - Good, well done. One suggestion is that you might want to consider turning your electrical units into renames, as they seem like the perfect place to practice this important concept. Alternatively, you can use JTextFields with a GridBagLayout, but it can be very usable.

for example, on the listing:

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

Then the user's choice can be an enumeration, and this can simplify the part of the code in your logical part of gui.

+3
source

I realized I had the first panel (what displayed correctly) declared as JPanel, and I declared the other 2 as regular panels. Sheesh! In any case, if someone wants to give advice or criticism, he is still welcome! - bots

0
source

Source: https://habr.com/ru/post/1339558/


All Articles