2016-06-17 28 views
-3

我想创建一个选项列表,当我的用户将鼠标悬停在按钮鼠标指针上变成手形指针并且一行显示在单词下面时,我如何在java swing中实现这个功能? 这里是一些代码,我试过如何在java swing中实现这个GUI列表

JButton[] buttons = new JButton[20]; 
    buttons [0] = new JButton("Option 1"); 
    buttons [0].setOpaque(true); 
    buttons [0].setRolloverEnabled(true); 
    buttons [0].setContentAreaFilled(false); 
    buttons [0].addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseEntered(MouseEvent e) { 
      super.mouseEntered(e); 
      buttons [0].setFocusPainted(true); 
      buttons [0].setFocusable(true); 
      buttons [0].setContentAreaFilled(true); 
      buttons [0].setCursor(new Cursor(Cursor.HAND_CURSOR)); 
     } 

    }); 

enter image description here

+1

它看起来像一个JTree https://docs.oracle.com/javase/tutorial/ uiswing/components/tree.html –

+0

它(图片)看起来像树。这是你想要实现的吗? – Beniton

+0

我试图使用JTree,但它的外观非常难看,并不像照片 – Joe22434

回答

2

THI是一个例子,试试这个

import javax.swing.JOptionPane; 
    public class InputDialogWithDropdownListbox { 
    public static void main(String[] a) { 
    String[] choices = { "A", "B", "C", "D", "E", "F" }; 
    String input = (String) JOptionPane.showInputDialog(null, "Choose now...", 
    "The Choice of a Lifetime", JOptionPane.QUESTION_MESSAGE, null, // Use 
                    //       default 
                    // icon 
    choices, // Array of choices 
    choices[1]); // Initial choice 
    System.out.println(input); 
} 
    }