第二个按钮没有响应

时间:2015-12-22 21:56:30

标签: java swt

我在欧洲最高的山峰上制作地图。我在Label中设置了背景,以使按钮不可见:[编辑完整代码]

package topOfEurope;
//Importing packages
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.*;

public class display {
    public static String[] country = {"Albania","Andorra","Austria", "Azerbaijan","Belarus","Belgium","Bosnia and Herzegovina",
        "Bulgaria","Croatia","Cyprus","Czech Republic","Denmark","Estonia","Finland","France","Georgia",
        "Germany","Greece","Hungary","Iceland","Ireland","Italy","Kosovo","Latvia","Liechtenstein","Lithuania",
        "Luxembourg","Macedonia","Malta","Moldova","Monaco","Montenegro","Netherlands","Norway","Poland",
        "Portugal","Romania","Russia","San Merino","Serbia","Slovakia","Slovenia","Spain","Sweden","Switzerland",
        "Turkey","Ukraine","United Kingdom","Vatican City"};
    public static String[] highestPoint = {"Mount Korab","Coma Pedrosa","Großglockner","Bazardüzü","Dzyarzhynskaya Hara","Signal de Botrange",
        "Maglić","Musala","Dinara","Mount Olympus (Cyprus)","Sněžka","Møllehøj","Suur Munamägi","Halti","Mont Blanc","Shkhara",
        "Zugspitze","Mount Olympus","Kékes","Hvannadalshnúkur","Carrauntoohil","Monte Bianco","Djeravica/Great Rudoka",
        "Gaizinkalns","Vorder Grauspitz","Aukštojas Hill","Kneiff","Mount Korab","Ta' Dmejrek","Bălăneşti","Chemin des Révoires",
        "Zla Kolata","Vaalserberg","Galdhøpiggen","Mount Rysy","Mount Pico","Moldoveanu Peak","Mount Elbrus","Monte Titano",
        "Midžor","Gerlachovský štít","Triglav","Mulhacén","Kebnekaise","Monte Rosa","Mahya Dağı","Hoverla","Ben Nevis",
        "unnamed location"};
    public static String[] MAMSL = {"2764","2942","3798","4466","345","694","2386","2925","1831","1952","1603","171","318","1324","4810","5201",
        "2962","2919","1014","2110","1041","4810","2656","311","2599","294","560","2764","253","430","163","2534","321","2469",
        "2499","2351","2544","5642","749","2169","2655","2864","3479","2104","4634","1031","2061","1344","75"};
    protected Shell shell;
    public Button _2;
    private Label txtCountry;
    private Label txtHighestMountain;
    private Label txtMeters;
    private Label lblNewLabel;
    private Button button;
    private Button _3;
    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            display window = new display();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();}}
    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }}}
    /**
     * Create contents of the window.
     */


    private SelectionListener getCountrySelectionListener(final String country, final String MAMSL, final String highestPoint) {
        return new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                try{
                txtCountry = new Label(shell, SWT.BORDER);
                txtCountry.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
                txtCountry.setText("Country: \r\n" + country);
                txtCountry.setBounds(1082, 0, 349, 81);
                txtCountry.setFont(SWTResourceManager.getFont("Lucida Grande", 20, SWT.BOLD));

                txtHighestMountain = new Label(shell, SWT.BORDER);
                txtHighestMountain.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
                txtHighestMountain.setText("Highest Mountain: \r\n" + highestPoint);
                txtHighestMountain.setBounds(1082, 81, 349, 81);
                txtHighestMountain.setFont(SWTResourceManager.getFont("Lucida Grande", 20, SWT.BOLD));

                txtMeters = new Label(shell, SWT.BORDER);
                txtMeters.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
                txtMeters.setText("Meters above sea level: \r\n" + MAMSL);
                txtMeters.setBounds(1082, 159, 349, 81);
                txtMeters.setFont(SWTResourceManager.getFont("Lucida Grande", 20, SWT.BOLD));
                }
                catch(Exception exc) {
                    MessageDialog.openError(shell,"Error","anuallyInvestedMoney amount");
                    return;
                }
}};}    

    protected void createContents() {
        shell = new Shell();
        shell.setImage(null);
        shell.setSize(1440, 899);
        shell.setText("List of highest points of European countries");

        //testing
        System.out.println(getInfo.getInfo3());

        //Setting background image
        //shell.setBackgroundImage(SWTResourceManager.getImage("/Users/Hadle/Downloads/europe_topography_map_en.jpg"));
        Label lblNewLabel = new Label(shell, SWT.NONE);
        lblNewLabel.setImage(SWTResourceManager.getImage("/Users/Hadle/Downloads/europe_topography_map_en.jpg"));
        lblNewLabel.setBounds(-206, -170, 1473, 1198);

        //Creating buttons

        Button _1 = new Button(shell, SWT.NONE);
        _1.setBounds(532, 695, 23, 50);
        _1.setText("1");

        _2 = new Button(shell, SWT.NONE);
        _2.setBounds(166, 655, 94, 28);
        _2.setText("2");

        _3 = new Button(shell, SWT.NONE);
        _3.setBounds(410, 552, 67, 38);
        _3.setText("New Button");

        //Creating button array
        Button[] btnArr = new Button[] {
                _1, _2, _3
            };

        //Assigning values to buttons
        for(int i = 0; i < btnArr.length; i++) {
            btnArr[i].addSelectionListener(getCountrySelectionListener(country[i],MAMSL[i],highestPoint[i]));
        } } }

当我运行程序并单击一个按钮查看一个国家中最高山的信息时,它工作正常。但是,当我想点击另一个国家/地区时,代码不会响应,但会停留在最后一个按钮的信息中。因此,我正在寻找改变的建议,以便我可以点击许多按钮,它将显示所有不同的国家而不会卡住。

为了给按钮分配值,我使用以下代码:

    //Creating button array
    Button[] btnArr = new Button[] {
            _1, _2
        };

    //Assigning values to buttons
    for(int i = 0; i < btnArr.length; i++) {
        btnArr[i].addSelectionListener(getCountrySelectionListener(country[i],MAMSL[i],highestPoint[i]));

干杯

1 个答案:

答案 0 :(得分:0)

我认为你刚才没有创建按钮_2。

//wrong _2 = new Button(); //right Button _2 = new Button();

如果那是错的,我很抱歉。我在很多方面都不了解你的代码。 你可以使用JButton,因为没有.setText或带有两个参数的新Button()。

另一个问题可能是您没有设置布局。 frame.setLayout(null);

否则我需要整个代码。

干杯

相关问题