如何使用ComboBox在卡之间切换

时间:2017-04-28 02:17:25

标签: java swing combobox jcombobox cardlayout

我正在尝试使用Jcombobox" Expenses"来更换卡片面板。有人能告诉我我做错了什么吗?我让面板显示正常,但是当我看到Expenses j选项卡式窗格时,会显示第一张卡片。一旦我尝试在它们之间切换,控制台就会出现错误。我注意到所有卡片都在同时显示,但它们是重叠的。 1)如何一次只显示一张卡片(面板)? 2)如何让JComboBox在卡片(面板)之间切换?

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.JList;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JButton; 
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JRadioButton;
import javax.swing.JLabel;

public class LanaFrame extends JFrame implements ItemListener{

private JPanel contentPane, cards;
private JList driverlist;
private JComboBox expenses;
final static String FUEL= "Fuel";
final static String TOLL= "Toll";
final static String REPAIR="Repair";
private static String[] comboboxitems= {FUEL,TOLL,REPAIR};
private JTextField textFieldFirstName;
private JTextField textFieldLastName;
private JTextField textFieldTruckNumber;
private JTextField textFieldTrailerNumber;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                LanaFrame frame = new LanaFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public LanaFrame() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 800, 500);
    setResizable(false);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBounds(5, 5, 775, 480);
    contentPane.add(tabbedPane);

    //Drivers Panel
    JPanel Drivers = new JPanel();
    tabbedPane.addTab("Drivers", null, Drivers, null);
    Drivers.setLayout(null);

    driverlist= new JList(comboboxitems);
    driverlist.setBackground(Color.WHITE);
    driverlist.setVisibleRowCount(4);
    driverlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    Drivers.add(driverlist);
    driverlist.setBounds(482,6,266,211);

    JButton btnAddDriver = new JButton("Add Driver");
    btnAddDriver.setBounds(5, 285, 117, 29);
    Drivers.add(btnAddDriver);

    JTextArea driverArea = new JTextArea();
    driverArea.setBackground(Color.LIGHT_GRAY);
    driverArea.setBounds(482, 217, 266, 211);
    Drivers.add(driverArea);
    driverArea.setEditable(false);


    JTextPane txtpnFirstName = new JTextPane();
    txtpnFirstName.setText("First name");
    txtpnFirstName.setBounds(5, 20, 140, 15);
    Drivers.add(txtpnFirstName);
    txtpnFirstName.setEditable(false);

    textFieldFirstName = new JTextField();
    textFieldFirstName.setBounds(5, 40, 140, 30);
    Drivers.add(textFieldFirstName);
    textFieldFirstName.setColumns(10);

    JTextPane txtpnLastName = new JTextPane();
    txtpnLastName.setText("Last Name");
    txtpnLastName.setBounds(5, 75, 140, 15);
    Drivers.add(txtpnLastName);
    txtpnLastName.setEditable(false);

    textFieldLastName = new JTextField();
    textFieldLastName.setBounds(5, 95, 140, 30);
    Drivers.add(textFieldLastName);
    textFieldLastName.setColumns(10);

    JTextPane txtpnTruckNumber = new JTextPane();
    txtpnTruckNumber.setText("Truck Number");
    txtpnTruckNumber.setBounds(5, 130, 140, 15);
    Drivers.add(txtpnTruckNumber);
    txtpnTruckNumber.setEditable(false);

    textFieldTruckNumber = new JTextField();
    textFieldTruckNumber.setBounds(5, 150, 140, 30);
    Drivers.add(textFieldTruckNumber);
    textFieldTruckNumber.setColumns(10);

    JTextPane txtpnTrailerNumber = new JTextPane();
    txtpnTrailerNumber.setText("Trailer Number");
    txtpnTrailerNumber.setBounds(5, 185, 140, 15);
    Drivers.add(txtpnTrailerNumber);
    txtpnTrailerNumber.setEditable(false);

    textFieldTrailerNumber = new JTextField();
    textFieldTrailerNumber.setBounds(5, 205, 140, 30);
    Drivers.add(textFieldTrailerNumber);
    textFieldTrailerNumber.setColumns(10);

    JButton btnDeleteDriver = new JButton("Delete Driver");
    btnDeleteDriver.setBounds(359, 185, 117, 29);
    Drivers.add(btnDeleteDriver);

    //New Expenses Panel
    JPanel NewExpense = new JPanel();
    tabbedPane.addTab("New Expense", null, NewExpense, null);
    //adds types of expenses to jcombo
    expenses = new JComboBox(comboboxitems);
    expenses.setBounds(257, 5, 236, 30);
    expenses.setEditable(false);
    expenses.addItemListener(this);
    NewExpense.setLayout(null);
    NewExpense.add(expenses);



    //create panels for each combo option
    JPanel fuel = new JPanel();
    fuel.setBounds(0, 0, 743, 399);
    fuel.setBackground(Color.BLUE);
    JPanel toll = new JPanel();
    toll.setBounds(0, 0, 743, 399);
    toll.setBackground(Color.RED);
    JPanel repair = new JPanel();
    repair.setBounds(0, 0, 743, 399);
    repair.setBackground(Color.BLACK);      

    //Assigns Panels to combo options
    cards = new JPanel(new CardLayout());
    cards.setBounds(5, 29, 743, 399);
    cards.setLayout(null);
    cards.add(fuel, FUEL);
    cards.add(toll, TOLL);
    cards.add(repair, REPAIR);

    NewExpense.add(cards);



    JPanel Income = new JPanel();
    tabbedPane.addTab("Income", null, Income, null);
    Income.setLayout(null);

    JPanel Results = new JPanel();
    tabbedPane.addTab("Result", null, Results, null);
    Results.setLayout(null);

}

@Override
public void itemStateChanged(ItemEvent e) {
            CardLayout cl = (CardLayout)(cards.getLayout());
            cl.show(cards, (String)e.getItem());


}
}

1 个答案:

答案 0 :(得分:1)

cards = new JPanel(new CardLayout());
cards.setBounds(5, 29, 743, 399);
cards.setLayout(null); // ???!!!

真的?您正在将卡片布局设置为CardLayout,然后立即将其设置为null。当你尝试使用卡片“布局”时,你会得到一个NullPointerException - 毫不奇怪!