0

私は3つのパネルを含むJPanelを持っています。パネルのレイアウトはBorderLayoutであり、3つのパネルはBorderLayout.NORTH、.CENTER、および.SOUTHを使用して分割されています。.SOUTHに設定されているパネルは「下」と呼ばれます。下部には、ユーザーがその上のパネルのコンボボックスから選択した内容に応じて情報を表示する必要があるtextAreaがあります。ただし、何らかの理由で、テキスト領域が情報を更新していません。たとえば、ユーザーが「マウンテンバイク」を選択すると、テキスト領域にマウンテンバイクに関する情報が表示されるという考え方です。その後、直後に「ロードバイク」を選択すると、ロードバイクの情報が表示されます。なんらかの理由でこれを行っていません、すべてのクラスインスタンスが正しいので、それを更新することが問題であることは確かです。コンボボックスに異なる開始インデックスを選択しようとすると、テキスト領域に正しい情報が表示されました。これが私の構成者です:

    public MainGUI(){
       System.out.print("Loading...");
       frame.setVisible(true);
       frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
       frame.add(main);
       int frameSize = frame.getWidth();
       instructionField.setColumns(frameSize);
       instructionField.setEnabled(false);
       instructionField.setDisabledTextColor(Color.RED);
       instructionField.setText("Welcome to the Bike App! Please choose from the following bikes!");
       Color backColor = frame.getBackground();
       instructionField.setBackground(backColor);
       main.add(instructionField, BorderLayout.NORTH);
       main.add(middle, BorderLayout.CENTER);
       main.add(bottom, BorderLayout.SOUTH);
       GridBagConstraints gbc = new GridBagConstraints();
       gbc.insets = new Insets(10,10,10,10);
       gbc.gridx=0;
       gbc.gridy=0;
       middle.add(bikeComboBox, gbc);
       submit = new JButton("Load Selected Bike");
       gbc.gridx=1;
       gbc.gridy=0;
       middle.add(submit, gbc);
       bikeComboBox.addActionListener(this);
       submit.addActionListener(this);
       //bottom is a JPanel
       //HERE IS WHERE THE TEXT AREA CODE IS
       informationTextArea = new JTextArea();
       JScrollPane sp = new JScrollPane(informationTextArea);
       informationTextArea.setEditable(false);
       bottom.add(informationTextArea, BorderLayout.CENTER);
       //bikeComboBox.setSelectedIndex(1);
       selectedBikeIndex = bikeComboBox.getSelectedIndex();
       selectedBikeName = bikeTypes[selectedBikeIndex];
       String returnedInfo = loadSelectedBikeInfo(selectedBikeName);
       informationTextArea.setText(returnedInfo);
       informationTextArea.updateUI();
       bottom.updateUI();
       informationTextArea.setBackground(backColor);
       frame.setSize(400,400);
    }

loadSelectedBikeInfo(String selectedBikeName)メソッドは次のとおりです。

    public String loadSelectedBikeInfo(String selectedBikeName){
      String s = selectedBikeName;
      String selectedInfo = "";
      if(s.equalsIgnoreCase(this.mounBikeType)){//load mountain bike info
          selectedInfo = mountainBike.getBikeInfo();
      }
      if(s.equalsIgnoreCase(this.roadBikeType)){//load mountain bike info
          selectedInfo = roadBike.getBikeInfo();
      }
      if(s.equalsIgnoreCase(this.raciBikeType)){//load mountain bike info
          selectedInfo = racingBike.getBikeInfo();
      }
      if(s.equalsIgnoreCase(this.cruisBikeType)){//load mountain bike info
          selectedInfo = cruiserBike.getBikeInfo();
      }
      return selectedInfo; 
    }

コンボボックスではなく、ボタンのactionPerformedメソッドを次に示します(コンボボックスのメソッドを作成する方法がわかりません)。

    public void actionPerformed(ActionEvent e){
      if(e.getSource()==submit){
         selectedBikeIndex = bikeComboBox.getSelectedIndex();
         selectedBikeName = bikeTypes[selectedBikeIndex];
         loadSelectedBikeObject(selectedBikeName);
      }
    }

編集:別のクラスでactionPerformedメソッドを作成し、そのリスナーをコンボボックスに追加しました。なぜまだ更新されていないのですか?

    public class ComboBoxUpdateListener implements ActionListener{
      private MainGUI maingui;

      ComboBoxUpdateListener(MainGUI _mainGUI){
         this.maingui = _mainGUI;
      }

      @Override
      public void actionPerformed(ActionEvent ae) {
         JComboBox cb = (JComboBox)(ae.getSource());
         String bikeTypePassed = maingui.bikeTypes[cb.getSelectedIndex()];
         System.out.println("This is from the LISTENERCLASS: "+bikeTypePassed);
         maingui.loadSelectedBikeInfo(bikeTypePassed);
         maingui.informationTextArea.updateUI();
         maingui.bottom.updateUI();
      }
    }
4

1 に答える 1

3

コンボ ボックスに異なる開始インデックスを選択しようとすると、すべてテキスト領域に正しい情報が表示されました。

作業コードに対して行ったことをよく見てください。

String returnedInfo = loadSelectedBikeInfo(selectedBikeName);
informationTextArea.setText(returnedInfo);

あなたの編集によると:

編集: 別のクラスで actionPerformed メソッドを作成し、そのリスナーをコンボ ボックスに追加しました。まだ更新されていないのはなぜですか?

  @Override
  public void actionPerformed(ActionEvent ae) {
     JComboBox cb = (JComboBox)(ae.getSource());
     String bikeTypePassed = maingui.bikeTypes[cb.getSelectedIndex()];
     System.out.println("This is from the LISTENERCLASS: "+bikeTypePassed);
     maingui.loadSelectedBikeInfo(bikeTypePassed);
     maingui.informationTextArea.updateUI();
     maingui.bottom.updateUI();
  }

loadSelectedBikeInfo(bikeTypePassed)を返しますString:

public String loadSelectedBikeInfo(String selectedBikeName){
      String s = selectedBikeName;
      String selectedInfo = "";
      if(s.equalsIgnoreCase(this.mounBikeType)){//load mountain bike info
          selectedInfo = mountainBike.getBikeInfo();
      }
      if(s.equalsIgnoreCase(this.roadBikeType)){//load mountain bike info
          selectedInfo = roadBike.getBikeInfo();
      }
      if(s.equalsIgnoreCase(this.raciBikeType)){//load mountain bike info
          selectedInfo = racingBike.getBikeInfo();
      }
      if(s.equalsIgnoreCase(this.cruisBikeType)){//load mountain bike info
          selectedInfo = cruiserBike.getBikeInfo();
      }
      return selectedInfo; 
    }

したがって、それを取得して、次のように呼び出す必要がありStringます。setText(String text)JTextArea

  @Override
  public void actionPerformed(ActionEvent ae) {
     JComboBox cb = (JComboBox)(ae.getSource());
     String bikeTypePassed = maingui.bikeTypes[cb.getSelectedIndex()];
     System.out.println("This is from the LISTENERCLASS: "+bikeTypePassed);
     String info=maingui.loadSelectedBikeInfo(bikeTypePassed);
     maingui.informationTextArea.setText(info);
  }

現在のルック アンド フィールの値で UI プロパティをリセットします。

何かrepaint()を呼び出す必要がある場合でも、これには理由がありません。

  • コンテンツを追加した後、可視に設定する前にsetSize正しい実装LayoutManagerと呼び出しを使用しないでくださいpack()JFrame
于 2013-02-03T17:56:10.320 に答える