簡體   English   中英

如何在通過JComboBox創建的按鈕上放置標簽?

[英]How can I put labels on buttons created from a JComboBox?

我正在創建一個電梯,並從JComboBox中創建了一些按鈕,但是似乎看不到標簽。 最多可創建8個按鈕,並且這些按鈕必須從下至上進行命名。 因此,最后添加的按鈕應該位於第一層。

如何在從JComboBox創建的按鈕上制作標簽?

[-------floor N-------]
[-------floor 3-------]
[-------floor 2-------]
[-------floor 1-------]

這是我的一些代碼...

//The main class
public class Elevator_Simulation extends JFrame implements ActionListener {

public JLabel state; //The current state of the elevator being displayed
public ButtonPanel control; //The button control panel
private Elevator elevator; //The elevator area
String[] floorStrings = {"Select one", "1", "2", "3", "4", "5", "6", "7", "8"};    
JComboBox floorList = new JComboBox(floorStrings); //The combo box
JButton go = new JButton();
public JPanel buttons;
//private int counter;

//constructor
public Elevator_Simulation() {        

    //Setting up layout and content pane
    this.setSize(500, 500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocation(100, 100);
    this.getContentPane().setLayout(new BorderLayout(1, 1));

    buttons = new JPanel(new GridLayout(8, 1));
    add(buttons);

    //Panel creation
    JPanel centerpanel = new JPanel();
    centerpanel.setLayout(new FlowLayout());

    //Adds the button panel to the BorderLayout
    this.getContentPane().add(buttons, BorderLayout.EAST);

    // adds the title to the top of p3
    p3.add(title, BorderLayout.NORTH);
    // adds floorlist to the top right of p3
    p3.add(floorList, BorderLayout.NORTH);
    // adds the start button to the panel
    p3.add(go, BorderLayout.NORTH);
    go.setText("Start");
    go.addActionListener(this);
    // adds p2 to the right of the container
    this.getContentPane().add(p3, BorderLayout.NORTH);

//Main method
public static void main(String[] args) {
    Elevator_Simulation eSim = new Elevator_Simulation();
    eSim.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    eSim.setVisible(true);
}

//start of the actionPerformed
@Override
public void actionPerformed(ActionEvent e) {
    int count = floorList.getSelectedIndex();
    //buttons.removeAll();
    for (int index = 0; index < count; index++) {
        buttons.add(new JButton("F" + String.valueOf(index)));
    }
    buttons.revalidate();

    elevator = new Elevator(this);
    this.getContentPane().add(elevator, BorderLayout.CENTER);

}
//end of the actionPerformed

更改floorStrings的順序,以使樓層按您期望的順序顯示。

在此處輸入圖片說明

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestComboBox08 {

    public static void main(String[] args) {
        new TestComboBox08();
    }

    public TestComboBox08() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JComboBox cb = new JComboBox(new String[]{"Select one", "8", "7", "6", "5", "4", "3", "2", "1"});

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(cb);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

已更新,可以簡單地反轉選擇索引...

因此,現在我們顛倒了順序,所以我們顛倒了選擇索引(項目8不在位置1 )。

我可以看到解決此問題的最簡單方法是使用Arrays.asList(floorsList).indexOf(...) ,它將返回所選值在floorsList數組中的位置...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestComboBox08 {

    public static void main(String[] args) {
        new TestComboBox08();
    }

    private String[] floorsList = new String[]{"Select one", "8", "7", "6", "5", "4", "3", "2", "1"};

    public TestComboBox08() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JComboBox cb = new JComboBox(floorsList);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(cb);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                cb.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String value = (String)cb.getSelectedItem();
                        int index = Arrays.asList(floorsList).indexOf(value);
                        System.out.println("Item at " + index + " = " + floorsList[index]);
                    }
                });
            }
        });
    }

}

使用ListCellRenderer操縱項目在組合框中的顯示方式。

http://docs.oracle.com/javase/6/docs/api/javax/swing/ListCellRenderer.html

編輯:

實現ListCellRenderer是一個更好的說法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM