表组件在 lwuit 中不起作用?

发布于 2025-01-04 14:58:14 字数 7962 浏览 0 评论 0原文

我想在从组合框中选择值后显示表格。它在第一次选择后在 UI 中添加了另外两个表。
谁能说一下如何解决这个问题?

这里是代码:

package com.onmo.classes;

import java.io.IOException;

import com.sun.lwuit.Button;
import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.TabbedPane;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.SelectionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.table.DefaultTableModel;
import com.sun.lwuit.table.Table;
import com.sun.lwuit.table.TableModel;
import com.sun.lwuit.util.Resources;

public class CompareScreen implements ActionListener, SelectionListener {

    private Command enterCmd;
    private Command backCmd;
    private Command exitCmd;
    private Form comparePage;
    private Label lblLeagues, lblTeam;
    private Button btnTeams, btnPlayers;
    private FlowLayout flowLayout;
    private ComboBox comboTeamA, comboTeamB, comboTeamC, comboTeamD, player1,
            player2;
    private Image firstTeamImage, secondTeamImage = null;
    private Container teamsContainer, teamCombo, teamImages, playersTeamTab,
            playersContainer, playersTab, playerImages;

    int count = 0;
    CompareScreen(){
        enterCmd = new Command("Select");
        backCmd = new Command("Back");
        exitCmd = new Command("Exit");
    }
    public void displayCompareScreen() {
        comparePage = new Form();
        // comparePage.getStyle().setBgColor(0xaa00ff);

        String[] teamA = { "Team A", "Villarreal", "Violent Vegans",
                "Venom XI", "Betis" };
        String[] teamB = { "Team B", "Villarreal", "Violent Vegans",
                "Venom XI", "Betis" };
        String[] playerList1 = { "Player1", "Acho, Sam", "Adams, Mike",
                "Ajirotutu, Seyi", "Abel Gomez" };
        String[] playerList2 = { "Player2", "Acho, Sam", "Adams, Mike",
                "Ajirotutu, Seyi", "Abel Gomez" };

        comparePage.addCommand(backCmd);
        comparePage.addCommandListener(this);
        lblLeagues = new Label("Premier League");
        lblTeam = new Label("Team");

        btnTeams = new Button("Teams");
        btnPlayers = new Button("Players");

        comboTeamA = new ComboBox(teamA);
        comboTeamB = new ComboBox(teamB);
        comboTeamC = new ComboBox(teamA);
        comboTeamD = new ComboBox(teamB);
        player1 = new ComboBox(playerList1);
        player2 = new ComboBox(playerList2);

        comparePage.setLayout(new BorderLayout());
        comparePage
                .addComponent(BorderLayout.WEST, new Label("Premier League"));
        comparePage.addComponent(BorderLayout.EAST, new Label("Compare"));

        flowLayout = new FlowLayout();
        comparePage.setLayout(flowLayout);

        try {
            Resources r = Resources.open("/theme/javathema.res");
            UIManager.getInstance().setThemeProps(r.getTheme("javathema"));
        } catch (IOException ioe) {
            System.out.println("Couldn't load theme.");
        }

        try {
            firstTeamImage = Image.createImage("/team1.png");
            secondTeamImage = Image.createImage("/team2.png");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Label lblFirstTeam = new Label(firstTeamImage);
        lblFirstTeam.setAlignment(Component.LEFT);

        Label lblSecondTeam = new Label(secondTeamImage);
        lblSecondTeam.setAlignment(Component.RIGHT);

        Label lblFirstPlayer = new Label(firstTeamImage);
        lblFirstTeam.setAlignment(Component.LEFT);

        Label lblSecondPlayer = new Label(secondTeamImage);
        lblSecondTeam.setAlignment(Component.RIGHT);

        teamsContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));

        teamCombo = new Container(new BoxLayout(BoxLayout.X_AXIS));
        teamCombo.addComponent(comboTeamA);
        teamCombo.addComponent(comboTeamB);

