2017-10-31 23 views
0

私はテキスト解析プログラム(単語数、TF-IDFなど)を作成してJavaを実践しています。JPanelドロップダウンメニューが表示されません

私は今、フレームの上部に表示される小さなGUIを構築しています。私はドロップダウンメニューを望んでおり、JPanelといくつかのJButtonコンポーネントを使って作成しようとしています。しかし、ドロップダウンを再利用可能にするために、私はそれを管理するクラスを構築しようとしていました。

しかし、私は問題が発生しました。ドロップダウンメニューを作成しようとすると、フレームの左上隅にのみ表示され、移動しようとすると完全に消えたり消えたりします。

同じことをする簡単な方法を作成しようとしましたが、問題は同じです。ここに関連するコードは、(プロセッサとStateものを無視する)です:

public class ExperimentState extends State { 

    private JFrame frame; 

    public ExperimentState(Processor processor) { 
     super(processor); 
     initialize(); 
    } 

    private void initialize() { 
     int width, height; 
     //these variables are temporary, for testing 
     width = 800; 
     height = 640; 

     //Set up the frame 
     frame = new JFrame(); 
     frame.setSize(width, height); 
     frame.getContentPane().setLayout(null); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Test code 
     String[] buttonNames = { "42", "Don't panic!", "Towel"}; 
     //       x, y, width, height, button names 
     JPanel panel = createPanel(0, 0, 100, 25, buttonNames); 
     frame.getContentPane().add(panel); 
    } 

    public JPanel createPanel(int x, int y, int width, int height, String[] names) { 
     //Create and setup the panel 
     JPanel panel = new JPanel(); 
     panel.setLayout(null); 
     panel.setBounds(x, y, width, height); 

     JButton[] buttons = new JButton[names.length]; 
     for(int i = 0; i < names.length; i++) { 
      JButton button = new JButton(names[i]); 
      button.setBounds(x, y + height*i, width, height); //each button has the size of the panel, and are placed on top of each other 
      panel.add(button); 
      buttons[i] = button; 
     } 
     buttons[0].addMouseListener(new MouseAdapter() { 
      public void mouseClicked(MouseEvent e) { 
       panel.setBounds(x, y, width, height*names.length); //the panel extends to reveal all the buttons 
      } 
      public void mouseExited(MouseEvent e) { 
       panel.setBounds(x, y, width, height); //the panel retracts 
      } 
     }); 

     return panel; 
    } 

    @Override 
    public void display() { 
     frame.setVisible(true); 
    } 

    @Override 
    public void hide() { 
     frame.setVisible(false);  
    } 
} 

私は本当に彼らがどのように動作するか分からないため、マウスリスナーに問題があるだろうと思ったし、彼らはことはできないだろうと思いましたxywidthおよびheightにアクセスするには、いつでも変数を呼び出すことができましたが、問題はありませんでした。

createPanel(50, 0, 100, 25, buttonNames)のようなものを使ってcreatePanel()と呼ぶと問題が発生します。

ここでxは0ではなく50です。表示されると、パネルは右に移動しますが、そのボタンは見えない線で半分にカットされます。 x = 100の場合、完全に見えなくなります。

どうしてですか?私は何が欠けていますか?

+0

自分で場所を設定しないでください。適切なレイアウトマネージャを使用してください。 –

+0

すぐにお返事ありがとうございます。 これは問題の中核ですか?同じドロップダウンメニューを手動で作成しても、フレーム内のどこにでも配置することができます。私は仕事をするためにメソッドやクラスを使うときだけ問題があります。 – XanXan

+0

はい私はそれがあなたの問題であると確信していますウェブ上のどこにでもそのような質問がたくさんあり、ソリューションは常に同じです...例:https://stackoverflow.com/questions/17481559/jbutton-setboundsx-ywh-doesnt -sem-to-work –

答えて

0

コンテナ内のコンポーネントは、これらのコンポーネントの境界が完全に(レイアウトマネージャなしで)完全に表示されます。 x座標を0から50に変更すると、ボタンの位置が移動します。サイズやコンテナのサイズを変更しないと、ボタンは切り取られます。

この問題を解決する最も簡単な方法は、ボタンの位置にあるxとyを0に置き換え、Panelの位置にある0をxとyに置き換えることです。

編集:類似した問題:Components not showing in second JPanel

関連する問題