        teamImages = new Container(new BoxLayout(BoxLayout.X_AXIS));
        teamImages.addComponent(lblFirstTeam);
        teamImages.addComponent(lblSecondTeam);

        teamsContainer.addComponent(teamCombo);
        teamsContainer.addComponent(teamImages);

        playersTeamTab = new Container(new BoxLayout(BoxLayout.X_AXIS));
        playersTeamTab.addComponent(comboTeamC);
        playersTeamTab.addComponent(comboTeamD);

        playersTab = new Container(new BoxLayout(BoxLayout.X_AXIS));
        playersTab.addComponent(player1);
        playersTab.addComponent(player2);

        playerImages = new Container(new BoxLayout(BoxLayout.X_AXIS));
        playerImages.addComponent(lblFirstPlayer);
        playerImages.addComponent(lblSecondPlayer);

        playersContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        playersContainer.addComponent(playersTeamTab);
        playersContainer.addComponent(playersTab);
        playersContainer.addComponent(playerImages);

        TabbedPane tabbedPane = new TabbedPane(TabbedPane.TOP);
        tabbedPane.addTab("Teams", teamsContainer);
        tabbedPane.addTab("Players", playersContainer);

        // comboTeamA.addSelectionListener(this);
        comboTeamB.addSelectionListener(this);
        // comboTeamC.addSelectionListener(this);
        comboTeamD.addSelectionListener(this);

        // comboTeamA.addActionListener(this);

        comparePage.addComponent(tabbedPane);
        comparePage.show();

    }

    public void selectionChanged(int arg0, int arg1) {
        // TODO Auto-generated method stub
        System.out.println("Before  :" + count);
        Table tableTeam = null, tablePlayer = null;
        if (comboTeamA.getSelectedIndex() != 0 && comboTeamB.getSelectedIndex() != 0 && comboTeamC.getSelectedIndex() == 0 && comboTeamD.getSelectedIndex() == 0) {

            TableModel model = new DefaultTableModel(new String[] { "10 ",
                    "Games Played", "10" }, new Object[][] {
                    { "7 ", "Wins", " 6" }, { "2 ", "Draws", " 1" },
                    { "1 ", "Defeats", " 1" }, { "10 ", "Goals for", " 8" }, });

            tableTeam = new Table(model);
            if (count == 1) {
                teamsContainer.addComponent(tableTeam);
                count = 0;
            }

            count = count + 1;
            System.out.println("On  :" + count);
        }

        if (comboTeamA.getSelectedIndex() == 0 && comboTeamB.getSelectedIndex() == 0 && comboTeamC.getSelectedIndex() != 0 && comboTeamD.getSelectedIndex() != 0) {

            TableModel model = new DefaultTableModel(new String[] { "10 ",
                    "Games Played", "10" }, new Object[][] {
                    { "260 ", "Minutes Played", " 280" },
                    { "240 ", "Starts", " 230" },
                    { "20 ", "Substitute", " 30" },
                    { "6 ", "Goals for", " 9" }, });

            tablePlayer = new Table(model);
            if (count == 1) {
                playersContainer.addComponent(tablePlayer);
                count = 0;
            }
            count = count + 1;
            System.out.println("On  :" + count);
        }
        /*
         * if(tableTeam != null && tablePlayer != null) {
         * teamsContainer.removeComponent(tableTeam);
         * playersContainer.removeComponent(tablePlayer); }
         */
        System.out.println("After  :" + count);
    }


    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        HomePage homePageObj=HomePage.getInstance();
        homePageObj.displayHomePage();
    }

}

I want to display the table after selecting the values from comboBox. It working first time selection after that it adding another two more table in UI.
Can anyone say how to fix this?

Here the code:

package com.onmo.classes;

import java.io.IOException;

import com.sun.lwuit.Button;
import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.TabbedPane;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.SelectionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.table.DefaultTableModel;
import com.sun.lwuit.table.Table;
import com.sun.lwuit.table.TableModel;
import com.sun.lwuit.util.Resources;

public class CompareScreen implements ActionListener, SelectionListener {

    private Command enterCmd;
    private Command backCmd;
    private Command exitCmd;
    private Form comparePage;
    private Label lblLeagues, lblTeam;
    private Button btnTeams, btnPlayers;
    private FlowLayout flowLayout;
    private ComboBox comboTeamA, comboTeamB, comboTeamC, comboTeamD, player1,
            player2;
    private Image firstTeamImage, secondTeamImage = null;
    private Container teamsContainer, teamCombo, teamImages, playersTeamTab,
            playersContainer, playersTab, playerImages;

    int count = 0;
    CompareScreen(){
        enterCmd = new Command("Select");
        backCmd = new Command("Back");
        exitCmd = new Command("Exit");
    }
    public void displayCompareScreen() {
        comparePage = new Form();
        // comparePage.getStyle().setBgColor(0xaa00ff);

        String[] teamA = { "Team A", "Villarreal", "Violent Vegans",
                "Venom XI", "Betis" };
        String[] teamB = { "Team B", "Villarreal", "Violent Vegans",
                "Venom XI", "Betis" };
        String[] playerList1 = { "Player1", "Acho, Sam", "Adams, Mike",
                "Ajirotutu, Seyi", "Abel Gomez" };
        String[] playerList2 = { "Player2", "Acho, Sam", "Adams, Mike",
                "Ajirotutu, Seyi", "Abel Gomez" };

        comparePage.addCommand(backCmd);
        comparePage.addCommandListener(this);
        lblLeagues = new Label("Premier League");
        lblTeam = new Label("Team");

        btnTeams = new Button("Teams");
        btnPlayers = new Button("Players");

        comboTeamA = new ComboBox(teamA);
        comboTeamB = new ComboBox(teamB);
        comboTeamC = new ComboBox(teamA);
        comboTeamD = new ComboBox(teamB);
        player1 = new ComboBox(playerList1);
        player2 = new ComboBox(playerList2);

        comparePage.setLayout(new BorderLayout());
        comparePage
                .addComponent(BorderLayout.WEST, new Label("Premier League"));
        comparePage.addComponent(BorderLayout.EAST, new Label("Compare"));

        flowLayout = new FlowLayout();
        comparePage.setLayout(flowLayout);

        try {
            Resources r = Resources.open("/theme/javathema.res");
            UIManager.getInstance().setThemeProps(r.getTheme("javathema"));
        } catch (IOException ioe) {
            System.out.println("Couldn't load theme.");
        }

        try {
            firstTeamImage = Image.createImage("/team1.png");
            secondTeamImage = Image.createImage("/team2.png");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Label lblFirstTeam = new Label(firstTeamImage);
        lblFirstTeam.setAlignment(Component.LEFT);

        Label lblSecondTeam = new Label(secondTeamImage);
        lblSecondTeam.setAlignment(Component.RIGHT);

        Label lblFirstPlayer = new Label(firstTeamImage);
        lblFirstTeam.setAlignment(Component.LEFT);

        Label lblSecondPlayer = new Label(secondTeamImage);
        lblSecondTeam.setAlignment(Component.RIGHT);

        teamsContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));

        teamCombo = new Container(new BoxLayout(BoxLayout.X_AXIS));
        teamCombo.addComponent(comboTeamA);
        teamCombo.addComponent(comboTeamB);

        teamImages = new Container(new BoxLayout(BoxLayout.X_AXIS));
        teamImages.addComponent(lblFirstTeam);
        teamImages.addComponent(lblSecondTeam);

        teamsContainer.addComponent(teamCombo);
        teamsContainer.addComponent(teamImages);

        playersTeamTab = new Container(new BoxLayout(BoxLayout.X_AXIS));
        playersTeamTab.addComponent(comboTeamC);
        playersTeamTab.addComponent(comboTeamD);

        playersTab = new Container(new BoxLayout(BoxLayout.X_AXIS));
        playersTab.addComponent(player1);
        playersTab.addComponent(player2);

        playerImages = new Container(new BoxLayout(BoxLayout.X_AXIS));
        playerImages.addComponent(lblFirstPlayer);
        playerImages.addComponent(lblSecondPlayer);

        playersContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        playersContainer.addComponent(playersTeamTab);
        playersContainer.addComponent(playersTab);
        playersContainer.addComponent(playerImages);

        TabbedPane tabbedPane = new TabbedPane(TabbedPane.TOP);
        tabbedPane.addTab("Teams", teamsContainer);
        tabbedPane.addTab("Players", playersContainer);

        // comboTeamA.addSelectionListener(this);
        comboTeamB.addSelectionListener(this);
        // comboTeamC.addSelectionListener(this);
        comboTeamD.addSelectionListener(this);

        // comboTeamA.addActionListener(this);

        comparePage.addComponent(tabbedPane);
        comparePage.show();

    }

    public void selectionChanged(int arg0, int arg1) {
        // TODO Auto-generated method stub
        System.out.println("Before  :" + count);
        Table tableTeam = null, tablePlayer = null;
        if (comboTeamA.getSelectedIndex() != 0 && comboTeamB.getSelectedIndex() != 0 && comboTeamC.getSelectedIndex() == 0 && comboTeamD.getSelectedIndex() == 0) {

            TableModel model = new DefaultTableModel(new String[] { "10 ",
                    "Games Played", "10" }, new Object[][] {
                    { "7 ", "Wins", " 6" }, { "2 ", "Draws", " 1" },
                    { "1 ", "Defeats", " 1" }, { "10 ", "Goals for", " 8" }, });

            tableTeam = new Table(model);
            if (count == 1) {
                teamsContainer.addComponent(tableTeam);
                count = 0;
            }

            count = count + 1;
            System.out.println("On  :" + count);
        }

        if (comboTeamA.getSelectedIndex() == 0 && comboTeamB.getSelectedIndex() == 0 && comboTeamC.getSelectedIndex() != 0 && comboTeamD.getSelectedIndex() != 0) {

            TableModel model = new DefaultTableModel(new String[] { "10 ",
                    "Games Played", "10" }, new Object[][] {
                    { "260 ", "Minutes Played", " 280" },
                    { "240 ", "Starts", " 230" },
                    { "20 ", "Substitute", " 30" },
                    { "6 ", "Goals for", " 9" }, });

            tablePlayer = new Table(model);
            if (count == 1) {
                playersContainer.addComponent(tablePlayer);
                count = 0;
            }
            count = count + 1;
            System.out.println("On  :" + count);
        }
        /*
         * if(tableTeam != null && tablePlayer != null) {
         * teamsContainer.removeComponent(tableTeam);
         * playersContainer.removeComponent(tablePlayer); }
         */
        System.out.println("After  :" + count);
    }


    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        HomePage homePageObj=HomePage.getInstance();
        homePageObj.displayHomePage();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2025-01-11 14:58:14

问题出在这段代码上:

public void selectionChanged(int arg0, int arg1) {
...
tableTeam = new Table(model);
            if (count == 1) {
                teamsContainer.addComponent(tableTeam);
                count = 0;
            }

            count = count + 1;
...
}

这段代码意味着您每次从 ComboBox 中选择另一个值时都会添加另一个 Table :不要 RESET > “计数”到 0!

The problem is at this code:

public void selectionChanged(int arg0, int arg1) {
...
tableTeam = new Table(model);
            if (count == 1) {
                teamsContainer.addComponent(tableTeam);
                count = 0;
            }

            count = count + 1;
...
}

This code snippet means that you add another Table each time you select another value from the ComboBox : DON'T RESET "count" TO 0 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文